Modbus TCP vs. Modbus RTU: MBAP Header, Function Codes & Exception Diagnostics
Originally published by Modicon in 1979 for programmable logic controllers, Modbus remains the most universally supported open communication protocol in industrial automation, smart grid power meters, VFD motor drives, temperature controllers, and building management systems. While the core Protocol Data Unit (PDU)—consisting of a 1-byte Function Code and variable Data payload—is identical across variants, the framing and error-detection layers differ fundamentally between serial Modbus RTU and Ethernet Modbus TCP.
1. Structural Comparison: ADU Framing
An complete Modbus packet on the wire is called an Application Data Unit (ADU). Understanding how the ADU wraps the core PDU is critical when debugging protocol converters (e.g., Moxa MGate or RS-485-to-Ethernet gateways):
-
Modbus RTU ADU (Serial RS-485 / RS-232):
[1-Byte Slave ID] + [PDU: Function Code + Data] + [2-Byte CRC-16 (LE)]. Frames are delimited on the physical serial bus by a silent idle interval of at least 3.5 character times ($t_{3.5}$). -
Modbus TCP ADU (Ethernet Port 502):
[7-Byte MBAP Header] + [PDU: Function Code + Data]. Because TCP already guarantees reliable delivery with hardware Ethernet FCS and TCP checksums, the 2-byte CRC-16 tail is removed and replaced by a 7-byte Modbus Application Protocol (MBAP) Header at the front of the packet.
| MBAP Header Field | Byte Length | Byte Order | Description & Gateway Behavior |
|---|---|---|---|
| Transaction Identifier | 2 Bytes | Big-Endian | Incrementing sequence number generated by the client and echoed by the server to pair concurrent TCP requests. |
| Protocol Identifier | 2 Bytes | Big-Endian | Always 00 00 for standard Modbus TCP. |
| Length Field | 2 Bytes | Big-Endian | Byte count of all following bytes, including the 1-byte Unit Identifier plus the entire PDU. |
| Unit Identifier (Slave ID) | 1 Byte | Single Byte | 0x01–0xF7 when routing through a Modbus TCP-to-RTU serial bridge; 0x00 or 0xFF for standalone TCP servers. |
2. Standard Function Codes & Register Memory Tables
Modbus organizes device memory into four conceptual tables. Unlike MELSEC PLCs (which use Little-Endian), all 16-bit addresses and register values inside a Modbus PDU are transmitted in Big-Endian (Most Significant Byte first) network order:
| Function Code | Hex | Target Memory Table | Traditional 5-Digit Reference | Access Type & Max Count per Frame |
|---|---|---|---|---|
| Read Coils | 0x01 |
Discrete Output Coils | 00001 – 09999 |
Read-Write (1-Bit). Up to 2,000 coils per request. |
| Read Discrete Inputs | 0x02 |
Discrete Input Contacts | 10001 – 19999 |
Read-Only (1-Bit). Up to 2,000 inputs per request. |
| Read Holding Registers | 0x03 |
16-Bit Output/Config Registers | 40001 – 49999 |
Read-Write (16-Bit). Up to 125 registers (250 bytes) per request. |
| Read Input Registers | 0x04 |
16-Bit Analog/Sensor Inputs | 30001 – 39999 |
Read-Only (16-Bit). Up to 125 registers per request. |
| Write Single Coil | 0x05 |
Discrete Output Coils | 00001 – 09999 |
Write-Only. Value FF 00 = ON (1), 00 00 = OFF (0). |
| Write Single Register | 0x06 |
16-Bit Holding Registers | 40001 – 49999 |
Write-Only. Echoes the exact request frame upon success. |
| Write Multiple Coils | 0x0F (15) |
Discrete Output Coils | 00001 – 09999 |
Write-Only. Packs multiple bit states into byte-aligned payload. |
| Write Multiple Registers | 0x10 (16) |
16-Bit Holding Registers | 40001 – 49999 |
Write-Only. Up to 123 contiguous 16-bit registers per request. |
3. The "Off-by-One" Addressing Trap (40001 vs. Offset 0x0000)
One of the most frequent sources of commissioning errors is the discrepancy between 1-based documentation notation and 0-based wire protocol addressing:
- In vendor manuals, Holding Register #1 is written as
40001(or400001in 6-digit notation), where the leading digit4merely denotes the "Holding Register" table. - On the wire, the Function Code (
0x03) already specifies the Holding Register table, and the 2-byte starting address field is 0-indexed. Therefore, to read Holding Register40001, the wire packet sends starting address0x0000. To read40101, the wire packet sends0x0064(decimal 100).
4. Packet Dissection & Exception Response Decoding
# Read 2 Holding Registers (40001..40002) from Slave 1
# 1) Modbus RTU Request (8 Bytes):
01 03 00 00 00 02 C4 0B
# [01: Slave 1] [03: Func] [00 00: Start Addr 0] [00 02: Qty 2] [C4 0B: CRC-16 LE]
# 2) Equivalent Modbus TCP Request (12 Bytes):
00 01 00 00 00 06 01 03 00 00 00 02
# [00 01: TxID] [00 00: Proto] [00 06: Len] [01: UnitID] [03 00 00 00 02: PDU]
When a slave rejects a command, it sets the Most Significant Bit of the Function Code (`Func | 0x80`, so `0x03` becomes `0x83`) and returns a 1-byte Exception Code:
0x01(Illegal Function): The slave does not implement the requested function code (e.g., sending0x06to a device that only supports0x10).0x02(Illegal Data Address): The starting address or `Start + Quantity` falls outside the device's valid register map.0x03(Illegal Data Value): A payload parameter is malformed (e.g., sending0x0001instead of0xFF00for Function0x05Write Single Coil).0x04(Slave Device Failure): An unrecoverable internal hardware error occurred while attempting the action.
Frequently Asked Questions (FAQ)
Q. Why does Modbus TCP not include a 2-byte CRC-16 checksum at the end of the packet?
Modbus TCP runs over TCP/IP and Ethernet, which already enforce 32-bit Ethernet Frame Check Sequences (FCS) and 16-bit TCP segment checksums. Adding an application-layer CRC-16 would be redundant.
Q. Why does my response value appear shifted by one register compared to the device manual?
Some vendor manuals list 1-based register numbers (40001 = address 1) while the Modbus wire protocol uses 0-based offsets (40001 = wire address 0x0000). Subtract 1 from the manual's register number if values are shifted.
Q. What does a Modbus response starting with Function Code 0x83 and byte 0x02 mean?
0x83 is 0x03 + 0x80, indicating an Exception Response to Read Holding Registers. Exception code 0x02 means Illegal Data Address—one or more requested register addresses do not exist on the slave.
Q. How are 32-bit floating-point values transmitted over 16-bit Modbus registers?
A 32-bit IEEE 754 float spans two consecutive 16-bit registers. Depending on the vendor, word order may be Big-Endian (ABCD) or Mid-Little-Endian Word-Swapped (CDAB).