C++跨平台串口通信类库CSerialPort 2022-11-07


如需转载请标明出处: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教程(QT MFC等)
https://blog.csdn.net/itas109/category_11520540.html
串口编程
https://blog.csdn.net/itas109/category_10770788.html2. 串口类库CSerialPort
基于C++的轻量级开源跨平台串口类库,可以轻松实现跨平台多操作系统的串口读写跨平台版本4.X【推荐】:
https://github.com/itas109/CSerialPort
https://gitee.com/itas109/CSerialPortwindows版本3.0.3:
https://github.com/itas109/CSerialPort/tree/CSerialPort_win_3.0.33. 开源文本串口助手CommLite【已上架UOS应用商店】
https://github.com/itas109/CommLite
https://gitee.com/itas109/CommLite4. 通用串口调试助手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.2.1.221107
by itas109 on 2022-11-07

下载地址:
https://github.com/itas109/CSerialPort/releases/tag/v4.2.1
https://gitee.com/itas109/CSerialPort/tree/v4.2.1

windows稳定版

CSerialPort_win_3.0.3

CSDN下载地址:

历史下载:
V3.0.3.180621
V3.0.2.180615


Screenshot 截图

CommMaster通信大师

https://gitee.com/itas109/CommMaster

CommLite

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

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 : 2022-10-01

CSerialPort # root
+--- .clang-format # code format 代码规范
+--- CHANGELOG.md # change log 修改日志
+--- cmake # cross compile toolchain 交叉编译文件
|   +--- toolchain_aarch64.cmake
|   +--- toolchain_arm.cmake
|   +--- toolchain_mips64el.cmake
|   +--- toolchain_riscv64.cmake
+--- CMakeLists.txt
+--- LICENSE # LGPL3.0 license
+--- pic # picture 图片
+--- README-EN.md
+--- README.md
+--- VERSION # version 版本号
+--- cserialport-config.cmake.in
+--- cserialport.cppcheck
+--- doc # document 文档目录
|   +--- CSerialPort_doc_cn.chm # Chinese documnet 简体中文说明书
|   +--- CSerialPort_doc_en.chm # English documnet 英文说明书
|   +--- directory_list.md # directory list 目录列表
|   +--- Doxyfile.in # Doxyfile Doxy文档配置文件
|   +--- error_guide.md # error guide 错误指南文档
|   +--- FAQ.md # Frequently Asked Questions 常见问题回答
|   +--- How To Use.txt
|   +--- suspending.txt
+--- examples # example 示例目录
|   +--- CommMFC # CSerialPort MFC Demo use source code windows直接调用源码MFC程序示例
|   +--- CommMFCDLL # CSerialPort MFC Demo use dll windows调用动态库MFC程序示例
|   +--- CommNoGui # CSerialPort No Gui Demo 无界面程序示例
|   +--- CommNoGuiTimeout # CSerialPort No Gui Timeout Demo 无界面读取超时程序示例
|   +--- CommQT # CSerialPort QT Demo QT程序示例
|   +--- CommTui # CSerialPort tui Demo 文本界面程序示例
+--- include # headers 头文件
|   +--- CSerialPort
|   |   +--- ibuffer.hpp # lightweight cross-platform buffer library 轻量级跨平台缓冲区类
|   |   +--- ithread.hpp # lightweight cross-platform thread library 轻量级跨平台线程类
|   |   +--- itimer.hpp # lightweight cross-platform timer library 轻量级跨平台定时器类
|   |   +--- osplatformutil.h # os platform define 操作系统定义
|   |   +--- SerialPort.h # lightweight cross-platform serial port library 轻量级跨平台串口类库
|   |   +--- 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 串口全局定义
|   |   +--- SerialPort_version.h # CSerialPort version 版本定义
|   |   +--- sigslot.h # signal and slot 信号与槽
+--- lib # lib 库目录
|   +--- CMakeLists.txt # CSerialPort library cmake file [recommend]
|   +--- Linux # linux lib linux库目录
|   +--- Windows # windows lib windows库目录
+--- src # source 源代码
+--- test # unit test 单元测试

CSerialPort v4 修改日志

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

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

C++跨平台串口通信类库CSerialPort 2022-11-07相关推荐

  1. 微信被拉黑强制发验证消息(2022/11/07验证有效)(非广告)

    微信被拉黑强制发验证消息(2022/11/07验证有效) 方法很简单,如果保留了聊天记录的话,先发一个红包(必须是红包),金额随意,然后发送之后会提示不是好友对方无法收到,然后在这段话后面有一个 &q ...

  2. Jeston AGX Xavier配置ROS+Miniforge+pyTorch-GPU+TorchVision+pcl+Opencv3.2+Ceres(2022.11.07)

    前言 本篇博客由于学校有项目要求,必须用ARM架构的平台进行算法开发,要对其深度学习环境配置.但是由于Xavier平台是极为特殊的,在CSDN上研究(摸)了好久参考了巨多博客才最终完成安装.本博客仅适 ...

  3. 千锋教育2218期2022.11.07

    泛型和项目 1. 泛型 1.1 泛型体验卡 补充知识点 方法的重载 方法的重载 reload 要求在同一个类内, 或者同一个接口内 要求方法名必须一致 要求方法形式参数类型必须不一致,调用方法的过程中 ...

  4. 单片机:11.UART串口通信

    原文地址:https://blog.csdn.net/Qingzhusshuiyun/article/details/78236798 通信按照传统的理解就是信息的传输与交换.对于单片机来说,通信则与 ...

  5. 51单片机之串口通信

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.串口介绍 二.电路 三.寄存器 四.串口通信操作流程 前言 2022.9.14的学习记录.` 一.串口介绍 串口是 ...

  6. 在C#中使用SerialPort类实现串口通信 遇到多线程问题

    在C#中使用SerialPort类实现串口通信 遇到多线程问题 在C#中使用SerialPort类实现串口通信. 2009年11月01日 星期日 10:03 在.NET work 2.0中提供了Ser ...

  7. 51单片机串口通信模板_51单片机串口通信(字符串接收和发送)(示例代码)

    1 #include 2 3 //------------------串口通信协议-----------------//4 /* 5 客户端数据包格式解释(长度恒为15):6 例如:A01_fmq_0 ...

  8. 51单片机学习笔记-6串口通信

    6 串口通信 [toc] 注:笔记主要参考B站江科大自化协教学视频"51单片机入门教程-2020版 程序全程纯手打 从零开始入门". 注:工程及代码文件放在了本人的Github仓库 ...

  9. 串口通信--CSerialport类

    参考链接: Afx全局函数及MFC常见数据类型 CSerialport类开源地址(国人在github上托管的) CSerialPort串口类最新修正版及源码简单分析 Remon提供的串口类网址(英文) ...

  10. VS_MFC:CSerialPort串口通信

    参考链接:  Afx全局函数及MFC常见数据类型  CSerialport类开源地址(国人在github上托管的)  CSerialPort串口类最新修正版及源码简单分析  Remon提供的串口类网址 ...

最新文章

  1. java类加载的表现形式
  2. Django web框架
  3. 【Java开发】生成二维码
  4. python爬取新浪新闻首页_学习了《python网络爬虫实战》第一个爬虫,爬取新浪新闻...
  5. python删除首行_Python删除文件第一行
  6. Hibernate XXX.hbm.xml 里的class标签的 schema 属性解释
  7. Python[装饰器]
  8. python水仙花数的编程代码-Python 求“水仙花数”的方法和具体代码
  9. 【F2FS LFS SSR】华为如何打造智能终端的有趣灵魂?(上)
  10. 易语言自定义数据类型转c,转换JSON结构为易语言代码自定义数据类型
  11. idea常用图标总结
  12. 检索汉字拼音,实用工具类
  13. 计算机操作系统知识点总结(有这一篇就够了!!!)
  14. 怎么用ping命令测试网速
  15. 【MySQL必知必会】第十四章 使用子查询
  16. 08-词嵌入(Word embeddings)
  17. python心率检测
  18. 计算机windows无效映像,应用程序或dll 为无效的windows映像怎么办
  19. 阿里云云服务器 修改远程桌面端口
  20. 暑假集训日记(四)(长题解)

热门文章

  1. java netbeans教程_Netbeans下载安装教程教程
  2. autoflowchart软件使用步骤_AutoFlowchart(c语言流程图生成器) V 3.5.3 官方版
  3. fmd单片机c语言教程,ft62f08x_c语言 FMD单片机C语言列子程序 - 下载 - 搜珍网
  4. Linux从入门到放弃(1)教学视频推荐以及一些经验分享
  5. 查看别人IP经典办法
  6. ubb码转换的java类库 ubb2html_ubb代码转换为html
  7. 简明python教程 沈洁元_Python简明教程-沈洁元
  8. python编程入门第九讲,小甲鱼python视频第九讲(课后习题)
  9. python设计模式之工厂模式概述
  10. 数据库常用sql语句总结