Freelance Writing Jobs | Today's Articles | Sign In


How To Configure an RS-232 Port On A PC To Read

Set Up Word Length, Baud, Parity, Start and Stop Bits

Jun 7, 2009 Martin Bell

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 Addresses

Every 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:

  • Bit rate (i.e. baud, or bits/second)
  • Number of data bits
  • Whether parity bit is used
  • Number of Start & Stop bits

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 Transfer

To 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:

  • Bits 0 & 1: Set the word length: 00 = 5, 01 = 6, 10 = 7, 11 = 8
  • Bit 2: Set number of Stop bits: 0 = 1, 1 = 2
  • Bits 3 & 4: Set parity: x0 = none, 01 = odd, 11 = even
  • Bit 5: Stuck parity (not used here)
  • Bit 6: Enable break control: 1 = start sending 0s
  • Bit 7: Divisor Latch Access Bit (DLAB): Determines the mode of ports 3f8 and 3f9. When DLAB = 1 the baud rate may be set.

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 Transfer

Handshake 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 Transfer

Since 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:
  • Go to set-up mode by setting DLAB to 1
  • Define Word length, Number of Stop bits, and parity type.
  • Calculate the baud divisor and write it to the appropriate ports
  • Exit set-up mode by setting DLAB back to 0
  • Set the Interrupt Enable Register by writing 1 to bit 0 of (base address + 1)

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.
Computers Can Communicate In Different Ways, earl53 Computers Can Communicate In Different Ways
   
What do you think about this article?

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
post your comment
What is 10+4?
;