C++ Wrapper class for RS232 COM connections

Recently I had the need to create and maintain a serial RS232 COM connection in a project I was working on. A quick bit of research and it soon became obvious that, although it was pretty straight forward to do using OS native APIs such as Win32 or Linux, there were no easy to use cross platform libraries readily available without resorting to the likes of boost, which seemed a bit excessive for the project in hand. Native APIs are also often written in C and, while I'm not uncomfortable with C, I'm generally an OO/C++ kinda guy so I decided to write a small piece of code which wraps both Windows and Linux native API calls in a C++ class. This allows me to open and maintain COM connections in a simple and object oriented way without worrying too much about what's going on underneath. The wrapper is also customisable, of course, so it's easy to alter specific settings and add support for manipulating control lines if needs be. The class consists of 6 functions:

SerialConnection::OpenPort()
SerialConnection::SendByte()
SerialConnection::SendByteArray()
SerialConnection::ReadByte()
SerialConnection::ReadByteArray()
SerialConnection::ClosePort()

which should be pretty self-explanatory. At some stage, should the need arise, I may add some functions for checking the status of connections and their properties, but currently these six are enough for what I need to do. I've made the class available for download with an included example and readme file which goes into a bit more detail.

You can download the class here.

Popular Posts