📄 serialporttestdlg.cpp
字号:
// SerialPortTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SerialPortTest.h"
#include "SerialPortTestDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CSerialPortTestDlg dialog
CSerialPortTestDlg::CSerialPortTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSerialPortTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSerialPortTestDlg)
m_strReceive = _T("");
m_strTransmit = _T("");
HexData = FALSE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSerialPortTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerialPortTestDlg)
DDX_Control(pDX, IDC_CHECK1, m_HexCtrl);
DDX_Control(pDX, IDC_Send, m_bSend);
DDX_Control(pDX, IDC_RECEIVE, m_CtrlReceive);
DDX_Text(pDX, IDC_RECEIVE, m_strReceive);
DDX_Text(pDX, IDC_TRANSMIT, m_strTransmit);
DDX_Check(pDX, IDC_CHECK1, HexData);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSerialPortTestDlg, CDialog)
//{{AFX_MSG_MAP(CSerialPortTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_OPEN, OnOpen)
ON_MESSAGE(WM_COMM_DATA_CALLBACK,OnDataHandle)
ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
ON_BN_CLICKED(IDC_Send, OnSend)
ON_BN_CLICKED(IDC_Clear, OnClear)
ON_BN_CLICKED(IDC_ClearSend, OnClearSend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerialPortTestDlg message handlers
BOOL CSerialPortTestDlg::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
//初始化变量
BaudRates[0]=1200;
BaudRates[1]=2400;
BaudRates[2]=4800;
BaudRates[3]=9600;
BaudRates[4]=19200;
BaudRates[5]=38400;
BaudRates[6]=57600;
BaudRates[7]=115200;
PortIDs[0]="COM1:";
PortIDs[1]="COM2:";
PortIDs[2]="COM3:";
PortIDs[3]="COM4:";
PortNo=4;
BaudRate=3;
DataBits=8;
StopBits=ONESTOPBIT;
Parity=NOPARITY;
bOpenPort=FALSE;
HexData=TRUE;
m_HexCtrl.SetCheck(1);
m_strDataReceived=_T("");
// SetDlgItemText(IDC_TRANSMIT,_T("0f f0 0f 00 01 01 08 01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 60 00 30 00 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 30 00 48 00 48 00 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 00 07 80 07 80 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 7f fe 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 20 08 20 04 40 04 40 02 80 02 80 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0c 60 0c 60 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 40 02 40 04 80 04 80 09 00 09 00 12 00 24 00 00 00 00 00 00 00 00 00 ac f6 0d 0f f0 0f 00 01 01 08 01 00 01 00 01 00 04 00 04 00 04 00 08 00 08 00 1f f8 20 08 40 10 00 10 08 20 04 40 02 80 01 00 00 c0 00 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff fe 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 3f 04 40 f8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 80 02 80 02 80 02 80 02 80 02 80 02 80 02 80 02 80 02 80 02 80 02 80 02 80 02 80 02 80 02 80 00 00 00 00 00 00 00 00 00 00 00 00 63 18 63 18 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 00 04 00 08 00 0c 00 0c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 60 00 60 00 20 00 40 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 44 00 88 01 10 01 98 01 98 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 54 0d"));
m_pSerialPort=new CSerialPortDll();
m_psenddata=FALSE;
start=0;
return TRUE; // return TRUE unless you set the focus to a control
}
void CSerialPortTestDlg::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 CSerialPortTestDlg::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 CSerialPortTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSerialPortTestDlg::OnDataCallBack(char *data, int length, DWORD userdata)
{
CSerialPortTestDlg *pWnd=(CSerialPortTestDlg *)userdata;
::SendMessage(pWnd->m_hWnd,WM_COMM_DATA_CALLBACK,(WPARAM)data,LPARAM(length));
}
void CSerialPortTestDlg::OnDataHandle(WPARAM wParam, LPARAM lParam)
{
//处理收到的数据
int length=(int)lParam;
char *data=(char*)wParam;
CString THex(_T(""));
int i;
if(length!=0)
{
if(HexData)
{
for(i=0;i<length;i++)
{
THex.Format(_T("%02x "),data[i] & 0xFF);
m_strDataReceived+=THex;
}
}
else
{
m_strDataReceived=CString(data).Left(length);
}
m_strReceive+=m_strDataReceived;
m_strDataReceived=_T("");
SetDlgItemText(IDC_RECEIVE,m_strReceive);
m_CtrlReceive.SetSel(m_strReceive.GetLength(),m_strReceive.GetLength(),FALSE);
}
//以上为将接收字符显示在编辑框中
//下面对接收的数据处理
for(i=0;i<16;i++)
{
d[i]=data[i] & 0xFF;
}
if((d[0]==0x0f)&&(d[1]==0xf0)&&(d[2]==0x0f))
{
if((d[3]==0x00)&&(d[4]==0x01)&&(d[5]==0x00)&&(d[6]==0x09))
{
if(((d[7]+d[8]+d[9]+d[10]+d[11]+d[12])&0xFF)==d[13])
{
if((d[7]^d[8]^d[9]^d[10]^d[11]^d[12])==d[14])
{
if(d[15]==0x0D)
{
if(d[7]=0x01)
{
start=256*256*d[8]+256*d[9]+d[10];
if(start>261442)
{
AfxMessageBox("超出文件长度!");
return;
}
m_psenddata=TRUE;
SendData();
//OnButtonSend();
}
else
MessageBox("无此数据库");
}
else
MessageBox("结束出错");
}
else
MessageBox("异或校验出错");
}
else
MessageBox("和校验出错");
}
else
MessageBox("地址出错");
// MessageBox("地址");
}
else
MessageBox("包头出错");
}
void CSerialPortTestDlg::OnOpen()
{
// TODO: Add your control notification handler code here
if(!bOpenPort)
{
PortID=PortIDs[PortNo-1];
bOpenPort=m_pSerialPort->OpenPort(PortID,BaudRates[BaudRate],DataBits,StopBits,Parity,OnDataCallBack,(DWORD)this);
if(bOpenPort)
{
SetDlgItemText(IDC_OPEN,_T("ClosePort"));
m_bSend.EnableWindow(TRUE);
}
}
else
{
m_pSerialPort->ClosePort();
bOpenPort=FALSE;
SetDlgItemText(IDC_OPEN,_T("OpenPort")); //将按键名改为"OpenPort"
m_bSend.EnableWindow(FALSE); //使发送按键无效/变灰
}
}
void CSerialPortTestDlg::OnSend()
{
// TODO: Add your control notification handler code here
/* DWORD dwCharToWrite=0;
DWORD dwBytesWritten=0;
int i,j;
DataLength=0;
DataStart=0;
GetDlgItemText(IDC_TRANSMIT,m_strTransmit);
if(!HexData)
{
dwCharToWrite=(DWORD)m_strTransmit.GetLength();
}
else
{
for(i=0;i<m_strTransmit.GetLength()/2;i++)
{
if(((m_strTransmit.GetAt(i*2)>='0'&&m_strTransmit.GetAt(i*2)<='9')||(m_strTransmit.GetAt(i*2)>='A'&&m_strTransmit.GetAt(i*2)<='F'))&&((m_strTransmit.GetAt(i*2+1)>='0'&&m_strTransmit.GetAt(i*2+1)<='9')||(m_strTransmit.GetAt(i*2+1)>='A'&&m_strTransmit.GetAt(i*2+1)<='F')))
{
dwCharToWrite++;
}
}
}
dwBytesWritten=0;
if(bOpenPort && dwCharToWrite !=0)
{
char* buf=new char[dwCharToWrite+1];
if(!HexData)
{
for(i=0;i<(int)dwCharToWrite;i++)
{
buf[i]=(char)m_strTransmit.GetAt(i);
}
}
else
{
j=0;
for(i=0;i<m_strTransmit.GetLength()/2;i++)
{
if(((m_strTransmit.GetAt(i*2)>='0'&&m_strTransmit.GetAt(i*2)<='9')||(m_strTransmit.GetAt(i*2)>='A'&&m_strTransmit.GetAt(i*2)<='F'))&&((m_strTransmit.GetAt(i*2+1)>='0'&&m_strTransmit.GetAt(i*2+1)<='9')||(m_strTransmit.GetAt(i*2+1)>='A'&&m_strTransmit.GetAt(i*2+1)<='F')))
{
if(m_strTransmit.GetAt(i*2+1)>='0'&&m_strTransmit.GetAt(i*2+1)<='9')
{
buf[j]=m_strTransmit.GetAt(i*2+1)-48;
}
else
{
buf[j]=m_strTransmit.GetAt(i*2+1)-55;
}
if(m_strTransmit.GetAt(i*2)>='0'&&m_strTransmit.GetAt(i*2)<='9')
{
buf[j]+=(m_strTransmit.GetAt(i*2)-48)*16;
}
else
{
buf[j]+=(m_strTransmit.GetAt(i*2)-55)*16;
}
j++;
}
}
}
buf[dwCharToWrite] = 13;//回车符
dwBytesWritten=m_pSerialPort->WritePort(buf,dwCharToWrite+1);
if(dwBytesWritten==0)
{
AfxMessageBox(_T("无法向端口写入数据!"));
}
delete[] buf;
}*///以上为将发送编辑框的内容发送出去
//**************************************************//
/*以下为发送字库文件
if(!m_psenddata)
return;
// if(!m_bSerialPortOpened) return; //检查串口是否打开
CFile fp;
if(!(fp.Open("ziku.bin",CFile::modeRead)))
{
AfxMessageBox("Open file failed!");
return;
}
fp.SeekToEnd();
int i;
unsigned long fplength=fp.GetLength();
// CString st;
// st.Format("%d",fplength);
// MessageBox(st);
char* fpBuff,*Re1,a3[272];
for(i=0;i<11;i++)
{
a3[i]=d[i];
}
a3[5]=0x01;
a3[6]=0x08;
a3[11]=0x01;
a3[12]=0x00;
fpBuff=new char[fplength];
fp.SeekToBegin();
if(fp.Read(fpBuff,fplength)<1)
{
fp.Close();
return;
}
fp.Close();
for(i=0;i<256;i++)
{
a3[13+i]=*(fpBuff+start+i);
}
a3[269]=a3[7];
a3[270]=a3[7];
for(i=0;i<261;i++)
{
a3[269]+=a3[8+i];
a3[270]^=a3[8+i];
}
a3[271]=0x0D;
Re1=&a3[0];
m_pSerialPort->WritePort(Re1,272);
// dwBytesWritten=m_pSerialPort->WritePort(Re1,272);
// if(fplength==0)
// {
// AfxMessageBox(_T("无法向端口写入数据!"));
// }
delete[] fpBuff,Re1;
m_psenddata=TRUE;
*/
}
void CSerialPortTestDlg::OnCheck1()
{
// TODO: Add your control notification handler code here
if (!HexData)
{
HexData=TRUE;
}
else
{
HexData=FALSE;
}
}
void CSerialPortTestDlg::OnClear() //将接收编辑框中的内容清空
{
// TODO: Add your control notification handler code here
m_strReceive=_T(""); //将接收编辑框的变量赋值为空
SetDlgItemText(IDC_RECEIVE,m_strReceive); //将变量的值显示在编辑框内
}
void CSerialPortTestDlg::SendData()
{
if(!m_psenddata)
return;
// if(!m_bSerialPortOpened) return; //检查串口是否打开
CFile fp;
if(!(fp.Open("ziku.bin",CFile::modeRead)))
{
AfxMessageBox("Open file failed!");
return;
}
fp.SeekToEnd();
int i;
unsigned long fplength=fp.GetLength();
// CString st;
// st.Format("%d",fplength);
// MessageBox(st);
char* fpBuff,*Re1,a3[272];
for(i=0;i<11;i++)
{
a3[i]=d[i];
}
a3[5]=0x01;
a3[6]=0x08;
a3[11]=0x01;
a3[12]=0x00;
fpBuff=new char[fplength];
fp.SeekToBegin();
if(fp.Read(fpBuff,fplength)<1)
{
fp.Close();
return;
}
fp.Close();
for(i=0;i<256;i++)
{
a3[13+i]=*(fpBuff+start+i);
}
a3[269]=a3[7];
a3[270]=a3[7];
for(i=0;i<261;i++)
{
a3[269]+=a3[8+i];
a3[270]^=a3[8+i];
}
a3[271]=0x0D;
Re1=&a3[0];
CString s;
for(i=0;i<272;i++)
{
s.Format("%02x",a3[i]&0xff);
m_strTransmit+=s+' ';
}
UpdateData(FALSE);
m_pSerialPort->WritePort(Re1,272);
// dwBytesWritten=m_pSerialPort->WritePort(Re1,272);
// if(fplength==0)
// {
// AfxMessageBox(_T("无法向端口写入数据!"));
// }
delete[] fpBuff,Re1;
m_psenddata=TRUE;
}
void CSerialPortTestDlg::OnClearSend()
{
// TODO: Add your control notification handler code here
m_strTransmit=_T(""); //将接收编辑框的变量赋值为空
SetDlgItemText(IDC_TRANSMIT,m_strTransmit); //将变量的值显示在编辑框内
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -