How To Configure an RS-232 Port On A PC To ReadSet Up Word Length, Baud, Parity, Start and Stop Bits
Detailed guide on how to configure the serial port on a PC to enable it to read data from external devices, including details of the registers used.
The article Reading Information From a PC Serial Port describes the steps needed to obtain data from an external sensor or other computer. This article explains how the different registers are configured to make the computer and the other device compatible. These two articles show what to do in a DOS (or earlier Windows) environment: for later versions of Windows please refer to How To Read a Serial Port Using Windows. Serial Port and Register AddressesEvery serial port has a base address where data are sent to or recieved from. On most home computers the COM1 serial port has address 03f8 in hexadecimal. It is supported by seven other registers at addresses 03f9 to 03ff. What Needs To Be Defined To Read A Serial Port?The devices that are communicating must use the same "format" of data:
The example here will use a data transfer rate of 4800, word length 8, one stop bit, and no parity. No handshake will be used, because many sensors or devices are unable to do this. Set Up Mode For Serial Data TransferTo go into set-up mode, bit 7 of the Line Control Register (Base address + 3, or 3fb) is set to 1. A full description of the function of each bit is:
In the example being used, an 8-bit word means that bits 0 and 1 are both =1, 1 stop bit means that bit 2=0, no parity is used so bits 3 and 4 = 0, (although bit 4 could also be 1), bit 5 is not used and so is 0, no break control will be used so bit 6=0, and the data rate is being set so bit 7 must be 1. The full description of the Line Control Register is therefore = 10000011. In decimal, this is equal to 131. If there are any doubts about which settings to use, the manufacturers data sheet should be consulted. Set Up Handshake Type For Serial Data TransferHandshake details are set up using address base address + 4 (3fc), the Modem Control Register. Only the lower 5 bits are ever used. To set up the serial port with no handshake between the computer and the other device, set all bits to zero, so (base address + 4) = 0. Set Up Data Transfer Rate For Serial Data TransferSince the baud rate is being set, DLAB is set to 1. By setting DLAB to 1, it is possible to write the baud rate to addresses 3f9 and 3f8. The baud rate chosen is 4800. Baud rate divisors are calculated by taking the oscillating frequency (1,843,200) and dividing by the desired Baud Rate x the UART clocking factor of 16. For 4800 baud, the divisor is:- 1,843,200 / (4800 x 16) = 24 So, 24 is written to port 3f8 (in its role as Divisor Latch Low Byte), while 0 is written to address 3f9 (the Divisor Latch High Byte). Summary Of Serial Port Configuration Setting up a serial port to read from an external device requires only a few lines of code. These are all shown in the article Reading Information From a PC Serial Port. The basic steps are:
To read COM1 in a Windows environment (for Windows 95 or above), see the article Reading Information From a PC Serial Port Using Windows. The hardware needed to connect to the serial cable is readily available and cheap.
The copyright of the article How To Configure an RS-232 Port On A PC To Read in Computer Programming is owned by Martin Bell. Permission to republish How To Configure an RS-232 Port On A PC To Read in print or online must be granted by the author in writing.
Related Topics
Reference
More in Technology
|