A C++ Class for Controlling a Serial Port in Windows
Whilst serial ports are old technology now, and has mostly been superceded by USB, it is still an easy way to communicate with embedded hardware; it is great for small projects. This page presents a simple C++ class for communicating with hardware via a serial port. It should work with Windows XP and Vista, and, unlike other classes out there, it does not require MFC or AFX, meaning that it can be used with Visual Studio 2003+ Express edition.
Recently, I needed to be able to use the serial port in order to control a small humanoid robot. The humanoid robot contained a microcontroller connected via a serial port to a Bluetooth module. Unfortunately, using the serial port under Microsoft Windows can be a little tricky. All the existing C++ classes that I found on the internet either required MFC or AFX, or were not free (one even asking over $100!!). Since I was using Visual Studio 2008 Express, I needed something which did not require MFC or AFX. Thus, this class was born.
Using the Class
The best way to learn how to use this class is to look at the example provided. Essentially, a serial port is opened by passing the name of the comm port (e.g., "COM1") and the desired bit-rate to the class constructor , for example:
Serial serial(commPortName, 9600);
If the bitrate parameter is not given, the bitrate will default to 115200 bps. An exception containing an error message will be thrown if anything goes wrong. After this, the serial port can be written-to/read-from via write/read methods. The methods (including variants) are:
- int write(const char buffer) - Writes a NULL terminated string. The parameter buffer is a pointer to the string. It returns the number of bytes actually written.
- int write(const char *buffer, int buffLen) - Writes buffLen bytes from the parameter buffer to the serial port. This string can include NULL characters. It returns the number of bytes actually written.
- int read(char *buffer, int buffLen, bool nullTerminate = true) - Reads buffLen bytes to the buffer parameter. If nullTerminate is set to true (which it is by default), then it will NULL terminate the string and will read at most buffLen - 1 bytes. It will return the number of bytes actually read, which can be NULL if no data was received.
- void flush() - Flushes the serial port's read buffer (i.e., everything is discarded).
About the Test Program
The test program that comes with the serial class provides a very simple example of how to use the serial class. It expects the name of the com port to be passed as a command line parameter. It opens that serial port and communicates at 115200 bps. If a different bitrate is desired, the source code will have to be modified.
This source code can be used and/or modified without restrictions. It is provided as is and the author disclaims all warranties, expressed or implied, including, without limitation, the warranties of merchantability and of fitness for any purpose. The user must assume the entire risk of using the Software.
Articles » Windows Development » A C++ Class for Controlling a Serial Port in Windows