How COM Ports Are Accessed
Serial data is carried as a sequence of high and low voltages over a number of wires with fairly precise timing characteristics. Fortunately for the programmer, it isn’t necessary to know much about how serial ports work. Under DOS, you had to work with a very low-level interface; a certain range of bytes (starting at 0x03F8 for COM1 and 0x02F8 for COM2) contained information about the serial port. Your program would loop continually checking the values of bits and bytes in this region; some of these signaled the arrival of data, others the data values itself, and others error values. It was messy and painful to program.
Windows 3.1 exposed a higher-level API, with a layer of buffering;
functions such as ReadComm and
WriteComm that provide insulation from the
hardware and a somewhat file-like interface; and event-driven
communications, which allowed you to have messages sent to a window
when data arrived at the COM port. Windows NT drops the windows
message concept and completes the move to a file-like interface,
using the same functions to open, read and write COM ports as regular
files. It also prevents programs from accessing the serial port
(i.e., those previous addresses) directly.
Timing issues
Example programs give the impression of reading and writing from files, but this can be misleading. Files on the disk are all there at once, and you know when you have got to the end. Furthermore, if you open a file for reading and/or writing, you know where ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access