The serial communications are used for transferring data over long distances, because parallel communications requires too many wires. Serial data received from a modem or other devices are converted to parallel so that it can be transferred to the PC bus.
The serial communications equipment can be divided into simplex, half-duplex and full-duplex. A simplex serial communication send information only in one direction (i.e. a commercial radio station). Half-duplex means that data can be send in either direction between two systems, but only in one direction at a time. In a full-duplex transmission each system can send and receive data at the same time.
There are two ways to transmit serial data: synchronously or asynchronously. In a synchronous transmission data is sent in blocks, the transmitter and the receiver are synchronized by one or more special characters called sync characters.
The serial port of the PC is an asynchronous device, so we will describe this kind of systems. For asynchronous transmission, a bit identifies its start and 1 or 2 bits identify its end, don't need any synchronization. The data bits are sent to the receiver after the start bit. The least significant bit is transmitted first. A data character usually consist of 7 or 8 bits. Depending on the configuration of the transmission a parity bit is send after each data bit. It is used to check errors in the data characters. Finally 1 or 2 stop bits are send.
The serial port of the PC is compatible with the RS-232C standard. This standard was designed in the 1960s to communicate a data terminal equipment or DTE (the PC in this case) and a data communication equipment or DCE (usually a modem).
The standard specifies 25 signal pins, and that the DTE connector should be a male and the DCE connector should be a female. The most used connectors are the DB-25 male, but many of the 25 pins are not needed. For that reason in many modern PCs a DB-9 male connector is used. So you will find one or more of these connectors in the rear panel of the PC. The voltage levels are between -3V and -15V for a logic high. A logic low is a voltage between +3V and +15V. The commonly used voltages are +12V and -12V.
The most commonly used signals are listed below:
/DTR (Data-Terminal-Ready):The PC tells the modem that is powered up and ready to send data.
/DSR (Data-Set-Ready): The modem tells the PC it is powered up and ready to transmit or receive data.
/RTS (Request-To-Send): The PC sets this signal when has a character ready to be sent.
/CD (Carrier-Detect): The modem sets this signal when has detected the computer.
/CTS (Clear-To-Send): The modem is ready to transmit data. The computer will start sending data to the modem.
TxD: The modem receives data from de PC.
RxD: The modem transmits data to the PC.
The integrated circuits that convert the serial data lines to parallel and vice versa are called UART (Universal Asynchronous Receiver-Transmitter). The typical PC UART is the Intel 8251A, this IC can be programmed like a synchronous or an asynchronous device.
Eight data bits (D0-D7) connect the 8251A to the data bus of the PC. The chip select (/CS) input enables the IC when is asserted by de control bus of the PC system. This IC has two internal addresses, a control address and a data address. The control address is selected when the C-/D input is high. The data address is selected when the C-/D input is low. The RESET signal resets the IC. When the /RD is low the computer reads a control or a data byte. The /WR enables the PC to write a byte. Both signals are connected to the system signals with the same names.
The UART includes four internal registers:
THR: Temporary output register.
TSR: Output register.
RDR: Input register.
RSR: Temporary input register.
Every character to be transmitted is stored in the THR register. The UART adds the start and stop bits. Then copies all bits (data, start and stop bits) to the TSR. To finish the process the bits are sent to the line by the TD signal.
Every character received from the line RD is stored in the RSR register. The start and stop bits are eliminated and the UART writes this character to the RDR. To finish the process the character is read for the PC.
It is two ways to address the serial port, by the 14H BIOS interrupt and by the 21H DOS interrupt.
The 14H BIOS interrupt uses four functions to program the serial port. Each function is selected assigning a value to the AH register of the microprocessor. We list the four functions below:
Function 00H: Initializes the serial port and sets the speed, data and stop bits and the parity parameters.
Function 01H: Sends a character to the specified serial port.
Function 02H: Reads a character from the specified serial port.
Function 003: Returns the state of the specified serial port.
There is three functions in the 21H DOS interrupt related to the operation of the serial port:
Function 03H: Reads a character from the COM1 serial port.
Function 04H: Writes a character to the COM1 serial port.
Function 40H: It is a common out function for all files and devices that use a handle access. This function send a number of bytes from a buffer to the specified device.
The program shows you how to communicate two PC s via serial port:
|
//Program to communicate two PC s via serial port //00H bios function (AL register) //bits 7 6 5 Baud rate // 0 0 0 110 // 0 0 1 150 // 0 1 0 300 // 0 1 1 600 // 1 0 0 1200 // 1 0 1 2400 // 1 1 0 4800 // 1 1 1 9600 //bits 4 3 Parity bits // 0 0 no parity // 0 1 odd parity // 1 1 even //bits 2 stop bits // 0 1 stop bit // 1 2 stop bit //bits 1 0 Number of bits per data // 1 0 7 data bits // 1 1 8 data bits //Register Dx 0->com1, 1->com2, 2->com3, 3->com4 //Configuration: 9600 bd,no parity,2 stop bits and 8 data bits //AL register value is 1 1 1 0 0 1 1 1 -> 0xE7 #include <stdio.h> #include <process.h> #include <conio.h> #include <dos.h> #include <bios.h> #define TRUE 1 #define PARAM 0xA7 #define COM1 0 #define COM2 1 void init_port(void); char state_port(void); void send_byte(unsigned char); unsigned char read_byte(void); void keyb(void); int tecla = 1; void main ( void ) { unsigned char read_com; unsigned char read_kb; clrscr(); init_port(); while(read_kb != 'c') { read_kb=getch(); send_byte(read_kb); read_com=read_byte(); if(read_com!=0){printf("%c",read_com);} } } void init_port() { union REGS regs; regs.h.ah = 0x00; regs.x.dx = COM2; regs.h.al = PARAM; int86( 0x14, ®s, ®s); } //return the state of the port char state_port() { union REGS regs; regs.h.ah = 0x03; regs.x.dx = COM2; int86( 0x14, ®s, ®s); if(regs.h.ah & 0x80) printf("\t EXCEED TIME\n"); if(regs.h.ah & 0x40) printf("\t TSR EMPTY\n"); if(regs.h.ah & 0x20) printf("\t THR EMPTY\n"); if(regs.h.ah & 0x10) printf("\t INTERRUPTION\n"); if(regs.h.ah & 0x08) printf("\t THREAT ERROR\n"); if(regs.h.ah & 0x04) printf("\t PARITY ERROR\n"); if(regs.h.ah & 0x02) printf("\t OVERLOAD ERROR\n"); return (regs.h.ah); } //Keyboard handle void keyb() { union u_type{int a;char b[3];}keystroke;char inkey=0; if(bioskey(1)==0) return; keystroke.a=bioskey(0); inkey=keystroke.b[1]; switch (inkey) { case 1: keyb=0; return; /*ESC*/ default: keyb=15; return; } } //Send a character to the serial port void send_byte(unsigned char byte) { union REGS regs; regs.h.ah = 0x01; regs.x.dx = COM2; regs.h.al = byte; int86( 0x14, ®s, ®s); if( regs.h.ah & 0x80) { printf("\t SENDING ERROR "); exit(1); } } //read a character from serial port unsigned char read_byte() { int x,a; union REGS regs; if((estate_port() & 0x01)) { regs.h.ah = 0x02; regs.x.dx = COM2; int86(0x14,®s,®s); if(regs.h.ah & 0x80) { printf("\t RECEIVING ERROR"); exit(1); } return(regs.h.al);} else { return(0); } } |
Please e-mail us at: pckits@apdo.com