## Saturday, April 19, 2008

### Asynchronous Serial Communication- RS232, RS422 and RS485

In serial communication, data bytes are sent bit by bit. Its advantage is that data can be sent using only one wire instead of using many parallel wires. But it needs a UART to convert data byte to serial bit stream.
UART not only converts data into serial bit stream, but it also format the bit stream into Asynchronous Serial Communication Protocol by inserting start bit, parity bit, stop bits, etc.

In idle state, a UART normally puts its output in 1. The receiving UART needs to differentiate whether the 1 is data to receive or idle state to ignore. Start bit is used to solve this problem. Normally, a UART ignore 1 as idle. After it received the first 0 as start bit, it accepts the following bits as data for predefined number of bits.

The rate of sending data unit is called Baud rate. For example, if the baud rate is 1 kHz, time period to send each data bit is 1/(1kHz)= 1 ms. A popular baud rate is 9600.

The number of data bits for sending UART and receiving UART must be the same. It can be from 5 bit data to 8 bit data. For example, ASCII data are normally sent with 7 bits. LSB is sent first. A popular number of data bits is 8.

Parity bit is used to check the integrity of the data. In even parity, the number of 1 in data bits and the parity bit itself is even. In odd parity, it is odd. Typically, parity bit is not used.

Stop bit is used to separate the consecutive data bytes. The number of stop bit can be 1,2, etc. 1 is used as a stop bit. Stop bits are sent after sending data bits and parity bits. A popular number of stop bit is 1.
The following example send the data byte 0x41 using 9600, 8N1. That means
Baud rate = 9600
Data bit = 8 bits
Parity = No parity (N= No parity, E= Even parity, O= Odd parity)
Stop bit= 1 stop bit
Since it is no parity, stop bit will come immediately after data bit without any parity bit.

### Forms of UART

A UART can be in many different forms. The first one is in the form of a discrete IC. A popular IC is 16550. For CMOS, it will become 16C550, and so on. It has its own buffer and interrupt mechanisms. Therefore, it is easy to use. Serial data comes out from sout pin. Its basic configuration is as shown in the following diagram.

Another form of UART is built-in UART within a microcontroller. The last form of UART is software UART that uses IO pins to emulate it.

### RS232

A UART uses 5V output to represent 1 and uses 0V output to represent 0. To connect to external devices, these output voltages are normally converted into suitable signaling levels by using a transceiver. A popular signaling standard is RS232. A RS232 transceiver outputs or inputs -10V for 1 and +10V for 0. Valid voltage range is from +/- 3V to +/-25V. A popular transceiver IC is MAX232. Its configuration is shown in the following diagram.

If you want to use smaller capacitor (0.1uF) and smaller IC, you can use MAX202. To use MAX202 with 16C550, connect sout pin of UART which produces 0V and 5V to its pin 11. Then Tx will come out from pin 14 as +10V and -10V. Similarly, +10V and -10V received from Rx line that is connected to pin 13 will come out as 0V and 5V from pin 12 which connects to sin of UART.
The following figure shows typical connection for RS232.

You can use a multimeter to differentiate Tx and Rx of opened RS232 lines. Typically, the voltage between Tx and Ground is -10V and there is no voltage between Rx and Ground. RS232 is said unbalanced lines.

In RS232 communication, besides data transmit line (Tx) and data receive line (Rx), there are also control and status lines for handshaking. Among Primary Communication lines, Tx and RTS are outputs and Rx and CTS are inputs. Among Status and Control lines, DTR is output while DSR and CD are inputs. They will be discussed further. When we talk about RS232 devices, DTE ( Data Terminal Equipment) refers to computer and DCE (Data Circuit-Terminating Equipment) refers to Modem or device.
RTS- Request To Send signal is requested by DTE to DCE. It is normally logic 1 (negative voltage) and when requested it becomes logic 0 (positive voltage).
CTS- Clear To Send signal is used by DCE to infrom DTE that it is ready to receive. It is normally logic 1 (negative voltage) and it becomes logic 0 (positive voltage) when asserted.
DSR- DCE Ready (Data Set Ready) is used by DCE (device) to inform DTE that it has turned on and is ready. It is normally logic 1 (negative voltage) and it becomes logic 0 (positive voltage) when asserted.
DTR- DTE Ready is asserted by DTE to inform DCE that it want to communicate. It is normally logic 1 (negative voltage) and it becomes logic 0 (positive voltage) when asserted.
CD- Carrier Detect (Received Line Signal Detector) is used when DCE is a modem. When this modem receives the answer tone of a modem at the other side of the telephone line, it asserts the computer with logic 0.
RI- Ring Indicator is asserted when a modem receives ring signal. It outputs logic 0 to the computer. It is positive only when there is ring signal. It is negative between ring signals and when there is no ring signal.
Let us continue to discuss about RS232 wire connectors.

As shown in the following figure, a computer (DTE) has Male DB9 connector for RS232 and a device (DCE) has Female DB9 connector. Pin connection for a male connector is shown.
pin 1 = CD- Carrier Detect (input)pin 2 = Rx- Receive Data (input)pin 3 = Tx- Transmit Data (output)pin 4 = DTR- Data Terminal Ready (output)pin 5 = GND- System Groundpin 6 = DSR- Data Set Ready (input)pin 7 = RTS- Request To Send (output)pin 8 = CTS- Clear To Send (input)pin 9 = RI - Ring Indicator (input)


Pin connection of a female connector is also shown below.
pin 1 = CD- Carrier Detect (output)pin 2 = Tx- Transmit Data (output)pin 3 = Rx- Receive Data (input)pin 4 = DTR- Data Terminal Ready (input)pin 5 = GND- System Groundpin 6 = DSR- Data Set Ready (output)pin 7 = CTS- Clear To Send (input)pin 8 = RTS- Request To Send (output)pin 9 = RI - Ring Indicator (output


### RS422 and RS485

RS422 and RS485 function using the difference voltage between two wires. Transmit voltage is between +12V and -12V and they can receive even if the voltage difference is about 0.4V. The voltage between +Tx and -Tx is output voltage and the voltage between +Rx and -Rx is input voltage. They are called balance line because they don't use the voltage between ground and transmit line. They have more resistance to common mode noise. Therefore, they can communicate faster and can communicate over longer distance.

Note:
-200mV to +200mV = 0.4 V
-6V to +6V = 12V
Both RS232 and RS422 can have many receivers on a single bus line but they can have only one transmitter on a bus line. In RS485, transmit driver circuit can be enabled and disabled by using data enable line. As a result, it can have many receivers and transmitters on a bus line. Normally, RS422 uses two wires (+Tx, -Tx) for transmitting and another two wires (+Rx, -Rx) for receiving. Therefore, RS422 uses total of four wires. RS485 can communicate using only two wires for both transmitting and receiving in half duplex mode. RS485 can also be used in Full duplex mode using four wires. The following figure shows a typical connection for RS485 using MAX481 transceiver IC. It has only two wires to use in half duplex mode.

An example using MAX491 to connect RS485 in full duplex mode is shown below. If you connect +Tx and +Rx and at the same time -Tx and -Rx, it can be used in half duplex mode.

Actually, full duplex RS485 with four wires whose transmit driver circuit always enabled is same as RS422. RS422 is normally used to extend distance of RS232 communication. When the distance is too far to use RS232, RS422 converter can be used in transmitting side and receiving side can convert the signal back to RS232.
In case, you want to use RS422/RS485 input device with RS232 output device without converter, you can directly connect RS232 Tx output to RS422/RS485 -Rx and RS232 GND to +Rx line. I tried it once before and it was OK. RS422, RS485 half duplex and RS485 full duplex connections are shown below. The first one is RS422 which can be used for only one to one connection. There can be many receivers, but there can only be one transmitter in a bus line. We cannot control transmit driver.

Note that RS485 half duplex has driver enable and receiver enable. Typically, there can be up to 32 node in a bus line.

RS485 full duplex.