CSerialPort跨平台串口类库2020-04-29

CSerialPort跨平台串口类库2020-04-29


如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033

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


前言

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无法显示评论。多次给官方反映没有结果。

本文最新修改时间:2020-04-29


news 最近进展

跨平台串口类库CSerialPort:
目前进展

2019-07-28:
Windows:
1.支持异步和同步方式串口读写
2.精简sigslot代码
Unix & Unix Like:
1.支持异步方式串口读写
2.基本功能完成

2018-11-20
Windows:
1.重新设计多线程,支持读写并发
2.重新设计通知模块,提高读写效率
Unix & Unix Like:
框架搭建完成…

2020-01-08
跨平台基础稳定版v4.0.2发布

新的跨平台类库,将具有极高的扩展性,开发者可以自己设计自己平台的类库

2020-04-29

v4.0.3发布

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虚拟串口

增加Chm说明文档
https://github.com/itas109/CSerialPort/wiki
English Document
https://github.com/itas109/CSerialPort/blob/master/doc/CSerialPort_doc_en.chm
简体中文文档
https://github.com/itas109/CSerialPort/blob/master/doc/CSerialPort_doc_cn.chm


a lightweight library of serial port, which can easy to read and write serical port on windows with C++

一个使用C++实现的轻量级串口类库,可以轻松在windows和linux下进行串口读写

License

since V3.0.0.171216 use LGPL v3 License

自 V3.0.0.171216版本后采用LGPL v3协议


Design Principles 设计原则

  • Cross-platform 跨平台
  • Easy use 简单易用
  • higher efficiency 高效

Todo List

1.通用通信协议封装
2.尽可能的封包完整数据给开发者,实际由开发者控制
3.基于通用通信协议的打包类库
4.跨平台类库的整合与分离,如定时器、多线程等


CSerialPort类地址:

https://github.com/itas109/CSerialPort

https://gitee.com/itas109/CSerialPort

Last Modify

跨平台版本

Version: 4.0.3.200429
by itas109 on 2020-04-29

下载地址:
https://download.csdn.net/download/itas109/12374409
https://github.com/itas109/CSerialPort/releases/tag/v4.0.3
https://gitee.com/itas109/CSerialPort/tree/v4.0.3

历史下载
v4.0.2

windows稳定版

CSerialPort_win_3.0.3

CSDN下载地址:

历史下载:
V3.0.3.180621
V3.0.2.180615


Result 结果

linux:

Gui 界面

Demo Path: CSerialPort/Demo/CommQT
在这里插入图片描述

No Gui 无界面

Demo Path: CSerialPort/Demo/CommNoGui
在这里插入图片描述

windows:

Gui 界面

Demo Path: CSerialPort/Demo/CommQT
在这里插入图片描述

No Gui 无界面

Demo Path : CSerialPort/Demo/CommNoGui
在这里插入图片描述

directory List

update : 2020-04-12

|-- CSerialPort # root
    |-- LICENSE # LGPL3.0 license
    |-- README.md
    |-- README-EN.md
    |-- VERSION # version 版本号
    |-- Demo # example 示例目录
    |   |-- CommMFC # CSerialPort MFC Demo use source code win32直接调用源码MFC程序示例
    |   |-- CommDLL # CSerialPort MFC Demo use Win32 Dll win32动态库MFC程序示例
    |   |-- CommQT # CSerialPort QT win/linux Demo QT win/linux 程序示例
    |   |-- CommNoGui # CSerialPort No Gui win/linux Demo win/linux无界面程序示例
    |-- 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
    |-- lib # lib 库目录
    |   |-- Linux # windows lib windows库目录
    |       |-- compile.sh # sh compile 命令编译
    |       |-- Makefile # Makefile compile Makefile编译
    |   |-- Windows # windows lib windows库目录
    |       |-- VC12 # windows lib for vs2013 windows vs2013库目录
    |           |-- libcserialport
    |               |-- libcserialport
    |   |--  CMakeLists.txt # win/linux library cmake
    |-- pic # picture 图片
    |-- src # source 源代码
        |-- osplatformutil.h # os platform define 操作系统定义
        |-- SerialPort.cpp
        |-- SerialPort.h # Lightweight library of serial port, which can easy to read and write serical port on windows and linux with C++ 轻量级跨平台串口读写类库
        |-- SerialPortBase.cpp
        |-- SerialPortBase.h # CSerialPort Base class 串口基类 
        |-- SerialPortInfo.cpp
        |-- SerialPortInfo.h # CSerialPortInfo class 串口信息辅助类 
        |-- SerialPortInfoBase.cpp
        |-- SerialPortInfoBase.h # CSerialPortInfo Base class 串口信息辅助类基类 
        |-- SerialPortInfoUnixBase.cpp
        |-- SerialPortInfoUnixBase.h # CSerialPortInfo unix class unix串口信息辅助类基类 
        |-- SerialPortInfoWinBase.cpp
        |-- SerialPortInfoWinBase.h # CSerialPortInfo windows class windows串口信息辅助类基类 
        |-- SerialPortUnixBase.cpp
        |-- SerialPortUnixBase.h # CSerialPort unix Base class unix串口基类 
        |-- SerialPortWinBase.cpp
        |-- SerialPortWinBase.h # CSerialPort Windows Base class windows串口基类 
        |-- SerialPort_global.h # Global difine of CSerialPort 串口全局定义 
        |-- sigslot.h # signal and slot 信号与槽

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

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


License

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章