Writing a Serial Communication Library for Windows
(Page 1 of 6 )
Serial Data transmission can be diffucult for newcomers to the world of Serial Communication and especially for developers trying it on the Windows platform using C++/VC++. This article illustrates how to develop a simple class for implementing serial data communication.
Introduction
Serial Data transmission seems a bit difficult for those who are new to the world of Serial Communication and especially for the developers who try to achieve it on the Windows platform using C++/VC++. When I was an Electronics graduate doing my Final year project that was developing a Add-on Data Acquisition Card that works on Windows 98/2000/XP, I faced a lot of problems on Windows 2000/XP and I searched a lot for some help on accessing ports in Windows 2000/XP. I got some valuable information on Direct Port Access under Windows NT on DDJ.com in an article by Dale Roberts. Here in this article I combine that experience with Serial communication and show you how to develop a simple class for implementing serial data communication.
This simple Class will ease development of Data Communication or Embedded Interface applications. It implements the functionality using the Win32 API functions, so I’d expect basic familiarity with Win32 on the reader’s part. So we shall first understand the basics of Serial data communication before we get to the class.
Serial Data Transmission Basics
In serial data communication the data is transmitted in serial format with the LSB (Least Significant Bit) of the byte to be transmitted as the first among the data bits. The general format for serial transmission is as in the figure below.

The Parity bit (as in the third box) is optional. It is to be included only if you want to put a check on the validity of the transmitted data on the receiving end i.e. to perform error checking in communication. Parity settings can be altered by software modifications and hence it is easy to enable or disable it as per the design of the application and hardware you’re designing. You might be aware that the parity can be odd or even so you might like to configure it appropriately.
The following steps are used for sending and receiving data through the serial port of a PC:
- Open the Communication Port that you decide to use.
- Configure the selected Communication Port by setting the Baud rate, parity, no. of data bits, etc.
- Set time-out value for Communication.
- Read/Write Data to the Port.
- Close the Port.
Now we shall discuss these steps in detail so as to get a closer look at the process.
Next: Opening the Selected Serial Port >>
More Code Examples Articles
More By Digvijay Chauhan