RS485, and the similar RS422, are communication bus systems using two wire differential transmission of each signal. Differential transmission improves the signal's immunity to electrical noise, at the expense of the requirement for an extra conductor for each signal - usually in a twisted pair configuration. Specifications only define the electrical properties of the system (ISO physical layer). There are no specific communications protocols, though many have been adopted.
RS485 is often used for serial data communications. The lack of a specific protocol means that several pre-existing ones have been adopted (with some adaptations to allow additional benefits of RS485 to be included), and new ones developed for specific applications.
RS485 can be used to transmit almost any digital signal that is within the frequency limits of the bus system. It is often used in industry to allow the transmission of sensor signals, rotary encoder pulses, and motor control signals around large machines. Large computer systems also use multiple, parallel, RS485/422 busses to connect high speed signals between cabinets.
Wiring and termination can be an important issue with many systems.
Terminating resistors are required at the ends of long cable runs to match the line impedance and prevent signal reflections. The terminating resistors can also be used to bias the signal lines so that a specific logic level is represented on the bus when it is not being driven by any of the nodes.
Common protocols:
RS485 can often be used to directly replace RS232 links - extending the transmission range and baud rate available. Many commercial products are available to perform this function.
An extension of RS232, available in UARTs of many microcontrollers, is the transmission of a 9th data bit. This is used in many RS485 systems to differentiate between two types of transmissions:
9th bit = 1, 8 bits = node address
9th bit = 0, 8 bits = data
Unlike RS232, which can only allow communication between two devices (officially), RS485 can allow communications between a small network of devices (maximum 32) using a multi-drop bus. Each node on the bus must have a unique address to allow information to be targeted at it. The bus transmitters in each device can be disabled, allowing other devices to transmit on the bus without causing conflict. The use of 9 data bits can allow packets of bytes to be transmitted. The first byte can be highlighted as the address of the intended recipient node by setting its 9th bit.
For simplicity, many RS485 networks have a single master node and multiple slave nodes. The master node initiates all communications and indicated the slave node being accessed by including its node addresses in the message.
RS485 does not have the in-built message collision detection and correction available in bus systems like CAN, but there are techniques to allow support for multi-master networks and unsolicited messages.
Network configurations:
A simple RS485 application is the direct transmission of the TX and RX signals generated by the UARTs of two microcontrollers. The use of standard RS485 drivers and twisted pair cables will allow the signals to be transmitted at higher baud rates, and over longer distances, than the use of RS232 drivers would allow. This is a full-duplex connection (allows simultaneous transmission in both directions) which requires a driver and receiver for each device (node), and four signal wires.
To reduce the wiring costs, a half-duplex connection can be used. This allows communication in either direction, but only one at a time, and requires a combined transmitter/receiver (transceiver) for each node. The ability to disable the RS485 transmitters allows each node to release the signal lines when not transmitting, allowing the other node to transmit if required. The handover of bus control from one node to another is the responsibility of the protocol being used.
The half duplex bus can be directly expanded to include more nodes if a suitable software addressing scheme is adopted. The full-duplex bus can also be expanded if the driver in each slave node has a controlled enable/disable signal.
The TX signal from the master node can be connected to the RX of each slave node, and the TX from each slave node can be connected to the RX of the master, with a maximum of one being enabled at any time.
A common device for RS485 conversion is the SN75176 (or compatibles). This is a transceiver available in an 8-pin dip package. It contains a transmitter and receiver with common RS485 connections, but separate logic level TX (D) and RX (R) pins, and individual transmit and receive enable pins. The transmit enable is active high, while the receive enable is active low, allowing the two signals to be connected together as a single 'Direction' control: High = transmit; Low = receive.
Example programs:
Two example programs are attached to this article:
MasterNode
SlaveNode1
These allow a simple, single master, multiple slave, network to be created. Initially 1 master and 1 slave are represented. The slave has node address 1 set in the program; the master does not require a node address.
The network uses 9-bit data transmission, with the addressing protocol described above. An address byte contains the target node address (bits 0 to 4) and a read/write bit (bit 7).
The master node writes a count value to node 1, which is displayed on the Port B pins of node 1.
The master node then requests a read from node 1. The slave node should respond by sampling its ADC1 channel and returning the value to the master node. The master node displays the returned value on its Port B pins.
The slave program can be copied into additional nodes on the network, which should be programmed with unique addresses between 2 and 31. The master can then be programmed to communicate with the new nodes be adding code to the existing program.
The only transfers supported by this example protocol are single read and write operations. The protocol can be expanded to allow additional message types to be handled, allowing greater control of the slave nodes.