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.
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.
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 page | Bytes before the CRC | CRC | On the wire |
|---|---|---|---|
| read slave 1, HR 0–1 | 01 03 00 00 00 02 | 0x0BC4 | C4 0B |
| its response | 01 03 04 00 EB 01 F4 | 0x108A | 8A 10 |
| write 1500 to slave 2 | 02 06 00 00 05 DC | 0x308B | 8B 30 |
| exception response | 01 83 02 | 0xF1C0 | C0 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.
| Code | Name | Meaning |
|---|---|---|
| 01 | ILLEGAL FUNCTION | the slave does not support this function code |
| 02 | ILLEGAL DATA ADDRESS | the register (or start + count) is outside its map |
| 03 | ILLEGAL DATA VALUE | a value in the request is not allowed, for example count 0 |
| 04 | SERVER DEVICE FAILURE | the 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.
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.