# CSerialPort
a lightweight library of serial port, which can easy to read and write serical port on windows with C++
# License
since V3.0.0.171216 use LGPL v3 License
---
Blog : http://blog.csdn.net/itas109
QQ Group : [129518033](http://shang.qq.com/wpa/qunwpa?idkey=2888fa15c4513e6bfb9347052f36e437d919b2377161862948b2a49576679fc6)
# Last Modify
## Version: 3.0.3.180621
by itas109 on 2018-06-21
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
---
## 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
---
没有合适的资源?快使用搜索试试~ 我知道了~
经典三方串口类SerialPort
共78个文件
h:20个
cpp:14个
txt:7个
需积分: 27 71 下载量 132 浏览量
2018-07-17
16:17:08
上传
评论 3
收藏 504KB ZIP 举报
温馨提示
经典三方串口类,内容丰富,方便二次开发调用。 通信程序在CreateFile处指定串口设备及相关的操作属性,再返回一个句柄, 该句柄将被用于后续的通信操作,并贯穿整个通信过程串口打开后,其属性 被设置为默认值,根据具体需要,通过调用GetCommState(hComm,&&dcb;)读取 当前串口设备控制块DCB设置,修改后通过SetCommState(hComm,&&dcb;)将其写 入。
资源推荐
资源详情
资源评论
收起资源包目录
经典串口类CSerialPort.zip (78个子文件)
CSerialPort-master
Demo
CSerialPortDemoWin32Console
CSerialPortDemoWin32Console
stdafx.cpp 225B
stdafx.h 219B
targetver.h 228B
CSerialPortDemoWin32Console.vcxproj.filters 2KB
CSerialPortDemoWin32Console.vcxproj 5KB
ReadMe.txt 2KB
CSerialPortDemoWin32Console.cpp 1KB
CSerialPortDemoWin32Console.sln 1005B
CommDLL
Release
Comm.exe 98KB
Comm
stdafx.cpp 129B
Comm.vcxproj.user 162B
res
Comm.rc2 664B
Comm.ico 66KB
stdafx.h 1KB
Comm.vcxproj.filters 2KB
targetver.h 226B
CommDlg.cpp 7KB
Comm.rc 12KB
Comm.aps 90KB
resource.h 2KB
Comm.vcxproj 6KB
Comm.h 400B
CommDlg.h 985B
Comm.cpp 2KB
ReadMe.txt 4KB
Comm.sln 936B
Comm
Release
Comm.exe 110KB
Comm
stdafx.cpp 129B
Comm.vcxproj.user 162B
res
Comm.rc2 664B
Comm.ico 66KB
stdafx.h 1KB
Comm.vcxproj.filters 2KB
targetver.h 226B
CommDlg.cpp 7KB
Comm.rc 12KB
Comm.aps 90KB
resource.h 2KB
Comm.vcxproj 6KB
Comm.h 400B
CommDlg.h 1KB
Comm.cpp 2KB
ReadMe.txt 4KB
Comm.sln 936B
Comm.v12.suo 195KB
CSerialPortDemoWin32
Release
CSerialPortDemoWin32.exe 32KB
CSerialPortDemoWin32.sln 984B
CSerialPortDemoWin32
CSerialPortDemoWin32.rc 7KB
stdafx.cpp 218B
small.ico 3KB
Resource.h 741B
CSerialPortDemoWin32.vcxproj 5KB
stdafx.h 401B
targetver.h 228B
CSerialPortDemoWin32.cpp 5KB
CSerialPortDemoWin32.ico 3KB
CSerialPortDemoWin32.vcxproj.filters 2KB
CSerialPortDemoWin32.h 88B
ReadMe.txt 2KB
LICENSE 7KB
src
sigslot.h 12KB
SerialPort.cpp 39KB
SerialPort.h 12KB
vsprojects
Windows
VC12
libcserialport
libcserialport
stdafx.cpp 212B
libcserialport.vcxproj 5KB
libcserialport.cpp 80B
stdafx.h 304B
dllmain.cpp 370B
targetver.h 228B
libcserialport.vcxproj.filters 2KB
ReadMe.txt 2KB
libcserialport.sln 966B
VERSION 13B
doc
How To Use.txt 817B
suspending.txt 519B
.gitignore 2KB
README.md 7KB
.gitattributes 2KB
共 78 条
- 1
资源评论
龙传
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功