home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Unsorted BBS Collection
/
thegreatunsorted.tar
/
thegreatunsorted
/
programming
/
misc_programming
/
comm
< prev
next >
Wrap
Text File
|
1990-01-05
|
18KB
|
338 lines
Chapter 10
Asynchronous Serial Communication Services
------------------------------------------
1. The Asynchronous Serial Protocol
The main concept behind the serial protocol is that all the data and
control information necessary to transmit and receive a character of
information must move over a single data line, one bit at a time.
The way this is done is shown in Figure 1-1.
MARKING
(LOGICAL 1) ───┐ ┌────┬────┬────┬────┬────┬────┬────┬────┬────────
│ │ | | | | | | │ │
SPACING └────┴────┴────┴────┴────┴────┴────┴────┴────┘
(LOGICAL 0) START DATA BITS PARITY STOP
BIT BIT BIT(S)
(OPTIONAL)
<─────── DATA FLOW
Figure 1-1. Asynchronous serial data format
-------------------------------------------
The width of each bit is determined by the data transmission speed,
which is measured in bits per second. This speed is called the
"baud rate".
When no data is being transmitted on the line, the line is left in a
logical 1 state, or a "marking state". When we wish to transmit a
character of data, the first bit to be transmitted is the "start bit".
This start bit is represented by a logical 0 state, or a "spacing
state", on the line. The receiver knows to expect a character of data
when it notices the line change from a marking state to a spacing state.
The data bits, which make up the character of information that is being
transmitted, immediately follow the start bit. The number of data bits
can be 5, 6, 7, or 8, but each character must contain the same number
of data bits in the same transmission.
An optional "parity bit" immediately follows the data bits. There are
three types of parity: even, odd, and none parity. The parity bit
allows the receiver to detect certain types of transmission errors.
There ara 1, 1.5, or 2 bits of marking (logical 1) following the parity
bit (or data bits if no parity bit is present). These bits are called
the "stop bits". The stop bits represent the minimum amount of time
that the line must be in a marking condition before the next start bit
appears.
Because data characters can start and stop arriving at any time, the
serial communications protocol discussed above is an "asynchronous"
protocol. The receiver has to synchronize with the sender when the
start bit of a new character is received. Since the baud rates of the
sender and the receiver are set up to be the same, the receiver is able
to pick off the start, data, parity, and stop bits from the line as a
function of time; the beginning of the start bit is used as the
synchronizing event.
2. The Universal Asynchronous Receiver Transmitter (UART)
It would seem quit complex to write a program to send and receive data
characters in an asynchronous serial communications environment.
Fortunately, a dedicated microprocessor called a "Universal
Asynchronous Receiver Transmitter (UART)", is used to perform most of
the serial protocol conversion. There are several UART's: Intel 8250,
8251(USART), NS 16450, 16550(with FIFOs).
A functional diagram of a UART is shown in Figure 2-1. Modem and
interrupt control signals have been omitted for clarity.
┌--------------------------------------------┐
| ┌───────────┐ ┌───────────┐ |
RECEIVED | │ RECEIVER │ │ RECEIVER │ | COMMUNICATIONS
DATA <───────┤ DATA │<──────────┤ SHIFT │<──── INPUT
| │ REGISTER │ │ REGISTER │ |
| └───────────┘ └───────────┘ |
| |
COMMUNICATIONS| STATUS |
STATUS <───── ERROR *PARITY |
| CONDITIONS *OVERRUN |
| *FRAMING |
| *BREAK |
| *RECEIVED DATA READY |
| *TRANSMITTER HOLDING REGISTER EMPTY |
| |
COMMUNICATIONS| |
CONTROL ─────> CONTROL |
| *DESIRED BAUD RATE |
| *NUMBER OF DATA BITS |
| *SELECTION AND TYPE OF PARITY |
| *NUMBER OF STOP BITS |
| |
| ┌───────────┐ ┌───────────┐ |
TRANSMITTED | │TRANSMITTER│ │TRANSMITTER│ | COMMUNICATIONS
DATA ───────>│ HOLDING ├──────────>│ SHIFT ├─────> OUTPUT
| │ REGISTER │ │ REGISTER │ |
| └───────────┘ └───────────┘ |
| ┌───────────┐ |
| │ BAUD │ |
| ┌>│ RATE │ |
| │ │ GENERATOR │ |
| │ └───────────┘ |
└-----------------│--------------------------┘
<── TO COMPUTER │ TO COMMUNICATIONS LINE ──>
CLOCK
Figure 2-1. Functional diagram of UART
----------------------------------------
When we want to transmit a character of data, we check the UART status
to see if the "transmitter holding register" is empty. If it is, we
can output a byte of data to the UART, and this byte is placed in the
transmitter holding register. When the UART has completed transmitting
the previous character or is currently not transmitting any data, the
contents of the transmitter holding register are placed in the
"transmitter shift register". The transmitter holding register is then
available for another output character from the computer.
The UART places any serial input it receives into the "receiver shift
register". After the appropriate number of stop bits are received and
error checking is performed, the character is placed in the "receiver
data register". The UART then updates its status to show that receiver
data is ready. The computer knows that it can read another byte of
data from the UART when the receiver-data-ready status is correct.
After the data is read by the computer, the UART does not show receiver
data ready again until it has placed another character into the
receiver data register.
If the UART must place a second character into the receiver data
register before the first character is read by the computer, then an
"overrun" error results.
If the UART does not receive a stop bit when it is expecting one, then
a "framing" error results.
To send a break condition, the line must be held in a spacing condition
for as long as it takes to transmit one character with all its
associated control bits. The UART will reflect the reception of a
break condition in its status output.
3. The Modem
The RS232 interface electronically connects the UART to the outside
world. It can be directly connected to another RS232 interface only
over short distances. If we want two computers to be able to talk to
each other over long distances or over the phonelines, then the
electronic output of the RS232 interface must be used with an
additional interfacing equipment called "MODEM". (see Figure 3-1)
┌──────┐ ┌─────────┐ SHORT ┌─────────┐ ┌──────┐
│ UART │ │ RS232 │ DISTANCE │ RS232 │ │ UART │
│ 1 │<───>│INTERFACE│<─────────────────────>│INTERFACE│<───>│ 2 │
└──────┘ └─────────┘ └─────────┘ └──────┘
┌──────┐ ┌─────────┐ ┌─────────┐ ┌──────┐
│ UART │ │ RS232 │ │ RS232 │ │ UART │
│ 1 │<───>│INTERFACE│<┐ ┌>│INTERFACE│<───>│ 2 │
└──────┘ └─────────┘ │ │ └─────────┘ └──────┘
│ PHONELINE │
┌─────────┐ │ OR │ ┌─────────┐
│ MODEM │<┘ LONG DISTANCE └>│ MODEM │
│ │<─────────────────────>│ │
└─────────┘ └─────────┘
Figure 3-1. Use of MODEM
------------------------
The modem control signals are described in Table 3-1.
Table 3-1. Modem Control Signals
┌────────────────────────────────────────────────────────────────────────┐
│Signals to Modem From Computer: │
│DTR - DATA TERMINAL READY │
│ Under program control. Used to tell the modem that the computer │
│ is powered up and ready. │
│RTS - REQUEST TO SEND │
│ Under program control. Used to tell the modem that the computer │
│ wants to send data now. │
│ │
│Signals From Modem to Computer: │
│DSR - DATA SET READY │
│ Used to tell the computer that the modem is powered up and ready. │
│CTS - CLEAR TO SEND │
│ Used to tell the computer that the modem is ready to accept data │
│ transmission. │
│DCD - DATA CARRIER DETECT │
│ Used to tell the computer that the modem has established a │
│ connection with the modem on the other end of the phone line. │
│RI - RING INDICATOR │
│ Used to tell the computer that the phone connected to the modem │
│ is ringing. │
└────────────────────────────────────────────────────────────────────────┘
4. The Physical Interface
The communications adapter for the computer provides a standard RS232
interface to the outside world. The physical pinout for the RS232
connector is shown in Table 4-1.
Table 4-1. Standard RS232 Pinout
┌───────────────┬───────────────┬────────────────────────────┐
│ Pin Number │ Direction │ Function │
├───────────────┼───────────────┼────────────────────────────┤
│ 2 │ Output │ Transmitted Data │
│ 3 │ Input │ Received Data │
│ 4 │ Output │ Request To Send │
│ 5 │ Input │ Clear To Send │
│ 6 │ Input │ Data Set Ready │
│ 7 │ │ Signal Ground │
│ 8 │ Input │ Data Carrier Detect │
│ 20 │ Output │ Data Terminal Ready │
│ 22 │ Input │ Ring Indicator │
└───────────────┴───────────────┴────────────────────────────┘
However, if we wish to connect a PC to another PC, we cannot simply
connect pin 2 to pin 2, and so on. If we did this, then the two
transmit-data pins would be connected to each other, which wouldn't
work very well. The correct way to connect two machines with the
interface shown in Table 4-1 is shown in Figure 4-1. This connection
scheme is sometimes called a "null modem".
COMPUTER 1 COMPUTER 2
┌──────────────────┐ ┌──────────────────┐
│ TRANSMIT DATA 2├────────────────────>│3 RECEIVED DATA │
│ RECEIVED DATA 3│<────────────────────┤2 TRANSMIT DATA │
│ RTS 4├────────────────────>│5 CTS │
│ CTS 5│<────────────────────┤4 RTS │
│ DSR 6│<────────────────────┤20 DTR │
│ DTR 20├────────────────────>│6 DSR │
│ GROUND 7│<───────────────────>│7 GROUND │
└──────────────────┘ └──────────────────┘
Figure 4-1. Null Modem Connection
---------------------------------
5. Programming The UART
Table 5-1 shows all the UART registers which is addressed for COM1.
Please reference the PORT SPECIFICATION for more informations.
Table 5-1. Accessing the UART Registers
┌───────────────────┬───────────────┬────────────────────────────────────┐
│ I/O Port Address │ Read/Write │ Descriptions │
├───────────────────┼───────────────┼────────────────────────────────────┤
│ 3F8h* │ Write │ Transmitter Holding Register │
│ 3F8h* │ Read │ Receiver Data Register │
│ 3F8h% │ Write │ Baud-Rate Divisor (LSB) │
│ 3F9h% │ Write │ Baud-Rate Divisor (MSB) │
│ 3F9h* │ Write │ Interrupt-Enable Register │
│ 3FAh │ Read │ Interrupt-Identification Register │
│ 3FBh │ Write │ Line-Control Register │
│ 3FCh │ Write │ Modem-Control Register │
│ 3FDh │ Read │ Line-Status Register │
│ 3FEh │ Read │ Modem-Status Register │
└───────────────────┴───────────────┴────────────────────────────────────┘
* Bit 7 of the Line-Control Register = 0
% Bit 7 of the Line-Control Register = 1
The UART's clock input runs at 1.8432 MHz. The value for
Baud-Rate-Divisor Registers are calculated as follows:
1,843,200 115,200
----------- = ----------- = Divisor
2 * 8 Baud Rate
Table 5-2 shows the Baud-Rate-Divisor values that are supported by the
BIOS Asynchronous Serial Communication Services.
Table 5-2. Baud-Rate-Divisor Values
┌───────────────────┬───────────────────────────────────────┐
│ Desired Baud Rate │ Value for Baud-Rate-Divisor Registers │
├───────────────────┼───────────────────────────────────────┤
│ 110 │ 0417h │
│ 150 │ 0300h │
│ 300 │ 0180h │
│ 600 │ 00C0h │
│ 1200 │ 0060h │
│ 2400 │ 0030h │
│ 4800 │ 0018h │
│ 9600 │ 000Ch │
└───────────────────┴───────────────────────────────────────┘
6. BIOS Asynchronous Serial Communication Services
The BIOS Asynchronous Serial Communication Services initialize serial
communications port, send character, receive character, and return the
status of the serial ports.
All functions require a serial port number in DX (0 through 1). This
serial port number indexes a table in BIOS data area which contains the
port base addresses for each of the two corresponding serial ports.
The power-on self test routine determines whether a serial port adapter
is active at ports 3F8h, and 2F8h, and, if it detects an adapter
installed, it stores its port address in the table.
All the Asynchronous Serial Communication functions return a status
byte in AH. The most significant bit is supplied by the Send Character
and Receive Character functions to signal a timeout error: the serial
port will not be ready after the character was sent or received, and
the timeout count expired. All three functions are listed as follows:
function:
ah = 000h Initialize the communications port
ah = 001h Send character
ah = 002h Receive a character
ah = 003h Return the communications port status
ah = 004h Extended initialize
ah = 005h Extended communications port control
ah = 006h - 0ffh - reserved - null return
Note: Function 4 and 5 are only for PS/2 products.
input:
ah - function number
dx - communications port number (0,1,2,3)
output:
ah = status
bit 7 = time out
bit 6 = trans shift register empty
bit 5 = tran holding register empty
bit 4 = break detect
bit 3 = framing error
bit 2 = parity error
bit 1 = overrun error
bit 0 = data ready