You are on page 1of 4

CSerialPort v1.

28 An MFC class for Win32 serial ports


Welcome to CSerialPort, an MFC class to support access to the Win32 APIs dealing with serial ports.

Features
Simple and clean C++ interface.
Uses C++ exceptions in preference to the normal Win32 return value mechanism. This ensures that code which
uses CSerialPort is more robust.
Unicode enabled, supports linking to MFC statically and all code compiles cleanly at warning level 4.
Supports overlapped, blocking and call-back usage models of the serial port. (Note Call-back is not supported on
Windows 9x).

The enclosed zip file contains the CSerialPort source code and a simple test program which exercises all class API's.

Copyright
You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise) when
your product is released in binary form.
You are allowed to modify the source code in any way you want except you cannot modify the copyright details at
the top of each module.
If you want to distribute source code with your application, then you are only allowed to distribute versions
released by the author. This is to maintain a single distribution point for the source code.

Updates
V1.0 (31 May 1999)
Initial public release.
V1.01 (3 June 1999)
Fixed problem with code using CancelIo function which does not exist on Windows 95. This caused any modules
developed with CSerialPort to fail to load on Windows 95.
Fixed leaks which can occur in sample app when an exception is thrown.
V1.02 (16 June 1999)
Fixed a bug whereby CString::ReleaseBuffer was not being called in CSerialException::GetErrorMessage
V1.03 (29 September 1999)
Fixed a simple copy and paste bug in CSerialPort::SetDTR
V1.04 (8 March 2000)
Links in the documentation now work <g>.
V1.05 (8 May 2000)
Fixed an unreferenced variable in CSerialPort::GetOverlappedResult in VC 6
V1.06 (12 June 2000)
Fixed another unreferenced variable problem in CSerialPortApp::InitInstance in VC 6.
3 July 2000
Minor update to the documentation.
V1.07 (10 December 2000)
Made class destructor virtual.
V1.08 (15 January 2001)
Attach method now also allows you to specify whether the serial port is being attached to in overlapped mode
Removed some ASSERT's which were unnecessary in some of the functions
Updated the Read method which uses OVERLAPPED IO to also return the bytes read. This allows calls to WriteFile
with a 0'ized overlapped structure (This is required when dealing with TAPI and serial communications)
Now includes copyright message in the source code and documentation.
V1.09 (24 March 2001)
Added a BytesWaiting method
V1.10 (4 April 2001)
Provided an overridden version of BytesWaiting which specifies a timeout.
V1.11 (23 April 2001)
Fixed a memory leak in DataWaiting method.
V1.12 (1 May 2002)
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.
V1.13 (29 May 2002)
Fixed an problem where the GetProcAddress for CancelIO was using the wrong calling convention.
V1.14 (7 August 2002)
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/Multiple]Object. 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.
V1.15 (20 September 2002)
Addition of an additional ASSERT in the internal _OnCompletion function.
Addition of an optional out parameter to the Write method which operates in overlapped mode. Thanks to Kevin
Pinkerton for this addition.
5 March 2003
Updated the documentation for the function CSerialPort::GetStatus.
v1.16 (10 April 2006)
Updated copyright details.
Addition of a CSERIALPORT_EXT_CLASS and CSERIALPORT_EXT_API macros which makes the class easier to use
in an extension dll.
Removed derivation of CSerialPort from CObject as it was not really needed.
Fixed a number of level 4 warnings in the sample app.
Reworked the overlapped IO methods to expose the LPOVERLAPPED structure to client code.
Updated the documentation to use the same style as the web site.
Did a spell check of the HTML documentation.
Updated the documentation on possible blocking in Read/Ex function. Thanks to D Kerrison for reporting this issue.
Fixed a minor issue in the sample app when the code is compiled using /Wp64
v1.17 (2 June 2006)
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
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.
Reviewed all TRACE statements for correctness.
v1.18 (5 June 2006)
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.
v1.19 (24 June 2006)
Fixed some typos in the history list. Thanks to Simon Wong for reporting this.
Made the class which handles the construction of function pointers at runtime a member variable of CSerialPort
Made AfxThrowSerialPortException part of the CSerialPort class. Thanks to Simon Wong for reporting this.
Removed the unnecessary CSerialException destructor. Thanks to Simon Wong for reporting this.
Fixed a minor error in the TRACE text in CSerialPort::SetDefaultConfig. Again thanks to Simon Wong for reporting
this.
Code now uses new C++ style casts rather than old style C casts where necessary. Again thanks to Simon Wong
for reporting this.
CSerialException::GetErrorMessage now uses the strsafe functions. This does mean that the code now requires
the Platform SDK if compiled using VC 6.
v1.20 (25 June 2006)
Combined the functionality of the CSerialPortData class into the main CSerialPort class.
Renamed AfxThrowSerialPortException to ThrowSerialPortException and made the method public.
v1.21 (5 November 2006)
Minor update to stdafx.h of sample app to avoid compiler warnings in VC 2005.
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.
Minor update to remove strsafe.h from stdafx.h of the sample app.
Updated copyright details.
v1.22 (25 January 2007)
Minor update to remove strsafe.h from stdafx.h of the sample app.
Updated copyright details.
v1.23 (24 December 2007)
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.
Simplified the code in CSerialException::GetErrorMessage somewhat.
Optimized the CSerialException constructor code.
Code now uses newer C++ style casts instead of C style casts.
Reviewed and updated all the TRACE logging in the module
Replaced all calls to ZeroMemory with memset
v1.24 (30 December 2007)
Updated the sample app to clean compile on VC 2008
CSerialException::GetErrorMessage now uses Checked::tcsncpy_s if compiled using VC 2005 or later.
v1.25 (18 May 2008)
Updated copyright details.
Changed the actual values for Parity enum so that they are consistent with the Parity define values in the Windows
SDK header file WinBase.h. This avoids the potential issue where you use the CSerialPort enum parity values in a
call to the raw Win32 API calls. Thanks to Robert Krueger for reporting this issue.
v1.26 (21 June 2008)
Code now compiles cleanly using Code Analysis (/analyze)
Updated code to compile correctly using _ATL_CSTRING_EXPLICIT_CONSTRUCTORS define
The code now only supports VC 2005 or later.
CSerialPort::Read, Write, GetOverlappedResult & WaitEvent now throw an exception irrespective of whether the
last error is ERROR_IO_PENDING or not
Replaced all calls to ZeroMemory with memset
v1.27 (4 July 2008)
Provided a version of the Open method which takes a string instead of a numeric port number value. This allows
the code to support some virtual serial port packages which do not use device names of the form "COM%d".
Thanks to David Balazic for suggesting this addition.
v1.28 (25 January 2013)
Updated copyright details.
Updated sample app and class to compile cleanly on VC 2010 and later.

You might also like