Serial I/O (RS232)

All Intel PCs have one or two serial ports usually referred to as Com1 and Com2. These comm ports provide voltages and pin outs consistent with RS-232C. Normally, these ports are used for mice, modems, printers, and the like.

Under DOS, it was possible to directly access these ports. However, Windows 95 and above forbid direct hardware access. Therefore, this page exists to help get around windows.

Generic References | Visual Basic | MS Access | Delphi | C++ Builder | Windows API

Generic References

Most of this was gathered by searching Altavista for rs232 (notice that I used lower case).

Visual Basic 6.0

You will use Comm.drv, MSComm.ocx, or MSComm32.ocx, depending on which compiler you are using. For example, in VisualBasic 6.0 Professional or Enterprise editions, first add the MSComm32.ocx to the Toolbox (add the Microsoft Comm Control 6.0 component), then add the control to your form and press F1 (Help).

Visual Basic Programmers Guide to Serial Communications by Richard Grier. Richard's page provides free ActiveX serial components for those that don't have MSComm32.ocx.

VB 6.0 provides VBTerm, a sample terminal emulation application under ..\samples\VB98\MSCOMM.

You can open a port as a file. This example, from comp.lang.basic.visual.misc, opens the printer

MS Access 97 SR-2

I developed the following code to read barcodes from a reader connected to the serial port. This first section is an attempt to open the port as a file (this works with most languages - but not with MS Access); the second section is based on MSCOMM32.ocx (which mostly works). This discussion is included here because MS Access uses VBA - Visual Basic for Applictions.

MS Access 97 SR-2 - Fails

On my system, the following code causes Access to hang.

I tested several variations with the same result

MS Access 97 SR-2 - Works

On the other hand, this works ... sort of. The trick is to know that ControlName_OnComm() is called when RThreshold is not zero. Unfortunately, this doesn't work so well because barcodes came in various length and RThreshold=12 only works with 12-character barcodes. With these modification, any length barcode will work. Notes:


Due to their size, the Delphi notes are here.

C++ Builder

C++ Builder uses the same API calls as Delphi. In addition, the it uses the came components.


With Borland C, you can use _bios_serialcom() in bios.h to perform serial I/O. It is unclear which compilers provide bios.h. On 9-24-01, I found references that both Microsoft Visual C++ and Watcom C++ also claim to support _bios_serialcom(). However, a search of my Visual C++ 6 system found neither bios.h nor _bios_serialcom(). Therefore, I assume that Microsoft has dropped the support.

At any rate, I will NOT provide copies of bios.h to anyone.

Virtual Integrated Design provides various RS-232 circuits and free software examples.

Accessing the RS232 Port in DOS using BIOS.H functions provides a summary of the allowed options/contants. There is also a program showing how to use bioscom to access ports.

Microsoft provides a Simple Example Using _bios_serialcom(). The article states that this interface tends to loose data. In order to improve the reliability, the comport needs to call an interrupt routine which moves the data to a buffer. This was easy under DOS, but generally not allowed under Windows.

Windows API

Search the SDK help for Communications (I used the SDK that came with Delphi 5).

Use CreateFile to open a handle to a communications resource, such as com1 or lpt1.

Test the returned handle to verify that the port is not locked by another process.

Use GetCommState to determine the current settings, SetCommState to change the settings.

You can use BuildCommDCB to pass common parameters (baud, parity, etc.) to the DCB as a string. But you'll still need SetCommState to actually change the settings.

See the DCB help for the supported constants.

Some additional commands - TransmitCommChar, PurgeComm, FlushFileBuffers

Microsoft Platform SDK - Communication Overview, Communication Functions

The only property which is remembered between disconnects is the Baud Rate. Parity, StopBits and the like are reset each time the connection is opened. Unfortunately, both SetCommState and SetCommConfig are extremely slow. As a result, it is not practical to disconnect the comport between uses.


Some versions of MSComm32.ocx leak memory.

Author: Robert Clemenzi -
URL: http:// / user / clemenzi / technical / Languages / SerialIO.htm