/*
Module : SerialPort.cpp
Purpose: Implementation for an MFC wrapper class for serial ports
Created: PJN / 31-05-1999
History: PJN / 03-06-1999 1. Fixed problem with code using CancelIo which does not exist on 95.
2. Fixed leaks which can occur in sample app when an exception is thrown
PJN / 16-06-1999 1. Fixed a bug whereby CString::ReleaseBuffer was not being called in
CSerialException::GetErrorMessage
PJN / 29-09-1999 1. Fixed a simple copy and paste bug in CSerialPort::SetDTR
PJN / 08-05-2000 1. Fixed an unreferrenced variable in CSerialPort::GetOverlappedResult in VC 6
PJN / 10-12-2000 1. Made class destructor virtual
PJN / 15-01-2001 1. Attach method now also allows you to specify whether the serial port
is being attached to in overlapped mode
2. Removed some ASSERTs which were unnecessary in some of the functions
3. Updated the Read method which uses OVERLAPPED IO to also return the bytes
read. This allows calls to WriteFile with a zeroed overlapped structure (This
is required when dealing with TAPI and serial communications)
4. Now includes copyright message in the source code and documentation.
PJN / 24-03-2001 1. Added a BytesWaiting method
PJN / 04-04-2001 1. Provided an overriden version of BytesWaiting which specifies a timeout
PJN / 23-04-2001 1. Fixed a memory leak in DataWaiting method
PJN / 01-05-2002 1. Fixed a problem in Open method which was failing to initialize the DCB
structure incorrectly, when calling GetState. Thanks to Ben Newson for this fix.
PJN / 29-05-2002 1. Fixed an problem where the GetProcAddress for CancelIO was using the
wrong calling convention
PJN / 07-08-2002 1. Changed the declaration of CSerialPort::WaitEvent to be consistent with the
rest of the methods in CSerialPort which can operate in "OVERLAPPED" mode. A note
about the usage of this: If the method succeeds then the overlapped operation
has completed synchronously and there is no need to do a WaitForSingle/MultipleObjects.
If any other unexpected error occurs then a CSerialException will be thrown. See
the implementation of the CSerialPort::DataWaiting which has been rewritten to use
this new design pattern. Thanks to Serhiy Pavlov for spotting this inconsistency.
PJN / 20-09-2002 1. Addition of an additional ASSERT in the internal _OnCompletion function.
2. Addition of an optional out parameter to the Write method which operates in
overlapped mode. Thanks to Kevin Pinkerton for this addition.
PJN / 10-04-2006 1. Updated copyright details.
2. Addition of a CSERIALPORT_EXT_CLASS and CSERIALPORT_EXT_API macros which makes
the class easier to use in an extension dll.
3. Removed derivation of CSerialPort from CObject as it was not really needed.
4. Fixed a number of level 4 warnings in the sample app.
5. Reworked the overlapped IO methods to expose the LPOVERLAPPED structure to client
code.
6. Updated the documentation to use the same style as the web site.
7. Did a spell check of the HTML documentation.
8. Updated the documentation on possible blocking in Read/Ex function. Thanks to
D Kerrison for reporting this issue.
9. Fixed a minor issue in the sample app when the code is compiled using /Wp64
PJN / 02-06-2006 1. Removed the bOverlapped as a member variable from the class. There was no
real need for this setting, since the SDK functions will perform their own
checking of how overlapped operations should
2. Fixed a bug in GetOverlappedResult where the code incorrectly checking against
the error ERROR_IO_PENDING instead of ERROR_IO_INCOMPLETE. Thanks to Sasho Darmonski
for reporting this bug.
3. Reviewed all TRACE statements for correctness.
PJN / 05-06-2006 1. Fixed an issue with the creation of the internal event object. It was incorrectly
being created as an auto-reset event object instead of a manual reset event object.
Thanks to Sasho Darmonski for reporting this issue.
PJN / 24-06-2006 1. Fixed some typos in the history list. Thanks to Simon Wong for reporting this.
2. Made the class which handles the construction of function pointers at runtime a
member variable of CSerialPort
3. Made AfxThrowSerialPortException part of the CSerialPort class. Thanks to Simon Wong
for reporting this.
4. Removed the unnecessary CSerialException destructor. Thanks to Simon Wong for
reporting this.
5. Fixed a minor error in the TRACE text in CSerialPort::SetDefaultConfig. Again thanks
to Simon Wong for reporting this.
6. Code now uses new C++ style casts rather than old style C casts where necessary.
Again thanks to Simon Wong for reporting this.
7. CSerialException::GetErrorMessage now uses the strsafe functions. This does mean
that the code now requires the Platform SDK if compiled using VC 6.
PJN / 25-06-2006 1. Combined the functionality of the CSerialPortData class into the main CSerialPort class.
2. Renamed AfxThrowSerialPortException to ThrowSerialPortException and made the method
public.
PJN / 05-11-2006 1. Minor update to stdafx.h of sample app to avoid compiler warnings in VC 2005.
2. Reverted the use of the strsafe.h header file. Instead now the code uses the VC 2005
Safe CRT and if this is not available, then we fail back to the standard CRT.
PJN / 25-01-2007 1. Minor update to remove strsafe.h from stdafx.h of the sample app.
2. Updated copyright details.
PJN / 24-12-2007 1. CSerialException::GetErrorMessage now uses the FORMAT_MESSAGE_IGNORE_INSERTS flag. For more information please see Raymond
Chen's blog at http://blogs.msdn.com/oldnewthing/archive/2007/11/28/6564257.aspx. Thanks to Alexey Kuznetsov for reporting this
issue.
2. Simplified the code in CSerialException::GetErrorMessage somewhat.
3. Optimized the CSerialException constructor code.
4. Code now uses newer C++ style casts instead of C style casts.
5. Reviewed and updated all the TRACE logging in the module
6. Replaced all calls to ZeroMemory with memset
PJN / 30-12-2007 1. Updated the sample app to clean compile on VC 2008
2. CSerialException::GetErrorMessage now uses Checked::tcsncpy_s if compiled using VC 2005 or later.
PJN / 18-05-2008 1. Updated copyright details.
2. Changed the actual
C++串口通信类(阻塞和非阻塞都支持)
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
![star](https://csdnimg.cn/release/downloadcmsfe/public/img/star.98a08eaa.png)
在IT领域,串口通信是一种常见且重要的通信方式,尤其在嵌入式系统、设备控制以及数据传输等场景中广泛应用。C++作为一种强大的编程语言,提供了丰富的库和工具来实现串口通信。在这个主题中,我们将深入探讨如何使用C++构建一个支持阻塞和非阻塞模式的串口通信类。 串口通信的基本概念: 串口通信是指通过串行接口进行的数据传输,通常涉及RS-232、RS-485或USB等标准。数据以比特流的形式逐位发送,每发送或接收一个位就进行一次确认,因此速度相对较慢但稳定性高。 阻塞与非阻塞串口通信: 1. 阻塞模式:在阻塞模式下,当执行读写操作时,程序会等待直到数据完全传输或读取。这意味着在等待期间,程序无法执行其他任务,可能导致效率降低。但在简单应用中,阻塞模式易于实现,代码简洁。 2. 非阻塞模式:非阻塞模式允许程序在等待数据传输时执行其他任务,提高程序的并发性。这种方式需要使用多线程或异步处理机制,以确保程序的运行不会因等待串口操作而中断。虽然实现起来相对复杂,但在需要高效处理多任务的场合,非阻塞模式更为合适。 C++实现串口通信的关键点: 1. 库的选择:C++中,可以使用如libserial、Boost.Asio或Qt等库来实现串口通信。这些库提供了封装好的API,简化了串口的打开、配置、读写及关闭等操作。 2. 设备设置:在开始通信前,需要配置串口参数,如波特率(baud rate)、数据位(data bits)、停止位(stop bits)和校验位(parity)。常见的波特率有9600、115200等,数据位通常为8位,停止位常设为1或2,校验位则根据需求选择奇偶校验或无校验。 3. 读写操作:在阻塞模式下,可以使用read和write函数直接读写数据;而在非阻塞模式下,通常需要配合select或poll系统调用来检测串口是否准备好进行读写。 4. 错误处理:在进行串口通信时,需要捕获并处理可能出现的错误,如打开失败、配置错误或数据传输错误等。 5. 多线程或异步处理:在非阻塞模式下,可以使用C++11引入的std::future、std::async或Boost.Asio的异步操作,实现串口通信与其他任务的并行处理。 6. 关闭串口:通信完成后,记得关闭串口,释放资源。 通过以上分析,我们可以看出,C++串口通信类的设计需要考虑阻塞与非阻塞模式的支持,以及相应的设备设置、错误处理和多线程/异步编程技术。实现这样的通信类,不仅能够提供灵活的串口操作,还能满足不同应用场景的需求。在实际项目中,开发者可以根据项目规模和性能要求,选择合适的串口通信模式和实现策略。
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
- 1
![avatar](https://profile-avatar.csdnimg.cn/8f73e96eeb6146db83b7052711baea9c_babyyang1213.jpg!1)
- 粉丝: 5
- 资源: 20
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- (GUI人机)MATLAB车道线检测.zip
- (GUI人机)MATLAB车牌出入库识别.zip
- (GUI人机)MATLAB车牌出入库计费系统.zip
- (GUI人机)MATLAB车牌号码出入库管理.zip
- (GUI人机)MATLAB车牌识别GUI实现.zip
- (GUI人机)MATLAB车牌识别.zip
- (GUI人机)MATLAB车牌识别GUI界面.zip
- (GUI人机)MATLAB车牌识别设计.zip
- (GUI人机)MATLAB虫害监测.zip
- (GUI人机)MATLAB虫害检测.zip
- (GUI人机)MATLAB答题卡自动识别系统.zip
- (GUI人机)MATLAB答题卡识别系统.zip
- (GUI人机)MATLAB答题卡识别GUI.zip
- (GUI人机)MATLAB打印纸缺陷检测GUI设计.zip
- (GUI人机)MATLAB答题纸试卷自动识别.zip
- (GUI人机)MATLAB打架斗殴异常行为识别.zip
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
- 1
- 2
前往页