Professional Documents
Culture Documents
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).
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.
• Without Len = 3
• With temp as a variant
• Both "com1" and "com1:"
• Both Input and Binary
'********************************************
Barcode_UIEdit_Change
End Sub
'********************************************
Notes:
• In the help, the case statement has many options. To simplify this example, I
removed all but the one that reads the data.
• The only available timer is attached to the form. Therefore, it is hard to
encapsulate this in a library.
• MS Access 97 SR-2 will not display the MSComm help when you click on a
command and press F1 ... unless you FIRST locate and open
"C:\WIN98\HELP\COMCTL2.HLP". (Your path may be different.)
• This code requires that MSCOMM32.ocx is installed on every machine that
uses this feature. Simply copying the file is NOT sufficient. There are no
instructions on how to do it correctly. Copying it installs the TypeLib resistry
key, but not the several CLSID's or the License.
• I had everything working perfect. When I tried to demonstrate the code to my
boss, NONE of the case constants were defined ... Well they were 2 hours
before! (And they work fine now.)
Delphi
C++ Builder
C++ Builder uses the same API calls as Delphi. In addition, the it uses the came
components.
_bios_serialcom
With Borland C, you can use _bios_serialcom() in bios.h to perform serial I/O.
#include <bios.h>
unsigned temp;
// Open serial port at 1200 baud, 8 data bits,
// No parity, 1 stop bit
temp = _bios_serialcom(_COM_INIT, 0,
_COM_CHR8 | _COM_NOPARITY | _COM_STOP1 |
_COM_1200);
temp = _bios_serialcom(_COM_RECEIVE, 0, 0); // Read a
character
temp = _bios_serialcom(_COM_SEND , 0, '*'); // Write a
character
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).
Test the returned handle to verify that the port is not locked by another process.
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.
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.
Warning