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

📄 dianbiaotestdlg.cpp

📁 自己设计的DL/T645标准电表抄表软件(VC6)
💻 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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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("");
	//}}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);
	//}}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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

BOOL CDianbiaotestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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, 1, 1200,'E',8,1,EV_RXCHAR|EV_TXEMPTY,512))
	{
		m_port.StartMonitoring();
	}
	SetTimer(102,500,0);
	m_input_order="1F90";
	m_input_address="999999999999";
	UpdateData(0);
	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
	if(comm_start!=comm_end)
	{
		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);
	}
	
	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();
	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;
	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=0;i<2;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);
		if(strlen(cc)<2)
			m_comm_data+=" 0";
		else
			m_comm_data+=" ";
		m_comm_data+=cc;
	}
	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;
	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);
}

⌨️ 快捷键说明

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