C++跨平台串口通信类库2020-10-10

1,215 阅读9分钟

C++跨平台串口通信类库2020-10-10


如需转载请标明出处
QQ技术交流群:129518033

相关:
windows串口编程
linux串口编程
串口类库
串口第三方库
serial port communication class
Serial Programming Guide
windows串口编程C/C++
串口通信类库
C++跨平台串口通信类库


前言

CSerialPort是一份优秀的串口类文件,好多的地方值得我们学习,具体在多线程,事件,自定义消息,类的封装方面等等。 Remon提供的串口类网址为: www.codeguru.com/cpp/i-n/net… 由于已经运行十几年了,原文的问答部分列出来这么多年来的问题,经过网友们的总结,补充和修改原来代码后,整理出一份相对比较完美的代码。

但是,CSerialPort只适应于Windows平台,我们希望有一个轻量级、跨平台、高效的串口通信类库。因此,这里将设计一个以CSerialPort命名的跨平台类库。

本文最新修改时间:2020-10-10


CSerialPort

Version Stars Forks License language platform appveyor-ci travis-ci GitHub last commit

Lightweight cross-platform serial port library based on C++, which can easy to read and write serial port on mutiple operating system

基于C++的轻量级开源跨平台串口类库,可以轻松实现跨平台多操作系统的串口读写

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类地址:

github.com/itas109/CSe…

gitee.com/itas109/CSe…

Last Modify

跨平台版本

Version: 4.1.0.201010
by itas109 on 2020-10-10

下载地址:
github.com/itas109/CSe…
gitee.com/itas109/CSe…

历史下载
v4.0.3
v4.0.2

windows稳定版

CSerialPort_win_3.0.3

CSDN下载地址:

历史下载:
V3.0.3.180621
V3.0.2.180615


Screenshot 截图

CommMaster通信大师

  • 支持windows/linux/macos/raspberrypi等等
  • 支持自定义串口名称
  • 支持自定义波特率
  • 支持自定义语言
  • 支持自定义主题

gitee.com/itas109/Com…
在这里插入图片描述

CommLite

CommLite是一款基于CSerialPort的文本UI串口调试助手

  • 支持x86, arm, mips等cpu架构
  • 支持windows dos, linux, macos, raspberrypi, freebsd等操作系统

github.com/itas109/Com…
gitee.com/itas109/Com…
在这里插入图片描述

Gui 图形用户界面

示例路径: CSerialPort/examples/CommQT
在这里插入图片描述

Tui 终端用户界面

示例路径: CSerialPort/examples/CommTui
在这里插入图片描述

No Gui 无界面

示例路径: CSerialPort/examples/CommNoGui
在这里插入图片描述

directory List

update : 2020-10-05

|-- CSerialPort # root
    |-- .clang-format # code format 代码规范
    |-- CHANGELOG.md # change log 修改日志
    |-- CMakeLists.txt
    |-- LICENSE # LGPL3.0 license
    |-- README.md
    |-- README-EN.md
    |-- VERSION # version 版本号
    |-- cserialport.cppcheck
    |-- doc # document 文档目录
    |   |-- CSerialPort_doc_cn.chm # Chinese documnet 简体中文说明书
    |   |-- CSerialPort_doc_en.chm # English documnet 英文说明书
    |   |-- directory_list.md # directory list 目录列表
    |   |-- FAQ.md # Frequently Asked Questions 常见问题回答
    |   |-- error_guide.md # error guide 错误指南文档
    |   |-- How To Use.txt
    |   |-- suspending.txt
    |-- examples # example 示例目录
    |   |-- CommMFC # CSerialPort MFC Demo use source code win32直接调用源码MFC程序示例
    |   |-- CommDLL # CSerialPort MFC Demo use Win32 Dll 
    |   |-- CommNoGui # CSerialPort No Gui Demo 无界面程序示例
    |   |-- CommQT # CSerialPort QT Demo QT程序示例
    |   |-- CommTui # CSerialPort tui Demo 文本界面程序示例
    |-- include # headers 头文件
    |   |-- CSerialPort
    |       |-- Doxyfile # Doxyfile Doxy文档配置文件
    |       |-- SerialPort.h # Lightweight library of serial port, which can easy to read and write serical port on windows and linux with C++ 轻量级跨平台串口读写类库
    |       |-- SerialPortBase.h # CSerialPort Base class 串口基类 
    |       |-- SerialPortInfo.h # CSerialPortInfo class 串口信息辅助类 
    |       |-- SerialPortInfoBase.h # CSerialPortInfo Base class 串口信息辅助类基类 
    |       |-- SerialPortInfoUnixBase.h # CSerialPortInfo unix class unix串口信息辅助类基类 
    |       |-- SerialPortInfoWinBase.h # CSerialPortInfo windows class windows串口信息辅助类基类
    |       |-- SerialPortUnixBase.h # CSerialPort unix Base class unix串口基类
    |       |-- SerialPortWinBase.h # CSerialPort Windows Base class windows串口基类 
    |       |-- SerialPort_global.h # Global difine of CSerialPort 串口全局定义 
    |       |-- osplatformutil.h # os platform define 操作系统定义
    |       |-- sigslot.h # signal and slot 信号与槽
    |-- lib # lib 库目录
    |   |-- CMakeLists.txt # CSerialPort library cmake file [recommend]
    |   |-- Linux # windows lib windows库目录
    |   |-- Windows # windows lib windows库目录
    |-- pic # picture 图片
    |-- src # source 源代码
    |-- test # unit test 单元测试

CSerialPort v4 修改日志

## 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协议


觉得文章对你有帮助,可以扫描二维码捐赠给博主,谢谢!
在这里插入图片描述
如需转载请标明出处
QQ技术交流群:129518033


License

License under CC BY-NC-ND 4.0: 署名-非商业使用-禁止演绎