- Modbus RTU frame layout
- An RTU frame is: slave address (1 byte), function code (1 byte), data (0–252 bytes) and CRC (2 bytes), 256 bytes at most. Address 0 is broadcast (slaves execute the write but do not reply), 1–247 are slave addresses and 248–255 are reserved. CRC-16/MODBUS uses polynomial 0x8005 (0xA001 reflected) with initial value 0xFFFF, and its low byte is sent first.
- Function codes and per-request limits
- 01 Read Coils and 02 Read Discrete Inputs take up to 2000 at a time; 03 Read Holding Registers and 04 Read Input Registers up to 125; 0F Write Multiple Coils up to 1968 and 10 Write Multiple Registers up to 123. These limits keep the whole frame inside the 253-byte PDU. A register read reply is 5 + 2N bytes, a coil read reply 5 + ⌈N/8⌉, replies to 05, 06, 0F and 10 are 8 bytes and an exception reply is always 5.
- How a browser master receives a frame
- Modbus RTU separates frames with 3.5 character times of silence (about 4 ms at 9600 bps), but a USB serial chip batches bytes into packets before the computer sees them, so the browser has no per-byte timing. Instead of gaps, the tool uses the length the reply must have: once that many bytes arrive it checks the CRC, slave ID and function code; otherwise it waits for the timeout. The timeout has to cover the request and the reply on the wire — 11 bits per character, so reading 125 registers at 1200 bps takes about 2.4 s for transmission alone.
- Telling a request from a reply
- Length is usually enough: a 03 request is always 8 bytes and its reply is 5 + 2N, so the two never collide. Replies to 05 and 06 are byte-for-byte the request, so only context or a log's send/receive marks can tell them apart. Reading 17 to 24 coils with 01 or 02 produces an 8-byte reply just like a read request; the tool first prefers the reading that breaks fewer rules of the specification, then looks at whether the previous frame is the matching request.
- Modbus TCP and ASCII versus RTU
- Modbus TCP puts a 7-byte MBAP header (transaction ID, protocol ID 0, length, unit ID) in front of the PDU, has no CRC and uses port 502 by default. Modbus ASCII starts with a colon, ends with CR LF, writes every byte as two hex characters and checks it with an LRC. RTU over TCP, the “transparent” mode of serial device servers, is not Modbus TCP: the TCP stream still carries RTU frames with their CRC.
- Modbus exception codes
- An exception reply carries the original function code plus 0x80 (03 → 83) followed by a one-byte exception code: 01 Illegal Function (the device does not support that function code), 02 Illegal Data Address, 03 Illegal Data Value (quantity out of range, or a 05 value other than FF 00 / 00 00), 04 Server Device Failure, 05 Acknowledge (accepted, still working), 06 Server Device Busy, 08 Memory Parity Error, 0A Gateway Path Unavailable and 0B Gateway Target Device Failed to Respond. A CRC error never produces an exception reply: the slave drops the corrupt frame and the master simply times out.