Modbus TCP Frame Decoder
Paste a Modbus TCP frame in hex, as captured by Wireshark or a test tool. The decoder splits the MBAP header, checks the length field, and decodes the function code, addresses, quantities, data values and exception codes byte by byte.
How a Modbus TCP Frame Is Built
A Modbus TCP message, called the ADU, is the normal Modbus PDU (function code plus data) with a 7 byte MBAP header in front. There is no CRC: TCP already checks the data. All Modbus TCP traffic goes to the registered TCP port 502. The header is defined in the Modbus Messaging on TCP/IP Implementation Guide.
| Field | Bytes | Meaning | Set by |
|---|---|---|---|
| Transaction identifier | 2 | Pairs a response with its request | Client; server copies it back |
| Protocol identifier | 2 | 0 = Modbus protocol | Client; server copies it back |
| Length | 2 | Number of following bytes (unit identifier plus PDU) | Client in the request, server in the response |
| Unit identifier | 1 | Slave address behind a gateway; often 1, 0 or 255 for a direct TCP device | Client; server copies it back |
| Function code | 1 | What to do (03 = read holding registers) | Client; server adds 0x80 on an error |
| Data | 0 to 252 | Addresses, quantities, byte count, values | Both |
Exception function code = request function code + 0x80
Worked example
The request 00 01 00 00 00 06 11 03 00 6B 00 03 decodes as transaction 1, protocol 0, length 6, unit 17 (0x11), function 03 Read Holding Registers, start address 0x006B = 107 and quantity 3. Counting the bytes after the length field gives 1 + 5 = 6, so the length is OK. In 1 based Modicon numbering the client is reading registers 40108 to 40110.
The response 00 01 00 00 00 09 11 03 06 02 2B 00 00 00 64 carries the same transaction and unit, a byte count of 6 and three values: 0x022B = 555, 0 and 0x0064 = 100. The tool opens with the request; click the FC03 response example to see the values.
Function codes and limits
| Code | Name | Request data | Limit per request |
|---|---|---|---|
| 01 / 02 | Read Coils / Discrete Inputs | start, quantity | 2000 bits |
| 03 / 04 | Read Holding / Input Registers | start, quantity | 125 registers |
| 05 | Write Single Coil | address, FF00 (on) or 0000 (off) | 1 coil |
| 06 | Write Single Register | address, value | 1 register |
| 15 (0x0F) | Write Multiple Coils | start, quantity, byte count, bits | 1968 coils |
| 16 (0x10) | Write Multiple Registers | start, quantity, byte count, values | 123 registers |
Common exception codes are 01 illegal function, 02 illegal data address, 03 illegal data value, 04 server device failure, 06 server device busy, 0A gateway path unavailable and 0B gateway target device failed to respond.
For serial frames with a CRC use the Modbus RTU frame decoder and the Modbus CRC calculator. To turn two registers into a real number, use the IEEE 754 float converter.
Frequently Asked Questions
What is the MBAP header in Modbus TCP?
The 7 byte Modbus Application Protocol header: transaction identifier (2), protocol identifier (2), length (2) and unit identifier (1). It replaces the slave address and CRC of Modbus RTU.
What does the length field count?
The number of bytes that follow it: the unit identifier plus the function code and data. It equals the total frame length minus 6.
What unit identifier should I use for Modbus TCP?
For a device addressed directly by IP the value is often ignored; 1, 0 or 255 are common. Behind a TCP to RTU gateway it must be the slave address of the serial device.
Does Modbus TCP have a CRC?
No. TCP and Ethernet already check the data, so Modbus TCP frames carry no CRC.
What does function code 0x83 mean?
It is an exception response to function 03. The next byte is the exception code, for example 02 means illegal data address.
Learn more on the blog
Modbus protocol explained: RTU vs TCP
A practical guide on instrumentationblog.in that explains the theory behind this tool with solved examples.
Read Full Article