建设营销型网站不足之处,建设网站平台的章程,洛阳建站推广公司,做网站最好的语言CSerialPort教程4.3.x (3) - CSerialPort在MFC中的使用
环境#xff1a;
系统#xff1a;windows 10 64位
编译器#xff1a;Visual Studio 2008前言
CSerialPort项目是一个基于C/C的轻量级开源跨平台串口类库#xff0c;可以轻松实现跨平台多操作系统的串口读写#x…CSerialPort教程4.3.x (3) - CSerialPort在MFC中的使用
环境
系统windows 10 64位
编译器Visual Studio 2008前言
CSerialPort项目是一个基于C/C的轻量级开源跨平台串口类库可以轻松实现跨平台多操作系统的串口读写同时还支持C#, Java, Python, Node.js等。
CSerialPort项目的开源协议自 V3.0.0.171216 版本后采用GNU Lesser General Public License v3.0
为了让开发者更好的使用CSerialPort进行开发特编写基于4.3.x版本的CSerialPort教程系列。
CSerialPort项目地址
https://github.com/itas109/CSerialPorthttps://gitee.com/itas109/CSerialPort
MFC完整示例程序地址
https://github.com/itas109/CSerialPort/tree/master/examples/CommMFChttps://gitee.com/itas109/CSerialPort/tree/master/examples/CommMFC
1. 新建基于对话框的MFC项目
新建一个基于对话框的MFC项目,解决方案名称为CommMFC
在CommMFC解决方案目录下载CSerialPort源码
$ cd CommMFC
$ git clone https://github.com/itas109/CSerialPort目录结构如下
D:/CommMFC $ tree
.
--- CommMFC
| --- CommMFC.aps
| --- CommMFC.cpp
| --- CommMFC.h
| --- CommMFC.rc
| --- CommMFC.vcproj
| --- CommMFCDlg.cpp
| --- CommMFCDlg.h
| --- ReadMe.txt
| --- res
| | --- CommMFC.ico
| | --- CommMFC.rc2
| --- Resource.h
| --- stdafx.cpp
| --- stdafx.h
| --- targetver.h
--- CommMFC.sln
--- CSerialPort
| --- include
| | --- CSerialPort
| | | --- SerialPort.h
| | | --- SerialPortInfo.h
| --- src
| | --- SerialPort.cpp
| | --- SerialPortBase.cpp
| | --- SerialPortInfo.cpp
| | --- SerialPortInfoBase.cpp
| | --- SerialPortInfoWinBase.cpp
| | --- SerialPortWinBase.cpp2. 设置CSerialPort头文件
右键【CommMFC根命名空间】-【属性】-【C/C】-【常规】-【附加包含目录】-添加CSerialPort的头文件目录
D:\CommMFC\CSerialPort\include或
$(ProjectDir)\..\CSerialPort\include3. 添加CSerialPort源文件
右键【CommMFC根命名空间】-【添加】-【新建筛选器(命名为CSerialPort)】
右键【CSerialPort筛选器】-【添加】-【现有项】-添加CSerialPort的src目录的所需文件()
所需文件清单如下
SerialPort.cppSerialPortBase.cppSerialPortWinBase.cppSerialPortInfo.cppSerialPortInfoBase.cppSerialPortInfoWinBase.cpp
注意
需要将添加的cpp文件的预编译头设置为不使用预编译头如右键【serialport.cpp】-【属性】-【C/C】-【预编译头】-【预编译头: 不使用预编译头】
如不设置会报错
serialport.cpp: fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include stdafx.h?
SerialPortBase.cpp: fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include stdafx.h?
SerialPortWinBase.cpp: fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include stdafx.h?
SerialPortInfo.cpp: fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include stdafx.h?
SerialPortInfoBase.cpp: fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include stdafx.h?
SerialPortInfoWinBase.cpp: fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include stdafx.h?4. 增加CSerialPort的必要依赖库
windows下CSerialPort必须的依赖库为setupapi.lib
右键【CommMFC根命名空间】-【属性】-【链接器】-【输入】-【附加依赖项】-添加setupapi.lib
5. 在MFC中添加CSerialPort代码
5.1 增加CSerialPort的头文件、继承类、接收函数及CSerialPort实例对象
在CommMFCDlg.h文件中
增加CSerialPort的头文件CCommMFCDlg类继承CSerialPortListener增加接收函数onReadEvent(const char *portName, unsigned int readBufferLen)增加CSerialPort的实例对象
代码如下
// CommMFCDlg.h : 头文件
//#pragma once// add by itas109
#include CSerialPort/SerialPort.h
#include CSerialPort/SerialPortInfo.h
using namespace itas109;
// end by itas109// CCommMFCDlg 对话框
class CCommMFCDlg : public CDialog, public CSerialPortListener // add by itas109
{...// add by itas109
private:void onReadEvent(const char *portName, unsigned int readBufferLen);// end by itas109// add by itas109
private:CSerialPort m_serialPort;// end by itas109
};
注意 如果CCommMFCDlg不继承CSerialPortListener调用connectReadEvent函数时会报错
CSerialPort::connectReadEvent: 不能将参数 1 从CCommMFCDlg *const 转换为itas109::CSerialPortListener *5.2 增加串口的相关实现代码
在CommMFCDlg.cpp文件增加
CCommMFCDlg::OnInitDialog()中增加CSerialPort的测试代码增加OnReceive函数的实现
// CommMFCDlg.cpp: 实现文件
...BOOL CCommMFCDlg::OnInitDialog()
{...// TODO: 在此添加额外的初始化代码// add by itas109m_serialPort.connectReadEvent(this);m_serialPort.init(COM1);m_serialPort.open();if (m_serialPort.isOpen()){m_serialPort.writeData(itas109, 7);}else{MessageBox(_T(open failed));}// end by itas109...
}// add by itas109
void CCommMFCDlg::onReadEvent(const char *portName, unsigned int readBufferLen)
{if(readBufferLen 0){char data[1024];int recLen m_serialPort.readData(data,readBufferLen 1023 ? 1023 : readBufferLen);if (recLen 0){data[recLen] \0;CString cstr;cstr.Format(_T(OnReceive - data: %s, size: %d), CString(data), recLen);MessageBox(LPCTSTR(cstr));}}
}
// end by itas1096. 结果
代码中的COM2对应的串口为RS232环回测试硬件因此对应的结果为程序启动后初始化并打开串口COM1发送数据itas09,随后弹框提示收到数据(如OnReceive - data: itas109, size: 7) License
License under CC BY-NC-ND 4.0: 署名-非商业使用-禁止演绎 Reference:
https://github.com/itas109/CSerialPorthttps://gitee.com/itas109/CSerialPorthttps://blog.csdn.net/itas109