Information Technology Reference
In-Depth Information
13.4 Programming RS-232
Normally, serial transmission is achieved via the RS-232 standard. Although 25 lines are
defined usually only a few are used. Data is sent along the TD line and received by the RD
line with a common ground return. The other lines, used for handshaking, are RTS (ready to
send) which is an output signal to indicate that data is ready to be transmitted and CTS (clear
to send), which is an input indicating that the remote equipment is ready to receive data.
The 8250/NS16650 IC is commonly used in serial communications. It is typically inte-
grated in the PC chip set, or can be mounted on an I/O card. This section discusses how it is
programmed.
Programming the serial device
The main registers used in RS-232 communications are the line control register (LCR), the
line status register (LSR) and the transmit and receive buffers (see Figure 13.14). The trans-
mit and receive buffers share the same addresses.
The base address of the primary port ( COM1: ) is normally set at 3F8h and the secondary
port ( COM2: ) at 2F8h. A standard PC can support up to four COM ports. These addresses
are set in the BIOS memory and the address of each of the ports is stored at address locations
0040:0000 ( COM1: ), 0040:0002 ( COM2: ), 0040:0004 ( COM3: ) and 0040:0008
( COM4: ). Program 13.1 can be used to identify these addresses. The statement:
ptr=(int far *)0x0400000;
initializes a far pointer to the start of the BIOS communications port addresses. Each address
is 16 bits thus the pointer points to an integer value. A far pointer is used as this can access
the full 1 MB of memory, a near pointer can only access a maximum of 64 kB.
Program 13.1
#include <stdio.h>
#include <conio.h>
int main(void)
{
int far *ptr; /* 20-bit pointer */
ptr=(int far *)0x0400000; /* 0040:0000 */
clrscr();
printf("COM1: %04x\n",*ptr);
printf("COM2: %04x\n",*(ptr+1));
printf("COM3: %04x\n",*(ptr+2));
printf("COM4: %04x\n",*(ptr+3));
return(0);
}
Test run 3.1 shows a sample run. In this case, there are four COM ports installed on the PC.
If any of the addresses is zero then that COM port is not installed on the system.
Test run 3.1
COM1: 03f8
COM2: 02f8
COM3: 03e8
COM4: 02e8
 
Search WWH ::




Custom Search