Serial Communications for 32-bit Windows
This application note describes serial communications within the Microsoft Windows 32-bit Application Programming Interface (Win32 API). It provides some hints on how one may construct a windows program to communicate via a serial port. If time is not one of your most plentiful resources then you may not want to program a serial port communicator yourself. In this case, I would suggest that a program like hyper-terminal for Windows be used instead. It should also be noted that this application note assumes the reader has some knowledge of GUI based operating systems.
What is Win32?
Win32 is an application programming interface provided by Microsoft for developers writing windows programs. It defines an interface to the underlying system kernel that manages system resources such as a serial communications port. The Win32 API is composed of a family of C-functions and structures usually available in the form of a C library. These libraries are provided with various programming environment products, such as Borland C++ Builder or Microsoft Visual C++. These tools provide various features that make windows programming more convenient like on-line Win32 help, project management, a compiler, graphical window builder etc. I recommend investing in such a development tool before diving into Win32.
Implementation
There are various ways that a developer may choose to construct a communications program. Basically, once a serial port is configured by the program at start up (i.e. baud rate, stop bits, parity etc.) it must poll for incoming data. This can be done in two ways. The first is to periodically check for data on the input buffer of the port. This implementation will consume CPU cycles from a program. If your program is very user interactive then this method may appear to "hang" the program, especially if baud rates are high and the communication on the serial port is heavy. Of course, one could compensate for this by decreasing the rate that the program polls the port, however, the program then runs the risk of input buffer overflow - causing data to be lost.
Reading the serial port can also be done within a separate thread of the program. This implementation requires that the programmer be familiar with multithreading for Win32. The advantage to multithreading a communications program is that waiting for data will not "hang" the program. Basically, the main program spawns a separate thread that is responsible for receiving data from the port in question. The thread can be written so that it waits for the data, collects it, informs the main program that data is waiting for consumption, then waits again. Although multithreading your program may be the "best" solution, it is a complicated one. For example, difficulties may erupt from resource synchronization and mismanagement between reading from the port and writing to the port. The Win32 API provides a form of **implicit** multithreading with a concept called overlapped I/O. For more information on multithreading and overlapped versus non-overlapped communications see the list of www links below.
Functions Overview
Listed here are some important Win32 functions needed for configuring and communicating with a serial port. This list is a minimal description of the functions available and/or necessary to write a communications program. Function prototypes can be found within on-line help manuals of various programming studio products or at some of the links provided below.
Function |
Description |
CreateFile() |
opens a specific communications port. |
SetCommState() |
sets the state of the serial port in question - Baud rate, parity, stop bits etc. |
GetCommState() |
retrieves the state of the serial port in question - Baud rate, parity, stop bits etc. |
ReadFile() |
reads data from a port. Can be specified to return immediately or wait for incoming data. |
WriteFile() |
writes data to the serial port. |
Links
Microsoft Win32 Developer Support Documents. Information on overlapped serial communications and multithreading can be found here.
Specifically:
Serial Communications in Win32
Windows Developer FAQ. This site provides a good example of a program that implements multithreading and serial communications (see link #6: "How do I use the serial port in Win32?").
Some products and books available for purchase:
A book on Serial Communications
Brought to you by the Navigator Development Team
October 1998