C++跨平台串口通信类库CSerialPort 2023-02-15
如需转载请标明出处:http://blog.csdn.net/itas109
相关:
windows串口编程
linux串口编程
串口类库
串口第三方库
serial port communication class
Serial Programming Guide
windows串口编程C/C++
串口通信类库
C++跨平台串口通信类库
CSerialPort
前言
CSerialPort是一份优秀的串口类文件,好多的地方值得我们学习,具体在多线程,事件,自定义消息,类的封装方面等等。 Remon提供的串口类网址为: https://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2483/A-communication-class-for-serial-port.htm, 由于已经运行十几年了,原文的问答部分列出来这么多年来的问题,经过网友们的总结,补充和修改原来代码后,整理出一份相对比较完美的代码。
但是,CSerialPort只适应于Windows平台,我们希望有一个轻量级、跨平台、高效的串口通信类库。因此,这里将设计一个以CSerialPort命名的跨平台类库。
为什么会再写一篇文章,因为CSDN的评论系统有问题,博文CSerialPort串口类最新修正版2018-06-21无法显示评论。多次给官方反映没有结果。
C++串口通信相关
1. 教程
CSerialPort教程v4.3.x(QT MFC cmake vcpkg等)
https://blog.csdn.net/itas109/category_12416341.html
CSerialPort教程4.2.1【旧版】
https://blog.csdn.net/itas109/category_11520540.html
串口编程
https://blog.csdn.net/itas109/category_10770788.html
3. 串口类库CSerialPort
基于C++的轻量级开源跨平台串口类库,可以轻松实现跨平台多操作系统的串口读写
跨平台版本4.X【推荐】:
https://github.com/itas109/CSerialPort
https://gitee.com/itas109/CSerialPort
windows版本3.0.3:
https://github.com/itas109/CSerialPort/tree/CSerialPort_win_3.0.3
3. 开源文本串口助手CommLite【已上架UOS应用商店】
https://github.com/itas109/CommLite
https://gitee.com/itas109/CommLite
4. 通用串口调试助手CommMaster【已上架UOS应用商店】
https://gitee.com/itas109/CommMaster
CSerialPort
CSerialPort is a lightweight cross-platform serial port library based on C/C++, which can easy to read and write serial port on multiple operating system. Also support C#, Java, Python, Node.js etc.
CSerialPort是一个基于C/C++的轻量级开源跨平台串口类库,可以轻松实现跨平台多操作系统的串口读写,同时还支持C#, Java, Python, Node.js等。
Design Principles 设计原则
- Cross-platform 跨平台
- Easy use 简单易用
- higher efficiency 高效
Platform 平台
CSerialPort已经在以下平台做过测试:
- DOS ( x86_64 )
- Windows ( x86_64 )
- Linux ( x86_64, aarch64, mips64el, s390x, ppc64le )
- macOS ( x86_64 )
- Raspberry Pi ( armv7l )
- FreeBSD ( x86_64 )
- …
CSerialPort类地址:
https://github.com/itas109/CSerialPort
https://gitee.com/itas109/CSerialPort
Last Modify
跨平台版本
Version: 4.3.0.230215
by itas109 on 2023-02-15
windows稳定版
CSDN下载地址:
历史下载:
V3.0.3.180621
V3.0.2.180615
Screenshot 截图
CommMaster通信大师
- 支持windows/linux/macos/raspberrypi等等
- 支持自定义串口名称
- 支持自定义波特率
- 支持自定义语言
- 支持自定义主题
https://gitee.com/itas109/CommMaster
CommLite
CommLite是一款基于CSerialPort的文本UI串口调试助手
- 支持x86, arm, mips等cpu架构
- 支持windows dos, linux, macos, raspberrypi, freebsd等操作系统
https://github.com/itas109/CommLite
https://gitee.com/itas109/CommLite
Gui 图形用户界面
示例路径: CSerialPort/examples/CommQT
Tui 终端用户界面
示例路径: CSerialPort/examples/CommTui
No Gui 无界面
示例路径: CSerialPort/examples/CommNoGui
directory List
update : 2023-08-20
CSerialPort # root
+--- .clang-format # code format 代码规范
├── bindings # 第三方语言接口
│ ├── c # c interface c接口
│ ├── csharp # csharp interface c#接口
│ ├── java # java interface java接口
│ ├── javascript # javascript interface javascript接口
│ └── python # python interface python接口
├── CHANGELOG.md # change log 修改日志
├── cmake # cross compile 交叉编译
├── CMakeLists.txt
├── doc # document 文档
├── examples # example 示例程序
│ ├── CommMFC # CSerialPort MFC Demo MFC程序示例
│ ├── CommNoGui # CSerialPort No Gui Demo 无界面程序示例
│ ├── CommQT # CSerialPort QT Demo QT程序示例
│ ├── CommTui # CSerialPort tui Demo 文本界面程序示例
│ ├── CommWXWidgets # CSerialPort wxwidgets Demo wxwidgets界面程序示例
├── include # headers 头文件
│ └── CSerialPort
│ ├── ibuffer.hpp # lightweight cross-platform buffer library 轻量级跨平台缓冲区类
│ ├── ithread.hpp # lightweight cross-platform thread library 轻量级跨平台线程类
│ ├── itimer.hpp # lightweight cross-platform timer library 轻量级跨平台定时器类
│ ├── iutils.hpp # lightweight cross-platform utils library 轻量级跨平台工具类
│ ├── SerialPortBase.h # CSerialPort Base class 串口基类
│ ├── SerialPort_global.h # Global difine of CSerialPort 串口全局定义
│ ├── SerialPort.h # lightweight cross-platform serial port library 轻量级跨平台串口类库
│ ├── SerialPortInfoBase.h # CSerialPortInfo Base class 串口信息辅助类基类
│ ├── SerialPortInfo.h # CSerialPortInfo class 串口信息辅助类
│ ├── SerialPortInfoUnixBase.h # CSerialPortInfo unix class unix串口信息辅助类基类
│ ├── SerialPortInfoWinBase.h # CSerialPortInfo windows class windows串口信息辅助类基类
│ ├── SerialPortListener.h # CSerialPortListener interface class 串口事件监听接口类
│ ├── SerialPortUnixBase.h # CSerialPort unix Base class unix串口基类
│ ├── SerialPort_version.h # CSerialPort version 版本定义
│ └── SerialPortWinBase.h # CSerialPort Windows Base class windows串口基类
├── lib # lib 库目录
├── LICENSE # LGPL3.0 license
├── pic # picture 图片
├── README.md
├── README_zh_CN.md
├── src # source 源代码
├── test # unit test 单元测试
└── VERSION # version 版本号
CSerialPort v4 修改日志
## v4.3.0 (2023-02-15)
Feature:
* readEvent replace with CSerialPortListener 使用CSerialPortListener进行读取事件通知
* replace std::string with const char* as much as possible 尽可能使用const char*代替std::string
* add function getReadBufferUsedLen() to get used buffer length 增加getReadBufferUsedLen函数用于获取缓冲区长度
* add LOG_INFO to output CSerialPort info 增加LOG_INFO输出串口信息
## v4.2.2 (2023-02-15)
Feature:
* read event notify with portName and readBufferLen 读取事件通知支持串口名称与长度
* add function getReadBufferUsedLen() to get used buffer length 增加getReadBufferUsedLen函数用于获取缓冲区长度
* add LOG_INFO to output CSerialPort info 增加LOG_INFO输出串口信息
## v4.2.1 (2022-11-07)
Feature:
* read buffer size deafult 4096 Bytes 读取缓冲区大小默认为4096字节
* read interval timeout default 0ms 读取超时间隔默认0ms,即实时接收
* vcpkg install CSerialPort 支持vcpkg安装CSerialPort
* wxWidgets demo 新增wxWidgets示例程序
Experimental:
* new notify class CSerialPortListener(USE_CSERIALPORT_LISTENER) 新的事件通知类CSerialPortListener(宏定义USE_CSERIALPORT_LISTENER开启)
* CSerialPort for C#(CSharp) 支持C#(CSharp)调用CSerialPort
* CSerialPort for Java 支持Java调用CSerialPort
* CSerialPort for Python 支持Python调用CSerialPort
* CSerialPort for JavaScript 支持JavaScript调用CSerialPort
## v4.2.0 (2022-10-01)
Fixed:
* #60 application compiled with source code under windows still has export information 在windows下以源码方式编译仍然带有导出信息
Feature:
* modify examples 优化示例程序
* add cross compile toochain.cmake for arm aarch64 mips64el riscv 增加arm aarch64 mips64el riscv的交叉编译cmake 文件
* improve windows receive performance 优化windows接收性能
* add custom baud rate for linux 新增linux下设置自定义波特率
* add cross-platform thread library 新增跨平台线程库
* add cross-platform ringbuffer library 新增跨平台环形缓冲区库
* add cross-platform timer based condition variable 新增基于条件变量的跨平台定时器库
* add ringbuffer for receive data(asynchronous mode) 新增环形缓冲区接收数据(异步模式)
* add read interval timeout setting(default 50ms, asynchronous mode) 新增读取超时设置(默认50ms, 异步模式)
* add clang-tidy 增加clang-tidy代码检测
## v4.1.1 (2021-09-03)
Fixed:
* #49 function writeData hanle leak on windows 修复windows下writeData函数句柄泄漏问题
* #41 could not enum all avaiable ports on windows 修复windows下偶尔枚举可用串口不全的问题
* #42 high cpu usage problem on unix 修复unix上高cpu占用问题
* #33 No Gui Application without endless loop crash problem 修复NoGui程序崩溃问题
* #28 VS2015 x64 MFC not work 修复VS2015生成x64程序不能正常运行问题
Feature:
* use unsigned int instead of int64 使用unsigned int代替int64
* add unix virtual serial port 增加unix虚拟串口工具
* read thread optimization 读取线程优化
## 4.1.0 (2020-10-10)
Fixed:
* #29 windows xp unable to locate the program input point in msvcrt.dll 无法定位程序输入点于msvcrt.dll
* #30 _T() cannot convert 'const char*' to 'LPCWSTR
* #39 fix getPortInfoList crash on unix(not linux and mac os) 修复unix系统(非linux和macos)getPortInfoList引起的崩溃问题
* #40 fix vs2008 vs2010 Cannot open include file: 'ntddser.h' 修复msvc无法找到ntddser.h问题
Feature:
* header files is separated into include directory 头文件独立到include文件夹
* add Tui Demo based pdcurses and ncurses 增加基于pdcurses和ncurses的tui示例
* use cmake compile CSerialPort 使用cmake编译
* add cmake install 增加cmake安装
* add cppcheck file 增加cppcheck代码检测文件
* add clang-format 增加clang-format代码格式化
* add travis ci and appveyor ci 增加travis和appveyor持续集成
Remove:
* remove function init of integer port 移除init整型串口函数
* remove function availablePorts and availableFriendlyPorts 移除availablePorts和availablePorts函数
## 4.0.3 (2020-04-29)
Fixed:
* fixed memory leak 修复内存泄露问题
* Optimize function closePort under windows 优化windows下的closePort函数
* #21 typo setFlowControl
* #22 function CSerialPortWinBase::openPort error when set error parameter
* #24 sigslot can not define static
* #26 linux receive miss 0x11 0x13 0x0d
* fixed compile error when baudrate not difine 修复波特率未定义错误
Feature:
* support mingw 支持mingw 4.8.2
* support mac 支持mac 10.13
* add Common baud rate 增加波特率
* add test case by gtest 增加测试用例
* add function CSerialPortInfo::availablePortInfos 增加通用串口信息枚举函数
* support linux list ports add /dev/pts/* 支持linux虚拟串口
---
## 4.0.2 (2020-01-08)
基础稳定版本
base and stable version
---
## 4.0.1 beta (2019-07-30)
测试第一版
first beta version
CSerialPort v3及以前 修改日志
## First Version by Remon Spekreijse on 2000-02-08
http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2483/A-communication-class-for-serial-port.htm
## Second Version by mrlong on 2007-12-25
https://code.google.com/p/mycom/
* 增加 ClosePort
* 增加 WriteToPort 两个方法
* 增加 SendData 与 RecvData 方法
## by liquanhai on 2011-11-04
http://blog.csdn.net/liquanhai/article/details/4955253
* 增加 ClosePort 中交出控制权,防止死锁问题
## by liquanhai on 2011-11-06
http://blog.csdn.net/liquanhai/article/details/6941574
* 增加 ReceiveChar 中防止线程死锁
## by viruscamp on 2013-12-04
https://github.com/viruscamp/CSerialPort
* 增加 IsOpen 判断是否打开
* 修正 InitPort 中 parity Odd Even 参数取值错误
* 修改 InitPort 中 portnr 取值范围,portnr>9 时特殊处理
* 取消对 MFC 的依赖,使用 HWND 替代 CWnd,使用 win32 thread 函数而不是 MFC 的
* 增加用户消息编号自定义,方法来自 CnComm
## by itas109 on 2014-01-10
http://blog.csdn.net/itas109/article/details/18358297
https://github.com/itas109
* 解决COM10以上端口无法显示的问题
* 扩展可选择端口,最大值MaxSerialPortNum可以自定义
* 添加QueryKey()和Hkey2ComboBox两个方法,用于自动查询当前有效的串口号。
##by liquanhai on 2014-12-18
* 增加一些处理措施,主要是对减少CPU占用率
## by itas109 on 2016-05-07
http://blog.csdn.net/itas109
https://github.com/itas109
* 修复每次打开串口发送一次,当串口无应答时,需要关闭再打开或者接收完数据才能发送的问题。
解决办法:在m_hEventArray中调整m_hWriteEvent的优先级高于读的优先级。CommThread(LPVOID pParam)函数中读写的位置也调换。
参考:
http://zhidao.baidu.com/link?url=RSrbPcfTZRULFFd2ziHZPBwnoXv1iCSu_Nmycb_yEw1mklT8gkoNZAkWpl3UDhk8L35DtRPo5VV5kEGpOx-Gea
* 修复停止位在头文件中定义成1导致SetCommState报错的问题,应为1对应的停止位是1.5。UINT stopsbits = ONESTOPBIT
* switch(stopbits)和switch(parity)增加默认情况,增强程序健壮性
##by itas109 on 2016-06-22
http://blog.csdn.net/itas109
https://github.com/itas109
* 增加ReceiveStr方法,用于接收字符串(接收缓冲区有多少字符就接收多少字符)。
解决ReceiveChar只能接收单个字符的问题。
## by itas109 on 2016-06-29
http://blog.csdn.net/itas109
https://github.com/itas109
* 解决RestartMonitoring方法和StopMonitoring方法命令不准确引起的歧义,根据实际作用。
将RestartMonitoring更改为ResumeMonitoring,将StopMonitoring更改为SuspendMonitoring。
* 增加IsThreadSuspend方法,用于判断线程是否挂起。
* 改进ClosePort方法,增加线程挂起判断,解决由于线程挂起导致串口关闭死锁的问题。
* 增加IsReceiveString宏定义,用于接收时采用单字节接收还是多字节接收
## by itas109 on 2016-08-02
http://blog.csdn.net/itas109
https://github.com/itas109
* 改进IsOpen方法,m_hComm增加INVALID_HANDLE_VALUE的情况,因为CreateFile方法失败返回的是INVALID_HANDLE_VALUE,不是NULL
* 改进ClosePort方法:增加串口句柄无效的判断(防止关闭死锁);m_hWriteEvent不使用CloseHandle关闭
* 改进CommThread、ReceiveChar、ReceiveStr和WriteChar方法中异常处理的判断,增加三种判断:串口打开失败(error code:ERROR_INVALID_HANDLE)、连接过程中非法断开(error code:ERROR_BAD_COMMAND)和拒绝访问(error code:ERROR_ACCESS_DENIED)
* 采用安全函数sprintf_s和strcpy_s函数替换掉sprintf和strcpy
* 改进QueryKey方法,用于查询注册表的可用串口值,可以搜索到任意的可用串口
* 改进InitPort方法,串口打开失败,增加提示信息:串口不存在(error code:ERROR_FILE_NOT_FOUND)和串口拒绝访问(error code:ERROR_ACCESS_DENIED)
* 加入viruscamp 取消对 MFC 的依赖
* 改进InitPort方法,如果上次串口是打开,再次调用InitPort方法,关闭串口需要做一定的延时,否则有几率导致ERROR_ACCESS_DENIED拒绝访问,也就是串口占用问题
* 初始化默认波特率修改为9600
* 修复一些释放的BUG
* 规范了一些错误信息,参考winerror.h -- error code definitions for the Win32 API functions
* 删除SendData和RecvData方法
## by itas109 on 2016-08-10
http://blog.csdn.net/itas109
https://github.com/itas109
* 改进ReceiveStr方法,comstat.cbInQue = 0xcccccccc的情况(如串口异常断开),会导致RXBuff初始化失败
## by itas109 on 2017-02-14
http://blog.csdn.net/itas109
https://github.com/itas109
* 兼容ASCII和UNICODE编码
* ReceiveStr函数中发送函数SendMessage的第二个参数采用结构体形式,包括portNr串口号和bytesRead读取的字节数,可以处理16进制的时候0x00截断问题
* 精简不必要的函数SendData和RecvData
* 尽量的取消对 MFC 的依赖,Hkey2ComboBox函数暂时保留
* 其他小问题修改
## by itas109 on 2017-03-12
http://blog.csdn.net/itas109
https://github.com/itas109
* 增加宏定义_AFX,用于处理MFC的必要函数Hkey2ComboBox
* 进一步去除MFC依赖,修改AfxMessageBox函数
## by itas109 on 2017-12-16
**Version:3.0.0.171216**
http://blog.csdn.net/itas109
https://github.com/itas109
* 支持DLL输出
* 去除QueryKey和Hkey2ComboBox,采用CSerialPortInfo::availablePorts()函数代替
* 增加CSerialPortInfo类,目前只有availablePorts静态函数,用于获取活跃的串口到list
* 增加命名空间itas109
* 精简不必要的头文件
* InitPort和~CSerialPort()中直接整合ClosePort()
## by itas109 on 2018-02-14
**Version:3.0.1.180214**
http://blog.csdn.net/itas109
https://github.com/itas109
* ★修复不能连续发送的问题 ★ fix can not continue send error
* ★一次性写入尽可能多的数据到串口 ★ try best to send mutil data once in WriteChar funtion
* 修复BYTE内存设置的问题 fix BYTE memset error
* 在构造函数中初始化和释放临界区 initialize and delete critical section in Constructor
* 精简代码
## by itas109 on 2018-06-15
**Version:3.0.2.180615**
http://blog.csdn.net/itas109
https://github.com/itas109
* 修复availablePorts函数不能枚举所有串口问题 fix function availablePorts can not enum all port error
## by itas109 on 2018-06-21
**Version:3.0.3.180621**
http://blog.csdn.net/itas109
https://github.com/itas109
* 增加信号与槽机制传输数据 add sigslot.h to send data
* 修复handle初始化问题,全部初始化为INVALID_HANDLE_VALUE modify handle init INVALID_HANDLE_VALUE
* 修复串口打开失败,再次成功打开串口后,不能正常读写问题 fix can not read and write when the port open failed
License
since V3.0.0.171216 use LGPL v3 License
自 V3.0.0.171216版本后采用LGPL v3协议
如需转载请标明出处:http://blog.csdn.net/itas109
License