The idea: one master asks, one slave answers

Modbus (Modicon, 1979) is the oldest and still the most common protocol in factories. A PLC (programmable logic controller) talks to sensors, motor drives and meters over one cable. The PLC is the master (the newer spec says client); the devices are slaves (servers). Only the master ever starts a conversation. A slave speaks only when it is asked, and then only once. Because of that one rule, the bus needs no arbitration at all.

In the serial form, Modbus RTU, the cable is an RS-485 twisted pair (wires A and B) with a 120 Ω terminator at each end. RS-485 sends each bit as the voltage difference between A and B, so noise that hits both wires cancels out, and a bus can run for about a kilometre. Each slave has an address from 1 to 247. The page uses 19200 baud with 8 data bits, even parity and 1 stop bit (8E1), the default in the Modbus serial line spec. Each byte is then 11 bits on the wire and takes 0.573 ms.

Demo 1 in time order. The PLC builds a request to read holding registers 0 and 1 of slave 01, with CRC 0x0BC4 sent as C4 0B. It sends the 8 bytes 01 03 00 00 00 02 C4 0B in 4.58 ms, and slaves 01, 02 and 03 all receive them. After 3.5 characters of silence (2.0 ms) the frame is complete. Slave 01 sees its own address and a good CRC; slaves 02 and 03 ignore the frame and stay silent. Slave 01 reads HR 0 = 235 and HR 1 = 500 and answers with the 9 bytes 01 03 04 00 EB 01 F4 8A 10 in 5.16 ms. The PLC checks CRC 0x108A and gets 23.5 °C and 50.0 %RH. After another 2.0 ms of silence it may send the next request.
Every slave hears every request, but only the addressed one answers, and the bus carries one transaction at a time.

The RTU frame and the 3.5-character silence

An RTU frame has only four parts: the slave address (1 byte), the function code (1 byte), the data (0 to 252 bytes) and a CRC-16 (2 bytes). There is no start byte and no length field. Instead, timing marks the frame: a frame begins after the line has been silent for at least 3.5 character times, and it ends when the line goes silent for 3.5 character times again. At 19200 baud that is 3.5 × 0.573 ms = 2.0 ms. Inside a frame, the bytes must follow each other with no gap longer than 1.5 characters (0.86 ms), or the receiver throws the frame away. Above 19200 baud the spec fixes these gaps at 1.75 ms and 0.75 ms.

The request 01 03 00 00 00 02 C4 0B drawn as eight byte boxes: address 01, function 03, start address 00 00, count 00 02, CRC C4 0B. Above it a time line: silence of at least 3.5 characters, 2.0 ms, then 8 bytes in 4.58 ms, then silence again.
Silence before and after, then address, function, data and CRC: 8 bytes, 4.58 ms at 19200 baud.

Inside the data, numbers are sent high byte first (big-endian): register 0x01F4 goes as 01, then F4. The one exception is the CRC.

CRC-16, low byte first

The sender computes a CRC-16 over every byte of the frame before the CRC: start with 0xFFFF; for each byte, XOR it into the low 8 bits, then 8 times shift right, and XOR with 0xA001 whenever a 1 fell out. 0xA001 is the polynomial 0x8005 written backwards, because serial lines send the lowest bit first. The result goes at the end, low byte first. For the request 01 03 00 00 00 02 the CRC is 0x0BC4, so the frame ends C4 0B. The receiver runs the same calculation over the bytes it got and compares. One flipped bit changes the CRC, so it is caught (Demo: noise breaks the CRC). See CRC for how the division works.

Frame on the pageBytes before the CRCCRCOn the wire
read slave 1, HR 0–101 03 00 00 00 020x0BC4C4 0B
its response01 03 04 00 EB 01 F40x108A8A 10
write 1500 to slave 202 06 00 00 05 DC0x308B8B 30
exception response01 83 020xF1C0C0 F1

Function codes 03 and 06

Modbus sees a device as a set of tables of 16-bit holding registers (and coils, inputs, input registers). The PLC's manual calls the first holding register 40001; on the wire it is address 0.

  • 03 Read Holding Registers: the request carries a start address and a count (here 0 and 2). The response carries a byte count (4) and the values, 2 bytes each (Demo: read 2 registers from slave 1). 0x00EB = 235 means 23.5 °C: devices often send a number times 10, because a register holds only whole numbers.
  • 06 Write Single Register: the request carries the register and the new value (1500 = 0x05DC). The slave answers with an exact echo of the request to say "done" (Demo: write 1500 rpm to slave 2).

When things go wrong

If a slave finds a wrong CRC, it drops the frame silently. It cannot even trust the address byte, so it must not answer. The master only notices that no answer came: after its response timeout (200 ms on the page; PLCs let you set it) it tries again (Demo: noise breaks the CRC → timeout, retry). A missing or switched-off slave looks exactly the same: silence.

If the frame is fine but the slave cannot carry out the request, it sends an exception response: its address, the function code with the top bit set (03 | 0x80 = 0x83) and one exception code (Demo: register 100 does not exist). That is a real answer, so the master does not retry; it reports a configuration error.

CodeNameMeaning
01ILLEGAL FUNCTIONthe slave does not support this function code
02ILLEGAL DATA ADDRESSthe register (or start + count) is outside its map
03ILLEGAL DATA VALUEa value in the request is not allowed, for example count 0
04SERVER DEVICE FAILUREthe slave failed while doing the work

The polling cycle

A PLC keeps a poll list and walks it forever: ask slave 1, wait for the answer or the timeout, ask slave 2, and so on (Demo: one polling cycle). One read here costs 4.58 ms for the request, 5.16 ms for the response and two 2.0 ms silences, about 13.8 ms, plus the slave's reaction time. Three slaves take about 41 ms per cycle. A slave that does not answer costs a whole timeout per try, so one dead device can slow every other reading on the bus.

Modbus TCP and the MBAP header

Modbus TCP carries the same requests over Ethernet. The client opens a TCP connection to port 502 (see the TCP handshake) and keeps it open. The message keeps the PDU (function code + data) exactly as on the serial line, drops the address and the CRC, and puts a 7-byte MBAP header in front:

  • transaction ID (2 bytes): chosen by the client and copied into the response, so several requests can be in flight at once;
  • protocol ID (2 bytes): always 0 for Modbus;
  • length (2 bytes): the number of bytes that follow (unit ID + PDU);
  • unit ID (1 byte): the old slave address, used when a gateway passes the request on to an RS-485 bus.
The same PDU, 03 00 00 00 02, in two wrappers. Top: Modbus RTU, address 01 in front and CRC C4 0B behind, 8 bytes. Bottom: Modbus TCP, MBAP header 00 01, 00 00, 00 06, unit ID 01 in front and nothing behind, 12 bytes, sent to TCP port 502.
The PDU is the same; RTU wraps it in an address and a CRC, TCP in an MBAP header and no CRC.

There is no CRC because TCP has its own checksum, Ethernet has a CRC-32, and TCP resends lost data (Demo: the same read as Modbus TCP).

Security: Modbus trusts everyone

Modbus has no authentication and no encryption. A slave answers any frame with its address and a good CRC, and a Modbus TCP server answers anyone who can reach port 502. Anyone with access to the bus or the network can read every value and write any register, for example a drive's speed setpoint. Modbus was designed for closed cables inside a plant, and that assumption is the weak point. Plants defend it from the outside:

  • keep control networks separate from office networks and the internet, in zones with firewalls between them (IEC 62443's zones and conduits);
  • allow port 502 only from the few hosts that need it, and use firewalls that understand Modbus and can block write function codes;
  • where devices support it, use Modbus/TCP Security: Modbus inside TLS with certificates, on port 802;
  • watch the traffic: an unknown client, or writes outside the usual pattern, are a reason to alarm.

See also the CAN bus, where there is no master and the lowest ID wins arbitration bit by bit, and the I2C bus, another addressed bus with one controller.

What the page leaves out

The page leaves out Modbus ASCII (hex characters, a colon start and an LRC check), broadcast to address 0 (no answer, and a turnaround delay after it), the other function codes (01/02 coils and inputs, 04 input registers, 05 write coil, 16 write multiple registers, 23 read/write), 32-bit and float values split over two registers and their word order, the drive's ramp up to the new speed, the slave's own reaction time, RS-485 biasing resistors and 2-wire versus 4-wire wiring, gateways between TCP and RTU, TCP acknowledgements for the Modbus data, and the Modbus/TCP Security handshake in detail.

On the mind map

Open the Computer Architecture mind mapComputerArchitectureAbstraction and numbers: layers and bit patternsAbstraction and numbersCombinational logic: output depends only on inputsCombinational logicSequential logic: state held by a clockSequential logicMIPS software: how C becomes machine wordsMIPS softwareThe processor: datapath plus controlThe processorPipelines and parallelism: more instructions at oncePipelines and parallelismMemory and I/O: small fast near, big slow farMemory and I/OMemory arraysMemory arraysdecoder picks wordline, bitlines readCPU cacheCPU cachetag, index, offset; hit or missCache address breakdownCache address breakdownsets = lines/ways; tag = rest of addressCache Write PoliciesCache Write PoliciesWrite-back sends dirty lines at evictionLoop tiling and interchangeLoop tiling and interchangeWalk rows; tile so reused data fitsMemory hierarchyMemory hierarchyAMAT = hit time + miss rate × penaltyDRAM timingDRAM timingopen row is fast, row change is slowMESI and false sharingMESI and false sharinga write invalidates other copiesVirtual memoryVirtual memoryTLB caches the page tableEmbedded I/OEmbedded I/Odevices are addresses; poll or interruptMCU startup to mainMCU startup to mainCopy .data, zero .bss, then mainButton debounce FSMButton debounce FSMTwo equal 10 ms samples = one pressI2C BusI2C BusWired-AND bus: ACK, stretch, arbitrateUSB enumerationUSB enumerationAddress 0, descriptors, SET_CONFIGURATIONCAN bus arbitrationCAN bus arbitrationDominant 0 wins: lowest ID sends unharmedModbus RTU and TCPone master asks; bad CRC means silenceTDMA time-triggered busTDMA time-triggered busFixed slots; FTA drops extremes, averagesAMBA AXI, AHB, APBAMBA AXI, AHB, APBVALID and READY both 1 on the edgePCIe TLPsPCIe TLPsRequests by address, completions by IDBus vs Crossbar vs NoCBus vs Crossbar vs NoCBus: one at a time; NoC: links in parallelADC and PWMADC and PWMSAR = binary search; duty = OC1RS/(PR2+1)PID ControlPID ControlI removes error; clamp stops windupOpen the Computer Architecture mind mapComputer Architecturesmall fast near, big slow farMemory and I/OMemory arraysMemory arraysdecoder picks wordline, bitlines readCPU cacheCPU cachetag, index, offset; hit or missCache address breakdownCache address breakdownsets = lines/ways; tag = rest of addressCache Write PoliciesCache Write PoliciesWrite-back sends dirty lines at evictionLoop tiling and interchangeLoop tiling and interchangeWalk rows; tile so reused data fitsMemory hierarchyMemory hierarchyAMAT = hit time + miss rate × penaltyDRAM timingDRAM timingopen row is fast, row change is slowMESI and false sharingMESI and false sharinga write invalidates other copiesVirtual memoryVirtual memoryTLB caches the page tableEmbedded I/OEmbedded I/Odevices are addresses; poll or interruptMCU startup to mainMCU startup to mainCopy .data, zero .bss, then mainButton debounce FSMButton debounce FSMTwo equal 10 ms samples = one pressI2C BusI2C BusWired-AND bus: ACK, stretch, arbitrateUSB enumerationUSB enumerationAddress 0, descriptors, SET_CONFIGURATIONCAN bus arbitrationCAN bus arbitrationDominant 0 wins: lowest ID sends unharmedModbus RTU and TCPone master asks; bad CRC means silenceTDMA time-triggered busTDMA time-triggered busFixed slots; FTA drops extremes, averagesAMBA AXI, AHB, APBAMBA AXI, AHB, APBVALID and READY both 1 on the edgePCIe TLPsPCIe TLPsRequests by address, completions by IDBus vs Crossbar vs NoCBus vs Crossbar vs NoCBus: one at a time; NoC: links in parallelADC and PWMADC and PWMSAR = binary search; duty = OC1RS/(PR2+1)PID ControlPID ControlI removes error; clamp stops windup
This page on the Computer Architecture mind map · 79 pages in 7 branches · open it to see the whole map and test yourself in recall mode.