MFC串口调试助手编程辅助小资料

1)UINT 32位无符号整数;

2)LPVOID是一个没有类型的指针,也就是说你可以将任意类型的指针赋值给LPVOID类型的变量(一般作为参数传递),然后在使用的时候再转换回来

3)DWORD就是Double Word, 每个word为2个字节的长度,DWORD双字即为4个字节,每个字节是8位,共32位;
4)COMSTAT结构包含串口的信息,结构定义如下:

typedef struct _COMSTAT { // cst
DWORD fCtsHold : 1;   // Tx waiting for CTS signal
DWORD fDsrHold : 1;   // Tx waiting for DSR signal
DWORD fRlsdHold : 1;  // Tx waiting for RLSD signal
DWORD fXoffHold : 1;  // Tx waiting, XOFF char rec''d
DWORD fXoffSent : 1;  // Tx waiting, XOFF char sent
DWORD fEof : 1;       // EOF character sent
DWORD fTxim : 1;      // character waiting for Tx
DWORD fReserved : 25; // reserved
DWORD cbInQue;        // bytes in input buffer
DWORD cbOutQue;       // bytes in output buffer
} COMSTAT, *LPCOMSTAT;

5)SendMessage这两个函数将指定的消息发送到一个或多个窗口。此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回

6)LRESULT SendMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM IParam)

hWnd:其窗口程序将接收消息的窗口的句柄。如果此参数为 HWND_BROADCAST,则消息将被发送到系统中所有顶层窗口,包括无效或不可见的非自身拥有的窗口、被覆盖的窗口和弹出式窗口,但消息不被发送到子窗口。

Msg:指定被发送的消息。

wParam:指定附加的消息特定信息。

IParam:指定附加的消息特定信息。

返回值:返回值指定消息处理的结果,依赖于所发送的消息

7)AFX_msg为消息标志,它向系统声明:有消息映射到函数实现体;而在map宏定义中,就有具体消息和此函数的映射定义(可以是自定义,也可以是系统自动完成的)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
里面讲了MSComm控件 、 WinAPI 串口通信、CSerial类,例子很详实,每一步操作都有解释。作者是龚建伟,有一个个人主页www.gjwtech.com,讲串口通信的内容比较多,还行,大家可以先看看这个主页。 本书目录 第1章轻松体验串口通信编程调试 1.1初识串口 1.1.1从外观上了解串口 1.1.2 串口通信的发展前景 1.2 自己制作简单的串口线 1.2.1 三线制串口接线的规定 1.2.2 焊接制作自己的串口连接线 1.3 调试串口通信程序时的几种使用串口的技巧 1.3.1 查看计算机串口资源 1.3.2 常规调试-2个物理串口之间的通信 1.3.3 特殊调试-单个物理串口之间的通信 1.3.4 虚拟串口-为计算机添加取之不尽的串口资源 1.4 使用串口调试助手来体验串口通信 1.5体验Windows环境下Visual C++串口通信编程 1.6体验DOS环境下Turbo C串口通信编程 第2章 VC多线程串口编程工具CSerialPort类 2.1 类功能及成员函数介绍 2.2 应用CSerialPort类编制基于对话框的应用程序 2.3 应用CSerialPort类编制基于单文档的应用程序 2.4对CSerialPort类的改进 2.5 在Visual C++.NET中应用CSerialPort类 第3章 控件MSComm串口编程 3.1 MSComm控件详细介绍 3.1.1 VC中应用MSComm控件编程步骤 3.1.2 MSComm控件串行通信处理方式 3.1.3 MSComm 控件的属性说明 3.1.4 MSComm控件错误信息 3.2使用MSComm控件的几个疑难问题 3.2.1使用VARIANT 和SAFEARRAY 数据类型从串口读写数据 3.2.2 MSComm控件能离开对话框独立存在吗? 3.2.3 如何发送接收ASCII值为0和大于128的字符? 3.2.4 在同一程序中用MSComm控件控制多个串口的具体操作方法 3.2.5解决使用控件编程时程序占用的内存会不断增大的问题 3.2.6在没有安装Visual Studio的计算机上如何使用MSComm控件 3.2.7 在MSComm控件串口编程时遇到的其它问题说明 3.3在基于单文档(SDI)程序中应用MSComm控件 3.4应用MSComm控件控制多个串口实例 第4章 Windows API串口编程 4.1 Windows API串口编程概述 4.2 API串口编程中用到的结构及相关概念说明 4.2.1 DCB(Device Control Block)结构 4.2.2超时设置COMMTIMEOUTS结构 4.2.3 OVERLAPPED异步I/O重叠结构 4.2.4 通信错误与通信设备状态 4.2.5 串行通信事件 4.3 Windows API串行通信函数 4.4 Win32 API串口通信编程的一般流程和特殊实例 4.4.1 Win32 API串口通信编程的一般流程 4.4.2 用查询方式读串口 4.4.3同步I/O读写数据 4.4.4 关于流控制的设置问题 4.5 CSerialPort类中的API函数编程应用剖析 4.6 Win32 API串口编程TTY(虚拟终端)实例 4.7 Windows API串口精简例程 第5章 串口调试助手V2.2详细编程 5.1 建立SCOMM程序工程实现界面功能 5.2 串口的初始化及关闭 5.3串口数据的发送与接收及十六进制数据的处理 5.3.1 十六进数据发送处理 5.3.2 手动发送处理 5.3.3自动发送处理 5.3.4 接收处理及十六进制显示 5.4 其它辅助功能的实现 5.4.1 接收数据的文件保存 5.4.2 实现小文件发送 5.4.3 图钉按钮功能使程序能浮在最上层 5.4.4 对话框动画图标的实现 5.4.5 超链接功能的实现 5.4.6 如何打开帮网页文件 第6章 DOS环境下的Turbo C串口编程及通用实例GSerial类 6.1 PC机异步通信适配器8250及其编程操作 6.1.1 INS8250内部寄存器及其选择方式 6.1.2 波特率设置 6.1.3数据位、奇偶校验、停止位等数据格式设置 6.1.4 查询I/O方式相关设置 6.1.5 中断I/O通信方式相关设置 6.1.6 Modem寄存器 6.2 COMRXTX程序实例
以下是一个简单的MFC串口调试助手的源码,可以实现串口的打开、关闭、发送和接收数据。需要注意的是,本程序仅供参考,具体实现可能因不同环境而异。 ``` // SerialPortTestDlg.cpp : implementation file // #include "stdafx.h" #include "SerialPortTest.h" #include "SerialPortTestDlg.h" #include "afxdialogex.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CSerialPortTestDlg dialog CSerialPortTestDlg::CSerialPortTestDlg(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_SERIALPORTTEST_DIALOG, pParent) , m_strSend(_T("")) , m_strReceive(_T("")) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CSerialPortTestDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDIT_SEND, m_strSend); DDX_Text(pDX, IDC_EDIT_RECEIVE, m_strReceive); } BEGIN_MESSAGE_MAP(CSerialPortTestDlg, CDialogEx) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON_OPEN, &CSerialPortTestDlg::OnBnClickedButtonOpen) ON_BN_CLICKED(IDC_BUTTON_CLOSE, &CSerialPortTestDlg::OnBnClickedButtonClose) ON_BN_CLICKED(IDC_BUTTON_SEND, &CSerialPortTestDlg::OnBnClickedButtonSend) ON_MESSAGE(WM_COMM_RXCHAR, &CSerialPortTestDlg::OnCommRxChar) END_MESSAGE_MAP() // CSerialPortTestDlg message handlers BOOL CSerialPortTestDlg::OnInitDialog() { CDialogEx::OnInitDialog(); // 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_pSerial = new CSerialPort; m_pSerial->m_hWnd = m_hWnd; // 初始化串口列表 for (int i = 1; i <= 16; i++) { CString strCom; strCom.Format(_T("COM%d"), i); ((CComboBox*)GetDlgItem(IDC_COMBO_COM))->AddString(strCom); } ((CComboBox*)GetDlgItem(IDC_COMBO_COM))->SetCurSel(0); // 初始化波特率列表 ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("1200")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("2400")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("4800")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("9600")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("19200")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("38400")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("57600")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->AddString(_T("115200")); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->SetCurSel(3); // 初始化数据位列表 ((CComboBox*)GetDlgItem(IDC_COMBO_DATABITS))->AddString(_T("5")); ((CComboBox*)GetDlgItem(IDC_COMBO_DATABITS))->AddString(_T("6")); ((CComboBox*)GetDlgItem(IDC_COMBO_DATABITS))->AddString(_T("7")); ((CComboBox*)GetDlgItem(IDC_COMBO_DATABITS))->AddString(_T("8")); ((CComboBox*)GetDlgItem(IDC_COMBO_DATABITS))->SetCurSel(3); // 初始化停止位列表 ((CComboBox*)GetDlgItem(IDC_COMBO_STOPBITS))->AddString(_T("1")); ((CComboBox*)GetDlgItem(IDC_COMBO_STOPBITS))->AddString(_T("1.5")); ((CComboBox*)GetDlgItem(IDC_COMBO_STOPBITS))->AddString(_T("2")); ((CComboBox*)GetDlgItem(IDC_COMBO_STOPBITS))->SetCurSel(0); // 初始化校验位列表 ((CComboBox*)GetDlgItem(IDC_COMBO_PARITY))->AddString(_T("无")); ((CComboBox*)GetDlgItem(IDC_COMBO_PARITY))->AddString(_T("奇校验")); ((CComboBox*)GetDlgItem(IDC_COMBO_PARITY))->AddString(_T("偶校验")); ((CComboBox*)GetDlgItem(IDC_COMBO_PARITY))->SetCurSel(0); return TRUE; // return TRUE unless you set the focus to a control } void CSerialPortTestDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 { CDialogEx::OnPaint(); } } HCURSOR CSerialPortTestDlg::OnQueryDragIcon() { return static_cast<HCURSOR>(m_hIcon); } void CSerialPortTestDlg::OnBnClickedButtonOpen() { // 获取串口参数 CString strCom, strBaudrate, strDatabits, strStopbits, strParity; ((CComboBox*)GetDlgItem(IDC_COMBO_COM))->GetWindowText(strCom); ((CComboBox*)GetDlgItem(IDC_COMBO_BAUDRATE))->GetWindowText(strBaudrate); ((CComboBox*)GetDlgItem(IDC_COMBO_DATABITS))->GetWindowText(strDatabits); ((CComboBox*)GetDlgItem(IDC_COMBO_STOPBITS))->GetWindowText(strStopbits); ((CComboBox*)GetDlgItem(IDC_COMBO_PARITY))->GetWindowText(strParity); // 设置串口参数 m_pSerial->m_nComPort = _ttoi(&strCom.Right(1)); m_pSerial->m_nBaud = _ttoi(strBaudrate); if (strDatabits == _T("5")) { m_pSerial->m_nDataBits = 5; } else if (strDatabits == _T("6")) { m_pSerial->m_nDataBits = 6; } else if (strDatabits == _T("7")) { m_pSerial->m_nDataBits = 7; } else if (strDatabits == _T("8")) { m_pSerial->m_nDataBits = 8; } if (strStopbits == _T("1")) { m_pSerial->m_nStopBits = ONESTOPBIT; } else if (strStopbits == _T("1.5")) { m_pSerial->m_nStopBits = ONE5STOPBITS; } else if (strStopbits == _T("2")) { m_pSerial->m_nStopBits = TWOSTOPBITS; } if (strParity == _T("无")) { m_pSerial->m_nParity = NOPARITY; } else if (strParity == _T("奇校验")) { m_pSerial->m_nParity = ODDPARITY; } else if (strParity == _T("偶校验")) { m_pSerial->m_nParity = EVENPARITY; } // 打开串口 if (m_pSerial->Open()) { GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(FALSE); GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(TRUE); } else { AfxMessageBox(_T("无法打开串口!")); } } void CSerialPortTestDlg::OnBnClickedButtonClose() { // 关闭串口 m_pSerial->Close(); GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(TRUE); GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(FALSE); } void CSerialPortTestDlg::OnBnClickedButtonSend() { // 发送数据 UpdateData(TRUE); m_pSerial->Write((LPCTSTR)m_strSend, m_strSend.GetLength()); } afx_msg LRESULT CSerialPortTestDlg::OnCommRxChar(WPARAM wParam, LPARAM lParam) { // 接收数据 BYTE szBuf[4096]; int nCount = m_pSerial->Read(szBuf, 4096); if (nCount > 0) { szBuf[nCount] = 0; m_strReceive += (LPCTSTR)szBuf; UpdateData(FALSE); } return 0; } ``` 需要注意的是,本程序使用了一个名为“CSerialPort”的类来实现串口的打开、关闭、发送和接收数据的功能。该类的实现代码如下: ``` class CSerialPort { public: CSerialPort() { m_hComm = NULL; m_hWnd = NULL; m_nComPort = 1; m_nBaud = 9600; m_nDataBits = 8; m_nStopBits = ONESTOPBIT; m_nParity = NOPARITY; } ~CSerialPort() { Close(); } BOOL Open() { Close(); CString strCom; strCom.Format(_T("\\\\.\\COM%d"), m_nComPort); m_hComm = CreateFile(strCom, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (m_hComm == INVALID_HANDLE_VALUE) { return FALSE; } DCB dcb; GetCommState(m_hComm, &dcb); dcb.BaudRate = m_nBaud; dcb.ByteSize = m_nDataBits; dcb.StopBits = m_nStopBits; dcb.Parity = m_nParity; SetCommState(m_hComm, &dcb); COMMTIMEOUTS timeouts; timeouts.ReadIntervalTimeout = MAXDWORD; timeouts.ReadTotalTimeoutMultiplier = 0; timeouts.ReadTotalTimeoutConstant = 0; timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = 0; SetCommTimeouts(m_hComm, &timeouts); SetCommMask(m_hComm, EV_RXCHAR); return TRUE; } void Close() { if (m_hComm) { CloseHandle(m_hComm); m_hComm = NULL; } } int Read(LPBYTE lpBuf, int nCount) { DWORD dwRead = 0; if (m_hComm && ReadFile(m_hComm, lpBuf, nCount, &dwRead, NULL)) { return (int)dwRead; } return 0; } BOOL Write(LPCTSTR lpBuf, int nCount) { DWORD dwWritten = 0; if (m_hComm && WriteFile(m_hComm, lpBuf, nCount, &dwWritten, NULL)) { return TRUE; } return FALSE; } HWND m_hWnd; int m_nComPort; int m_nBaud; int m_nDataBits; int m_nStopBits; int m_nParity; protected: HANDLE m_hComm; }; ``` 该类的实现使用了Win32 API中的串口相关函数,可以通过修改该类的实现来适应不同的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值