RS485

Simple serial communications as a UART send digital signals from one device to an other. Both devices look at their ground to determine if the signal is high or low. The problem is that the devices have different grounds. There is a ground potential difference or common mode voltage between those grounds. This happens due to the currents flowing and has especially high frequency disturbance to to switching currents in the system.

To be tolerant against common mode noise (-7 to 12V), also a inverted B signal is sent and the receiver looks just at the difference between signal A and inverted signal B.

For RS485 this difference must be bigger than 0.2V for receivers. The drivers produce voltage difference bigger than 1.5V under load. The signal voltage to ground are smaller than the power supply voltage of the RS485 chip (3.3V or 5V).

If A>B then the Receiver gets a 1 at its Receiver Output RO and indicates idle to an attached UART. If A<B then 0

If the difference is too small then it is not defined. This might be the case when all RS485 drivers are disabled or on failures. To still get a defined output some RS485 drivers have fail safe features to get a high level. If not one RS485 device should have a 4.7 resistor from A to Vcc and one from B to Gnd. It is also required to add 120R bus terminator resistor on the cable ends (resulting in 60R). Those resistors then produce in idle condition a high level on the receiver outputs.

The receivers have a Receiver Enable input RE~. If set to 0 then the receiver receives always a 1 and gets also the echo from its driver. If set to 1 it gets a 1 the same output as idle.

RS485 is used in half duplex mode. so there are multiple drivers but just one is allowed to send. The drivers have a Driver Enable signal DR. If set to 1 the put their input Driver Input DI to the output.

It is a challenge to control the Driver Enable signal so no collision occurs but also all data is sent. Different methods exist:

Direction control with RS485 hack

Many simple HATs as the Waveshare CAN HAT use just RX and TX and no additional hardware signals to control the driver. Such devices can not be recommended. However it is helpful to understand how they work.

In bus idle condition the resistors produce a bus voltage that is safely producing a 1 at the receivers outputs. The receivers are always enabled and will also produce an echo of what will be sent.

When the driver input is 0 the driver adds a low voltage on A and a hight voltage on B and producing a clean 0 on the receivers. If the driver input is 1, the driver will be disabled and the resistors produce a high voltage on A and a low voltage on B. The time required to switch from 0 to 1 will be much longer when the resistors are producing a 1. The driver transistors would be obviously much faster. Therefore this solution will fail on high speed and high bus load.

The wiring of the RS485 chip puts the Driver Input constantly to 0 and feeds an inverse TX signal to:

  • the Driver Enable input to send the 0

  • the Receiver Enable input to prevent echo

Analog Devices MAX13487E with AutoDirection control

The MAX13487E and MAX13488E work similar to the Waveshare CAN HAT hack and just need TX and RX and no other signals. However being professional, the drivers include a state machine looking at the 0 to 1 edge of the driver input. If this occurs they turn on their output transistors to quickly change the voltages on the A and B pins. Once there is a safe 1 level for the receivers the output transistors are disabled and the external resistors hold the bus in a condition providing a 1 level on the receivers.

Important

The RE~ pin works differently on this chip. There is also an internal RE signal coming from the state machine and the external RE~ from the pin. Either signal can enable the Receiver. To have just the state machine controlling RE and turn of local echo the external RE~ must set to 1 as disable the receiver. If the external RE~ is set to ground then the Receiver is always enabled.

Those chips have no DE pin, since the DE signal is produced internally by the state machine. The have a SHDN~ input instead that allows the device to be turned off when 0.

Do not connect a pull down resistor (or a voltage divider to convert 5V to 3.3V signals) to the receiver output. If the DE is on, as when sending a 0 the receiver is off. But a receiver off means a high impedance output. if there is a pull down resistor then a 0 appears at the receiver output pin. If the driver sends a 1, DE is on until the idle condition appears and then turned off, so the receiver turn on. The receiver puts then a 1 at its output pin. The result is the voltage on the receiver output follows the drivers signal, this might be falsely interpreted as the receiver gets never disabled. Therefore do not connect a pull down resistor to the receiver output. If it is desired that the receiver output follows the driver, simply connect RE~ to ground.

Control direction by software

Controlling by software the pins to control the direction is not that easy since UARTs have FIFO's and finally a Transmit Shift register TSR. If the software has passed the data to the hardware it can not disable the output driver and must be wait until data is sent out.

RTS using the UART

Some UARTs and kernel drivers support RS485 mode TIOCSRS485 for this.The RTS signal can be feed directly to DE/RE~

import serial

ser = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=1)

ser.rs485_mode = serial.RS485Settings(
    rts_level_for_tx=True,  
    rts_level_for_rx=False,  
)

ser.write(b"Hello")

RTS using the driver software

If the UART and its kernel driver (as Prolific PL2303) do not support RS485 then the pyserial submodule rs485 can control RTS

import serial
import serial.rs485

ser = serial.rs485.RS485("/dev/ttyAMA0", 115200, timeout=1)
ser.rs485_mode = serial.rs485.RS485Settings(
    rts_level_for_tx=False,
    rts_level_for_rx=True,  
    delay_before_tx=0.0,
    delay_before_rx=0.0001  # seconds as float
)
ser.write(b"Hello")

Best practice is having a small program that sends out a byte every second and attach an oscilloscope. adjust theBSS138 delay_before_rx value until the RTS signal covers the byte.

RTS using the application software

The direction pin could also be controlled entirely by the application software:

import serial, time
from gpiozero import OutputDevice

de = OutputDevice(17, initial_value=False)   # DE/RE~
ser = serial.Serial("/dev/ttyAMA0", 115200, timeout=1)

def send(data: bytes):
    de.on()                     # send direction
    ser.write(data)
    ser.flush()                 # wait until done
    time.sleep(10 / ser.baudrate)   # wait again safely
    de.off()                    # receive direction

An other simple way is waiting for the echo, obviously the RE~pin must be wired to gnd:

def send(data: bytes):
    de.on()
    ser.write(data)
    echo = ser.read(len(data))   # read the echo
    de.off()

Just read the echo and do nothing with it is sad. It could be used to detect errors:

import serial
ser = serial.Serial("/dev/serial0", 57600, timeout=0.05)

def send_checked(data: bytes) -> str:
    ser.reset_input_buffer()
    ser.write(data)
    ser.flush()
    echo = ser.read(len(data))
    if len(echo) == 0:
        return "No Echo"
    if len(echo) < len(data):
        return f"Echo not complete{len(echo)}/{len(data)}"
    if echo != data:
        return "Echo has different data"
    return "ok"

Important

The sender must switch back to receive before the receiver has prepared the response and sends back


Linurs startpage