RS232/422/485 Serial Communications
Users Manual
(ASYNC)
Version 2.5
June 19, 2000
(Revised August 7, 2000)
This software is provided as-is.
There are no warranties, expressed or implied.
Copyright (C) 2000
All rights reserved
MarshallSoft Computing, Inc.
Post Office Box 4543
Huntsville AL 35815 USA
Voice : 256-881-4630
FAX : 256-880-0925
email : info@marshallsoft.com
web : www.marshallsoft.com
MarshallSoft is a member of the Association of Shareware Professionals
MARSHALLSOFT is a registered trademark of MarshallSoft Computing.
1 The UART
1.1 UART Types2 Modems
1.1.1 National 82501.2 UART Operation
1.1.2 National 16450
1.1.3 National 16550
1.1.4 StarTech 16650
1.1.5 Texas Instruments 16750
1.3 RS-232 Signals
1.4 UART Registers
1.5 Register Summary
2.1 Modem Standards3 RS422 and RS485
2.1.1 Speed2.2 Modem AT Command Set
2.1.2 Data Compression
2.1.3 Error Control
2.3 Flow Control
2.4 Modem Initialization
2.5 Modem CONNECT
2.6 More Modem Documentation
4 Other Serial Devices
The heart of serial communications is the "Universal Asynchronous Receiver Transmitter", or UART for short. The UART is responsible for controlling the computer's RS-232/422/485 port.
UARTs can be broken down into two classes - buffered and unbuffered. The 8250 and 16450 are unbuffered, while the 16550, 16650, and 16750 are buffered.
The National 8250 was the original UART used in the IBM PC and compatibles, and are only suitable up to 9600 baud on slower DOS (before the IBM/AT) machines. The 8250A is somewhat faster than the 8250, but should also be limited to slower DOS machines.
The National 16450 was designed to work with the IBM PC/AT (16-bit data bus) and faster machines. Faster than the 8250, it still has a one-byte buffer. The 16450 chip is suitable for many DOS applications and some Windows applications up to about 38400 baud.
The National 16550 features 16-byte transmit side and receive side FIFO buffers. The interrupt trigger level (on the receive side) can be set at 1, 4, 8 or 14 bytes. The TX FIFO trigger is fixed at 16 bytes.
The FIFOs on the original 16550 UARTs did not work. The 16550A soon followed the 16550 and is the "standard" UART for Windows machines.
The 16550A is recommended as the minimum chip on any new serial board purchase.
The StarTech 16650 features 32-byte FIFOs and on-chip flow control, and can be run at up to 460800 baud. It is also pin for pin compatible with the 16550 UART.
The Texas Instruments 16750 features 64-byte FIFOs and on-chip flow control and can be run at up to 921600 baud, but is not pin for pin compatible with the 16550 UART.
The purpose of the UART is:
The first bit is always the start bit, followed by 5 to 8 data bits, (optionally)
followed by the parity bit, followed by the stop bit or bits.
The UART is capable of operating in one of two modes, 'polled' and 'interrupt driven'. The serial communications functions in the BIOS use the polled method. In this approach, the CPU is typically in a loop asking the UART over and over again if it has a byte ready. If a byte is ready, the polling code returns the byte. But, if the next byte comes in before the polling code is executing again, then that byte is lost.
In the interrupt driven approach, when a byte is received by the UART, an 'Interrupt Service Routine' (ISR) is executed immediately, suspending temporarily whatever is currently executing. The ISR then moves the byte from the UART to a buffer so that the application program can later read it.
The 16550 can be programmed so that a receive (RX) interrupt is not triggered until 4 (or 8 or 14) bytes have been received, while the 16650 can be triggered at up to 30 bytes, and the 16750 can be triggered at up to 56 bytes. This can significantly reduce the CPU processing time, since 14 (or 30 or 56) bytes can be moved at once.
Transmitted bytes are queued up awaiting transmission. When a byte is moved from the UART transmitter holding register to the UART transmitter shift register, an interrupt is generated and the next byte is taken from the transmitter buffer by the ISR and written to the UART holding register.
Up to 16 bytes can be written at once to the transmitter FIFO buffer while processing one transmitter interrupt if an 16550 UART is used, while the 16650 can write up to 32 bytes at once, and the 16750 can write up to 64 bytes at once.
RS-232 is the name of the serial data interface standard used to connect computers to modems.
A summary of the serial port pins and their function follows. For more detailed information, refer to one of the many books dealing with RS-232 interfacing.
The SG line is used as the common signal ground, and must always be connected.
The TX line is used to carry data from the computer to the serial device.
The RX line is used to carry data from the serial device to the computer.
The DTR line is used by the computer to signal the serial device that it is ready. DTR should be set high when talking to a modem.
The DSR line is used by the serial device to signal the computer that it is ready.
The RTS line is used to 'turn the line around' in half duplex modems, and for hardware flow control in most modems that require flow control. RTS is controlled by the computer and read by the serial device (modem).
The CTS line is used to 'turn the line around' in half duplex modems, and for hardware flow control in most modems that require flow control. CTS is controlled by the serial device (modem) and read by the computer.
The DCD line is used by the modem to signal the computer that a data carrier signal is present.
The RI line is asserted when a 'ring' occurs.
Data sheets can be obtained from the following sources on the Internet.
[UART] [URL] 16550 : www.national.com and www.exar.com 16650 : www.exar.com 16750 : www.ti.com These UARTs consists of 8 register ports as follows: [REGISTER] [DESCRIPTION] Reg 0 R/W : Receiver (read) / Transmitter (write) Reg 1 R/W : Interrupt Enable Register (IER) Reg 2 R : Interrupt Identification Register (IIR) Reg 2 W : FIFO Control Register (FCR : 16550/650/750) Reg 2 R/W : Enhanced Features Register (EFR : 16650 Only) Reg 3 R/W : Line Control Register (LCR) Reg 4 R/W : Modem Control Register (MCR) Reg 5 R/W : Line Status Register (LSR) Reg 6 R/W : Modem Status Register (MSR) Reg 7 R/W : Scratch register. Not used.
For the standard PC ports, the UART registers are based at 3F8h (COM1), 2F8h (COM2), 3E8h (COM3), and 2E8h (COM4). COM1 and COM3 share interrupt request line IRQ4 while COM2 and COM4 share request line IRQ3. This means that COM1 and COM3 can't be used concurrently. Similarly for COM2 and COM4.
Four sources of interrupts are possible. (1) receiver error or BREAK, (2) receiver data ready, (3) ready to transmit, and (4) RS-232 input. Additional sources can be generated by the 16650 (see 16650 spec sheet).
These four sources of interrupts are summarized as follows:
[Source of Interrupt] [Action Required to Clear] Receiver error or BREAK. Read Line Status register. Receiver data. Read data from data register. Transmitter Buffer Empty. Write to data register or read IID reg. RS-232 input. Read Modem Status register.Serial ports are configured as either Data Communications Equipment (DCE) or Data Terminal Equipment (DTE). Modems are always configured as DCE, while serial printers are (almost) always configured as DTE. Most serial (computer) ports are designed to talk to modems and are therefore configured as DTE. Serial (computer) ports designed to talk to serial printers (rare today) are configured as DCE. Most other serial device are configured as DCE. A normal RS-232 cable is used to connect two serial ports with opposite configuration (DTE & DCE), while a null modem cable is used to connect two serial ports of the same configuration (both DTE or both DCE).
[BIT] [DESCRIPTION] bit 3 : Enable interrupt on RS-232 input. bit 2 : Enable interrupt on receiver error or break. bit 1 : Enable interrupt on transmitter buffer empty (TBE). bit 0 : Enable interrupt on received data (RxRDY).
[Bit 2] [Bit 1] [Bit 0] [Priority] [Interrupt] 0 0 1 none none 1 1 0 0 (high) Serialization or break. 1 0 0 1 Received data. 0 1 0 2 Transmitter Buffer Empty. 0 0 0 3 (low) RS-232 Input.
[Bit 7] [Bit 6] [16550 Trigger] [16650 Trigger] [16750 Trigger] 0 0 1 byte 8 bytes 1 byte 0 1 4 bytes 16 bytes 16 bytes 1 0 8 bytes 24 bytes 32 bytes 1 1 14 bytes 28 bytes 56 bytesThe TX FIFO level can also be set in the 16650 by setting bits 4 & 5. The 64-byte FIFO mode on the 16750 can be enabled by setting bit 5 in the FCR. See the 16650 & 16750 data sheets for more details.
[BIT] [DESCRIPTION] bit 7 : DLAB = 0 bit 6 : BREAK on(1), off(0). bits 5-3 : Parity None(000),ODD(001),EVEN(011),MARK(101),SPACE(111) bit 2 : One stop bit(0), two stop bits(1). bits 1-0 : Data bits = 5 (00), 6(01), 7(10), 8(11). [PARITY] [DESCRIPTION] Odd : The parity bit is 1 if the sum of the data bits is odd. Even : The parity bit is 1 id the sum of the data bits is even. None : There is no parity bit. Mark : The parity bit is always set to 0. Space : The parity bit is always set to 1.When the Divisor Latch Access Bit (DLAB) is 1, registers 0 and 1 become the LS and MS bytes of the Baud Rate Divisor registers. The baud rate is computed as (115200 / BaudRateDivisor). Thus, common baud rates correspond to divisors as follows:
[BAUD] [DIVISOR] [BAUD] [DIVISOR] [BAUD] [DIVISOR] 300 0180 4800 0018 38400 0003 1200 0060 9600 000C 57600 0002 2400 0030 19200 0006 115200 0001NOTES:
RTS, DTR, loopback testing, and General Purpose Outputs #1 and #2 are controlled by the Modem Control register as follows:
[BIT] [DESCRIPTION] bit 7 : Clock select. X1 (if 0), X4 (if 1). [16750 ONLY] bit 6 : IR enable [16650 ONLY] bit 5 : Interrupt type select [16650 ONLY] or Flow control enable [16750 ONLY]. bit 4 : Enable local loopback. bit 3 : Enable GP02. Necessary for UART interrupts. bit 2 : Enable GP01. bit 1 : Set / clear RTS. bit 0 : Set / clear DTR.
[BIT] [DESCRIPTION] bit 7 : FIFO data error [16650 & 16750 ONLY]. bit 6 : Transmitter Empty (TXE). bit 5 : Transmitter Buffer Empty (TBE). bit 4 : BREAK detect. bit 3 : Framing error. bit 2 : Parity error. bit 1 : Overrun error. bit 0 : Data Ready.
[BIT] [DESCRIPTION] bit 7 : DCD status. bit 6 : RI status. bit 5 : DSR status. bit 4 : CTS status. bit 3 : Delta DCD status. bit 2 : Delta RI status. bit 1 : Delta DSR status. bit 0 : Delta CTS status.The delta bits (bits 0 through 3) are set whenever one of the status bits (bits 4 through 7) changes (from 0 to 1 or from 1 to 0) since the last time that the Modem Status register was read. Reading the Modem Status register clears the delta bits.
The first AT command set was developed by Hayes, and allowed the programmer to communicate directly with the modem in "command mode" using the "AT command set". When a modem is first powered up, it enters "command mode", and will respond to the various AT commands that it recognizes. Once connected to another modem ("online" or "connected" mode), AT commands are not recognized.
Other modem manufacturers soon followed Hayes, typically making their command set upwardly compatible with the original Hayes command set. However, different modem manufacturers choose different AT commands for the same functionality, such as setting flow control. This lead to the present condition, in which each modem manufacturer has it's own command set with a core set of commands common to all modems.
The User's Guide for your modem should contain a list of the AT commands that it uses. If not, you should be able to get this information from the manufacturer. Most modem manufacturers have this information on their Internet site.
With modems using data compression, the modem to modem connection will run at various speeds depending on the quality of the line. The computer to modem connection will be at a fixed baud rate. Therefore, a protocol (flow control) is necessary to synchronize the data flow between a modem and the computer to which it is connected. Refer to your modem manual for information on flow control protocols supported.
Two flow control protocols are used by most all modems which require flow control. Software flow control is called 'XON/XOFF' (other software flow control character pairs are defined but operate the same as XON/XOFF) and hardware flow control is called 'RTS/CTS'. Most modems which require flow control enable hardware flow control by default.
In XON/XOFF (software) flow control, the computer suspends transmitting data if it receives a XOFF character (13 hex) from the modem, and continues transmitting when it receives a XON character (11 hex). Similarly, the computer can signal the modem not to send any more data by transmitting a XOFF to it, and can tell the modem to continue transmission be sending a XON.
In RTS/CTS (hardware) flow control, the RTS line is used by the computer to signal the modem , while the CTS line is used by the modem to signal the computer. The RTS line is set OFF by the computer to tell the modem to suspend transmission, and set to ON to tell the modem to continue transmission. The CTS line is set to OFF by the modem to tell the computer to stop transmitting, and set to ON to tell the computer to continue transmitting.
Given the choice, always choose hardware flow control over software flow control so that all data transmission is transparent. If hardware flow control is not the default (which it almost always is), you should modify your modem initialization string to turn hardware flow control on.
If an application uses a modem (as opposed to using a null modem cable), then it should always send an initialization string to the modem. Communication programs such as PROCOMM and TELIX always send such a string automatically as soon as they start up.
The particular initialization string depends on the make of your modem. For most modems, the following string (followed by a carriage return) should work:
AT E1 S7=60 S11=60 V1 X1 Q0 S0=0Recall that the modem must be in command mode in order to send AT commands. To force command mode, send the character string "+-+" (without the quotes), preceded by 1 second of silence, and followed by one second of silence. Refer to your Modem User's Guide for a full discussion of these commands. A brief description is as follows:
[COMMAND] [DESCRIPTION] AT : Modem attention command. E1 : Modem will echo what you send to it. S7=60 : Wait 60 seconds for carrier and/or dial tone. S11=60 : Use 60 milliseconds for tone dialing duration & spacing. V1 : Display result code as words (not numbers). X1 : Use the extended result message (CONNECT XXXX) set. Q0 : Modem displays result codes. S0=0 : Do not answer RING.If the application will answer incoming calls, set the S0 register to the ring on which to automatically answer. Most modems can be set to the 'factory default' by transmitting
AT&F
When dialing a modem (using the ATDT command) or when answering an incoming call, modems will typically respond with a "CONNECT xxxxx" string, where the "xxxxx" is the connect baud rate. Some modems, notably many WinModems, require that you change the UART baud rate (using SioBaud) to match the "xxxxx" value.
There is a lot of information regarding modems on the Internet, including a listing of the AT command set for most modems.
An excellent book covering a wealth of modem information is the Programmer's Technical Reference: Data and Fax Communications by Robert L. Hummel (Ziff-Davis Press).
RS422 and RS485 use the same UARTs as RS232. However, both RS422 and RS485 are based on balanced differential signals, as opposed to RS232 which uses unbalanced signal levels. In other word, RS422 uses the difference in voltage levels between 2 wires whereas RS232 uses the voltage level of a single wire with respect to a common signal ground.
RS422 and RS485 both require a pair of wires for every signal. RS422 is only usable in point to point systems.
RS485 has tri-state capability (its driver can be disabled) and can support up to 32 receivers (multidrop) on the same line.
RS485 can also be wired as "2-wire" in which the same pair of wires are used for both transmitting and receiving. Typically, RTS is set before transmitting and dropped after the last bit of the last byte is sent.
RS422 and RS485 may both require (for long run lengths) termination resistors and/or biasing resistors, which is beyond the scope of this discussion.
RS422 an RS485 are typically used in industrial settings where long run lengths (to
4000 feet) are necessary. For more information, refer to one of the many technical
references on the Internet such as:
To be sure, the modem is the most common serial device. But there are many other serial devices such as digitizing tablets, scanners, digital cameras, numerical control machines, card readers, panel displays, etc.
Some serial devices (such as modems) use hardware (RTS/CTS) flow control, but DTR/DSR flow control and software (XON/XOFF) flow control are also common.
If you are writing a program to communicate with a serial device, keep in mind the
following: