⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dianbiaotestdlg.cpp

📁 本人设计的DL/T645规约电能表的抄表和电表模拟软件
💻 CPP
字号:
// dianbiaotestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "dianbiaotest.h"
#include "dianbiaotestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
UINT test=0;
CString test_data="";
CWnd *pview;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
CSerialPort m_port;
UINT CommThreadProc(LPVOID pParam)
{
	while(1)
	{
/*		revice=0;
		while (m_port.m_bThreadAlive)
		{
			SetEvent(m_port.m_hShutdownEvent);
			Sleep(1000);
			revice=0;
		}
		Sleep(10000);
		if(m_port.InitPort(pview, 4, 1200,'E',8,1,EV_RXCHAR|EV_TXEMPTY,512))
		{
			revice=0;
			m_port.StartMonitoring();
			comm_start=comm_end=0;				
			int n=0;
			m_port.m_szWriteBuffer[n++]=0xFE;
			m_port.m_szWriteBuffer[n++]=0x68;
			m_port.m_szWriteBuffer[n++]=0x28;
			m_port.m_szWriteBuffer[n++]=0x28;
			m_port.m_szWriteBuffer[n++]=0x28;
			m_port.m_szWriteBuffer[n++]=0x28;
			m_port.m_szWriteBuffer[n++]=0x28;
			m_port.m_szWriteBuffer[n++]=0x02;
			m_port.m_szWriteBuffer[n++]=0x68;
			m_port.m_szWriteBuffer[n++]=0x01;
			m_port.m_szWriteBuffer[n++]=0x02;
			m_port.m_szWriteBuffer[n++]=0x43;
			m_port.m_szWriteBuffer[n++]=0xC3;
			m_port.m_szWriteBuffer[n++]=0xA3;
			m_port.m_szWriteBuffer[n++]=0x16;
			m_port.WriteChar(&m_port,n);
			//				FE 68 28 28 28 28 28 02 68 01 02 43 C3 A3 16 
			Sleep(10000);
		}*/
	}
	return 0;
}
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDianbiaotestDlg dialog

CDianbiaotestDlg::CDianbiaotestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDianbiaotestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDianbiaotestDlg)
	m_comm_data = _T("");
	m_input_address = _T("");
	m_input_order = _T("");
	m_output_address = _T("");
	m_output_data = _T("");
	port = 0;
	m_dianbiao_type = -1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDianbiaotestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDianbiaotestDlg)
	DDX_Text(pDX, IDC_COMM_DATA, m_comm_data);
	DDX_Text(pDX, IDC_INPUT_ADDRESS, m_input_address);
	DDX_Text(pDX, IDC_INPUT_ORDER, m_input_order);
	DDX_Text(pDX, IDC_OUTPUT_ADDRESS, m_output_address);
	DDX_Text(pDX, IDC_OUTPUT_DATA, m_output_data);
	DDX_Text(pDX, IDC_PORT, port);
	DDX_Radio(pDX, IDC_DIANBIAO_TYPE, m_dianbiao_type);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDianbiaotestDlg, CDialog)
	//{{AFX_MSG_MAP(CDianbiaotestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDSEND, OnSend)
	ON_BN_CLICKED(IDC_READ_DATA, OnReadData)
	ON_BN_CLICKED(IDCLEAR, OnClear)
	ON_BN_CLICKED(IDC_TEST, OnTest)
	ON_BN_CLICKED(IDOPEN_PORT, OnPort)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDianbiaotestDlg message handlers

BOOL CDianbiaotestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	pview=this;

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	if(m_port.InitPort(this, 4, 1200,'E',8,1,EV_RXCHAR| EV_CTS|EV_TXEMPTY,512))
	{
		m_port.StartMonitoring();
	}
	SetTimer(102,500,0);
	SetTimer(101,1500,0);
	SetTimer(103,50,0);

	m_input_order="9010";
	m_input_address="282828282802";
	port=1;
	m_dianbiao_type=0;
	UpdateData(0);
comm_start=comm_end=CommStatus=LEN_R=OK_R=0;

//	AfxBeginThread(CommThreadProc,this,THREAD_PRIORITY_BELOW_NORMAL);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDianbiaotestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDianbiaotestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDianbiaotestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDianbiaotestDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	static BOOL flag=0;
	switch(nIDEvent)
	{
	case 103:
		if(test)
		{
			
			revice++;
			if(OK_R)
			{
				if(!flag)
				{
					flag=1;
					if(LEN_R>1)
					{
						char ccc[10];
						itoa(test,ccc,16);
						m_comm_data+=ccc;
						m_comm_data+="  ";
						UpdateData(0);
					}
				}
				else 
				{
					if(revice>6)
					{
						revice=0;
						OK_R=0;
						flag=0;
						CommStatus=0;
						test++;
						char cc[5];
						sprintf(cc,"%04x",test);
						m_input_order=cc;
						for(unsigned char i=comm_start;i<comm_end;i++)
						{
							itoa(comm_data[i],cc,16);
							if(strlen(cc)<2)
							{
								cc[1]=cc[0];
								cc[0]='0';
							}
							m_comm_data+=cc;
							m_comm_data+=" ";
						}
						comm_start=comm_end=0;

						UpdateData(0);
						OnSend();
					}
					
				}
			}
			else
			{
				
				if(revice>100)
					OnSend();
			}
		}
			/*		if(revice>2)
		if(comm_start!=comm_end)
		{
			comm_start=comm_end;
			revice=0;
		}*/
		break;
	case 102:
if(!test&&OK_R)	
{
	OK_R=0;
	if(comm_start!=comm_end)
	{
		revice=0;
		char cc[3]={0,0,0};
		for(unsigned char i=comm_start;i<comm_end;i++)
		{
			itoa(comm_data[i],cc,16);
			if(strlen(cc)<2)
			{
				cc[1]=cc[0];
				cc[0]='0';
			}
			m_comm_data+=cc;
			m_comm_data+=" ";
		}
		comm_start=i;
		m_comm_data.MakeUpper();
		UpdateData(0);
		if(comm_end>13)
		{
			char cc[5];
			itoa(test,cc,16);
			test_data+=cc;
			test_data+=" ";
		}
			
	}
}
	break;
	case 101:
/*		if(test>0x9000&&test<0xe000)
		{
			test++;
			char cc[5];
			itoa(test,cc,16);
			m_input_order=cc;
			UpdateData(0);
			OnSend();
		}*/
		break;
	}
	
	CDialog::OnTimer(nIDEvent);
}

void CDianbiaotestDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	UpdateData(1);
	int i,k,n=0;
	 char cc[3]={0,0,0},rn[3]={0x0d,0x0a,0};
	 unsigned char c;
	m_port.m_szWriteBuffer[n++]=(unsigned char )0xfe;
	m_port.m_szWriteBuffer[n++]=(unsigned char )0x68;
//	m_comm_data+=rn;
//	m_comm_data+="fe 68";
	m_input_address.MakeLower();
	//许继地址:282828282801辽精地址:000000000000
	for(i=0;i<6;i++)
	{
		c=m_input_address.GetAt(i*2);
		if((c>='a')&&(c<='f'))
			k=(10+c-'a')*16;
		else
			k=(c-'0')*16;

		c=m_input_address.GetAt(i*2+1);
		if((c>='a')&&(c<='f'))
			k+=(10+c-'a');
		else
			k+=(c-'0');
		m_port.m_szWriteBuffer[n++]=k;
		itoa(k,cc,16);
//		if(strlen(cc)<2)
//			m_comm_data+=" 0";
//		else
//			m_comm_data+=" ";
//		m_comm_data+=cc;
	}
	m_port.m_szWriteBuffer[n++]=(unsigned char )0x68;
	switch(m_dianbiao_type)
	{
	case 0:
		m_port.m_szWriteBuffer[n++]=(unsigned char )0x01;
		m_port.m_szWriteBuffer[n++]=(unsigned char )0x02;
		//	m_comm_data+=" 68 01 02";
		m_input_order.MakeLower();
		for(i=1;i>=0;i--)
		{
			c=m_input_order.GetAt(i*2);
			if((c>='a')&&(c<='f'))
				k=(10+c-'a')*16;
			else
				k=(c-'0')*16;
			c=m_input_order.GetAt(i*2+1);
			if((c>='a')&&(c<='f'))
				k+=(10+c-'a');
			else
				k+=(c-'0');
			m_port.m_szWriteBuffer[n++]=k+0x33;
			itoa(k+0x33,cc,16);
		}
		break;
	case 1:
		m_port.m_szWriteBuffer[n++]=(unsigned char )0x03;
		m_port.m_szWriteBuffer[n++]=(unsigned char )0x00;
		m_port.m_szWriteBuffer[n++]=(unsigned char )0x00;
		break;
	}
	c=0;
	for(i=1;i<n;i++)
		c+=m_port.m_szWriteBuffer[i];
	m_port.m_szWriteBuffer[n++]=c;
	itoa(c,cc,16);
	//	if(strlen(cc)<2)
//		m_comm_data+=" 0";
//	else
//		m_comm_data+=" ";
//	m_comm_data+=cc;
	m_port.m_szWriteBuffer[n++]=0x16;
//	m_comm_data+=" 16";

	
	
//	m_comm_data+=rn;
	comm_start=comm_end=0;
	revice=0;
	m_port.WriteChar(&m_port,n);
		UpdateData(0);
}

void CDianbiaotestDlg::OnReadData() 
{
	// TODO: Add your control notification handler code here
		char cc[3]={0,0,0},status=0,len;

		for(unsigned char i=0;i<comm_end;i++)
		{
			itoa(comm_data[i],cc,16);
			if(strlen(cc)<2)
			{
				cc[1]=cc[0];
				cc[0]='0';
			}
			switch(status)
			{
			case 0:if(comm_data[i]==0x68) 
				   {
					   status=1;
					   len=0;
					   m_output_address="";
				   }
				break;
			case 1:			
				itoa(comm_data[i],cc,16);
			if(strlen(cc)<2)
			{
				cc[1]=cc[0];
				cc[0]='0';
			}

				m_output_address+=cc;
				if(++len>5)
				{
					status=2;
					m_output_data="";
				}
				break;
			case 2:status=3;
				break;
			case 3:status=4;
				break;
			case 4:len=comm_data[i];status=5;
				break;
			case 5:
			if(len--<1)
				status=6;
			else
			{
				itoa(comm_data[i]-0x33,cc,16);
			if(strlen(cc)<2)
			{
				cc[1]=cc[0];
				cc[0]='0';
			}
				m_output_data+=cc;
			}
			break;
			case 6:break;

			}
		}
//		m_comm_data.MakeUpper();
		UpdateData(0);

}

void CDianbiaotestDlg::OnClear() 
{
	// TODO: Add your control notification handler code here
	m_comm_data="";
		UpdateData(0);
}

void CDianbiaotestDlg::OnTest() 
{
	// TODO: Add your control notification handler code here
test=0x1330;	
}



void CDianbiaotestDlg::OnPort() 
{
	// TODO: Add your control notification handler code here
	UpdateData(1);
	if(m_port.InitPort(this, port, 1200,'E',8,1,EV_RXCHAR| EV_CTS|EV_TXEMPTY,512))
	{
		m_port.StartMonitoring();
	}
	else
		AfxMessageBox("串口打开错误");
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -