📄 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_strEditReceiveMsg = _T("");
m_strEditSendMsg = _T("");
m_unEditPosition = 0;
m_unEditVelocity = 0;
m_unEditVelocity2 = 0;
m_unEditPosition2 = 0;
m_strEditDispData2 = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bSerialPortOpened=FALSE; //初始状态:串口1没有打开
m_bSerialPortOpened2=FALSE; //初始状态:串口2没有打开
m_unPort=1;
m_unPort2=2;
m_strPortRXData2="";
m_strChecksum2="";
}
void CSerialPortTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerialPortTestDlg)
DDX_Control(pDX, IDC_STATIC_ICONS1, m_ctrlStaticIconS1);
DDX_Control(pDX, IDC_STATIC_ICONS2, m_ctrlStaticIconS2);
DDX_Control(pDX, IDC_STATIC_ICONS3, m_ctrlStaticIconS3);
DDX_Control(pDX, IDC_STATIC_ICONS4, m_ctrlStaticIconS4);
DDX_Control(pDX, IDC_STATIC_ICONS5, m_ctrlStaticIconS5);
DDX_Control(pDX, IDC_STATIC_ICONS6, m_ctrlStaticIconS6);
DDX_Control(pDX, IDC_STATIC_ICONS7, m_ctrlStaticIconS7);
DDX_Control(pDX, IDC_STATIC_ICONS8, m_ctrlStaticIconS8);
DDX_Control(pDX, IDC_STATIC_ICONS9, m_ctrlStaticIconS9);
DDX_Control(pDX, IDC_CHECK_SWITCH1, m_ctrlCheckSwitch1);
DDX_Control(pDX, IDC_CHECK_SWITCH2, m_ctrlCheckSwitch2);
DDX_Control(pDX, IDC_CHECK_SWITCH3, m_ctrlCheckSwitch3);
DDX_Control(pDX, IDC_CHECK_SWITCH4, m_ctrlCheckSwitch4);
DDX_Control(pDX, IDC_CHECK_SWITCH5, m_ctrlCheckSwitch5);
DDX_Control(pDX, IDC_CHECK_SWITCH6, m_ctrlCheckSwitch6);
DDX_Control(pDX, IDC_CHECK_SWITCH7, m_ctrlCheckSwitch7);
DDX_Control(pDX, IDC_CHECK_SWITCH8, m_ctrlCheckSwitch8);
DDX_Control(pDX, IDC_CHECK_SWITCH9, m_ctrlCheckSwitch9);
DDX_Control(pDX, IDC_COMBO_COMPORT2, m_ctrlComboComPort2);
DDX_Control(pDX, IDC_COMBO_COMPORT, m_ctrlComboComPort);
DDX_Text(pDX, IDC_EDIT_RECEIVEMSG, m_strEditReceiveMsg);
DDX_Text(pDX, IDC_EDIT_SENDMSG, m_strEditSendMsg);
DDX_Text(pDX, IDC_EDIT_POSITION, m_unEditPosition);
DDV_MinMaxUInt(pDX, m_unEditPosition, 0, 31);
DDX_Text(pDX, IDC_EDIT_VELOCITY, m_unEditVelocity);
DDV_MinMaxUInt(pDX, m_unEditVelocity, 0, 120);
DDX_Text(pDX, IDC_EDIT_VELOCITY2, m_unEditVelocity2);
DDX_Text(pDX, IDC_EDIT_POSITION2, m_unEditPosition2);
DDX_Text(pDX, IDC_EDIT_DISPDATA2, m_strEditDispData2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSerialPortTestDlg, CDialog)
//{{AFX_MSG_MAP(CSerialPortTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_COMM_RXCHAR, OnComm)
ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
ON_BN_CLICKED(IDC_BUTTON_OPEN2, OnButtonOpen2)
ON_BN_CLICKED(IDC_BUTTON_CLOSE2, OnButtonClose2)
//}}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
m_ctrlComboComPort.SetCurSel(0); //初始选择串口1
m_ctrlComboComPort2.SetCurSel(1); //初始选择串口2
//以下两句分别设置“打开串口”、“关闭串口”两个按状态的使能状态
GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(!m_bSerialPortOpened);
GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(m_bSerialPortOpened);
GetDlgItem(IDC_BUTTON_OPEN2)->EnableWindow(!m_bSerialPortOpened2);
GetDlgItem(IDC_BUTTON_CLOSE2)->EnableWindow(m_bSerialPortOpened2);
//载入图标
m_hIconRed = AfxGetApp()->LoadIcon(IDI_ICON_RED);
m_hIconOff = AfxGetApp()->LoadIcon(IDI_ICON_OFF);
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;
}
#define CR 0X0D //回车
#define LF 0X0A //换行
LONG CSerialPortTestDlg::OnComm(WPARAM ch, LPARAM port)
{
static char checksum=0,checksum1=0;
static int count1=0;//,count2=0,count3=0;
static unsigned char buf[20];
static char c1,c2; //用于计算半Byte校验
static int flag; //用于接收阶段标记
static int twoflag=0;
if(port == m_unPort)
{
if(ch>127) //是不是首字节
{
count1=0; //记录接收字符的个数
buf[count1]=ch;
checksum1= ch-128; //开始计算校验值
}
else
{
count1++;
buf[count1]=ch;
checksum1 = checksum1^ch;
if(count1==3) //包括校验字节在内的全部接收完毕
{
if(checksum1) //校验错
{
m_strEditReceiveMsg = "接收校验出错";
UpdateData(FALSE);
}
else
{
CString str;
unsigned char * temp=(unsigned char*)buf;
m_strEditReceiveMsg ="接收到的简单通信协议字节为:";
for(int i=0;i<4;i++)
{
str.Format("%02X ",*(buf+i));
m_strEditReceiveMsg += str;
}
UpdateData(FALSE);
}
if(count1>5) //防止出错
count1=0;
}
}
}
if(port==m_unPort2) //串口2的数据处理
{
m_strPortRXData2 += (char)ch;
switch(ch)
{
case '$':
checksum=0; //开始计算CheckSum
flag=0;
break;
case '*': //有效数据结束,可以$和*之间数据的半Byte校验值了
flag=2;
c2=checksum & 0x0f; c1=((checksum >> 4) & 0x0f);
if (c1 < 10) c1+= '0'; else c1 += 'A' - 10;
if (c2 < 10) c2+= '0'; else c2 += 'A' - 10;
break;
case CR: //这句必须加上,否则会出错的
break;
case LF: //数据包的最后一个字符
m_strPortRXData2.Empty();
break;
default:
if(flag>0) //注意:只有在接收到'*'后,flag才大于0
{
m_strChecksum2 += ch;
if(flag==1)
{
CString strCheck="";
strCheck.Format("%c%c",c1,c2);
if(strCheck!=m_strChecksum2) //校验计算不正确,说明接收数据出错
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -