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: 署名-非商業使用-禁止演繹

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