The puzzle
A UART link to a sensor 30 m away works in the lab and fails on the factory floor whenever a motor starts. A second sensor on the same cable would be convenient, but a UART is strictly point to point. How do industrial and automotive systems run many devices over long, noisy cables?
STEP 1
Why single-ended links fail over distance
A single-ended receiver compares one wire with its own ground. Over a long cable, two things add to the signal:
- noise coupled into the wire from motors, switching supplies and neighbouring cables;
- ground difference: currents flowing in the ground connection make the two ends’ grounds differ by volts.
Both land directly on the level the receiver sees; once a 0 rises past the input’s low threshold, bits are misread.
STEP 2
Differential signalling
A differential driver sends each bit on a pair of wires, one going up while the other goes down. The receiver measures only the difference between them. Noise coupled onto a tightly twisted pair appears almost equally on both wires (common mode) and cancels in the difference; a ground shift also moves both wires together. The receiver’s common-mode range, from its datasheet, bounds how far both wires may move together.
STEP 3
Termination
On a long cable, an edge travels as a wave. At an unterminated end it reflects and returns, ringing across the following bits. A resistor at each end of the bus equal to the cable’s characteristic impedance (typically about 120 Ω for the twisted pairs used by RS-485 and CAN) absorbs the wave. Terminate the two physical ends only: termination in the middle loads the bus, and stubs to each node should be short.
STEP 4
RS-485: a multipoint UART
RS-485 defines differential drivers and receivers for multipoint links; the data is usually ordinary UART frames. On a two-wire (half-duplex) bus only one node may drive at a time, so each transceiver has a driver enable, typically on a GPIO or the UART’s RTS pin. The sequence for sending is:
- enable the driver (some transceivers need a short delay before the first bit);
- send the frame;
- wait until the last stop bit has left the shift register;
- disable the driver, so the reply is not blocked.
Linux exposes exactly these controls: RTS level on send and after send, and delays before and after. A protocol above RS-485, such as Modbus RTU (lesson 6), provides the addressing; the bus itself has none.
STEP 5
CAN: arbitration without collisions
CAN is a differential multi-controller bus in which any node may start sending when the bus is idle. A 0 is dominant and a 1 recessive: if any node drives 0, the bus reads 0. After a start-of-frame bit, frames send an 11-bit (or 29-bit) identifier, most significant bit first (a few control bits after it also take part in arbitration), and every node listens while it sends. A node that sends recessive but reads dominant has lost arbitration: it stops and retries after the frame. The frame with the lowest identifier continues without damage or delay.
On a CAN bus a 0 is dominant and a 1 recessive: if any node sends 0, the bus reads 0. Nodes that start a frame at the same time send their identifiers bit by bit, most significant first, and each listens to the bus. A node that sends a recessive 1 but reads a dominant 0 has lost; it stops and retries after the frame. The frame with the lowest identifier continues undamaged, so identifiers set priority and must be unique on the bus.
Identifiers are therefore priorities, not addresses: every node receives every frame and filters the identifiers it wants. Two nodes must never send the same identifier, because both would win arbitration and then collide in the data. A classic CAN frame carries up to 8 data bytes (CAN FD up to 64).
STEP 6
Staying in step, and staying out of the way
CAN has no clock wire; nodes resynchronise on edges, and after five equal bits the sender stuffs an opposite bit (lesson 1). Every node counts errors. A node whose error counters exceed 127 becomes error passive and may no longer destroy other frames with active error flags; if its transmit errors keep rising, it goes bus-off and stops driving the bus entirely. It may rejoin only after seeing 128 occurrences of 11 recessive bits, and many controllers wait for software to request that recovery. A single faulty node cannot hold the whole network hostage.
STEP 7
Worked example: the RS-485 turnaround bug
A node at 115 200 baud sends an 8-byte request and disables its driver as soon as the transmit FIFO reports empty. The request arrives with its last byte corrupted. The FIFO was empty, but the last byte was still in the shift register, a full frame of
and the driver was switched off while it was going out. Waiting for the UART’s BUSY flag to clear (the pico-sdk’s uart_tx_wait_blocking()) before disabling the driver fixes it. Waiting much longer causes the opposite failure: the device’s reply starts while this node is still driving the bus.
MYTHS AND FACTS
Common misconceptions
Twisting the wires is optional
Twisting makes the coupled noise equal on both wires; without it the difference picks up noise too.
More termination is safer
Exactly two resistors, at the two ends; extra ones load the drivers.
The CAN identifier is the node address
It names the message and sets its priority; any node may receive it.
A differential link needs no ground
The receiver still needs both wires within its common-mode range, which usually means a ground or reference connection.
Check yourself
Answer in your head, then open the card.
Nodes with identifiers 0x120, 0x123 and 0x301 start sending at once. Which wins, and when does each loser back off?
0x120 wins. 0x301 backs off at bit 9 (it sends 1 while the others send 0); 0x123 backs off at bit 1, where it sends 1 and 0x120 sends 0.
Why must an RS-485 driver be disabled promptly after the last stop bit?
On a half-duplex bus the other node may reply immediately; a driver still enabled would fight its reply.
A 50 m RS-485 bus has termination resistors at both ends and a third one at a node in the middle. What happens?
The three resistors load the drivers more heavily and reduce the differential voltage; the middle one also causes a reflection. Remove it.
A CAN node with a faulty transceiver keeps corrupting frames. What happens to it and to the rest of the network?
Its error counters rise: above 127 it becomes error passive, and with further transmit errors it goes bus-off and stops driving the bus. The other nodes carry on.
Sources (4)
- Linux kernel, Documentation/driver-api/serial/serial-rs485.rst — RS-485 “a standard defining the electrical characteristics of drivers and receivers for use in balanced digital multipoint systems … used effectively over long distances and in electrically noisy environments”; half-duplex direction control via RTS: SER_RS485_RTS_ON_SEND, SER_RS485_RTS_AFTER_SEND, delay_rts_before_send, delay_rts_after_send, SER_RS485_RX_DURING_TX
- Zephyr Project, doc/hardware/peripherals/can/controller.rst — 11-bit or 29-bit identifiers; “write collisions are allowed. They resolve by the fact that dominant bits override recessive bits … This effectively gives lower number identifiers priority”; sample point recommended at 87.5 %; error counters: above 127 “error-passive”, then bus-off, where a node may not send dominant bits and “may recover after receiving 128 occurrences of 11 concurrent recessive bits”
- Linux kernel, include/uapi/linux/can.h and Documentation/networking/can.rst — CAN_SFF_MASK 0x7FF (11-bit), CAN_EFF_MASK 0x1FFFFFFF (29-bit), CAN_MAX_DLEN 8, CANFD_MAX_DLEN 64; can.rst: CAN is “a broadcast-only(!) medium that has no MAC-layer addressing … the CAN-IDs have to be chosen uniquely on the bus”
- Raspberry Pi Ltd, pico-sdk 1.5.1, hardware_uart/uart.h and rp2040/hardware_regs/uart.h — uart_tx_wait_blocking() loops while UARTFR BUSY is set; BUSY “remains set until the complete byte, including all the stop bits, has been sent from the shift register”