Modbus TCP: the MBAP header, port 502 and the unit ID
Part of the Modbus guide
Modbus TCP carries Modbus requests and responses over a TCP connection, by default to port 502 on the server. Each message is a seven-byte MBAP header followed by the same function code and data used on a serial line. There is no CRC, because TCP and Ethernet already check the data, and there is no silent interval, because the header states the length.
This page belongs to the Modbus guide. For the data model and register addressing, see What is the Modbus protocol?; for the serial variant, see Modbus RTU.
Modbus TCP frame format: the MBAP header
MBAP stands for Modbus Application Protocol. The header is seven bytes, and all multi-byte fields are sent high byte first.
| Field | Size | Set by | Purpose |
|---|---|---|---|
| Transaction identifier | 2 bytes | Client, echoed by the server | Pairs a response with its request |
| Protocol identifier | 2 bytes | Client, echoed by the server | Always 0x0000 for Modbus |
| Length | 2 bytes | Client in requests, server in responses | Number of bytes that follow, counting the unit identifier and the PDU |
| Unit identifier | 1 byte | Client, echoed by the server | Addresses a device behind a gateway |
The PDU follows: one function code byte and up to 252 data bytes, 253 bytes in total. With the header, the largest Modbus TCP frame is 260 bytes.
A decoded request and response
The client asks unit 17 for three holding registers starting at register 108 (address 0x006B).
Request: 00 01 00 00 00 06 11 03 00 6B 00 03
00 01 transaction identifier 1
00 00 protocol identifier: Modbus
00 06 length: 6 bytes follow
11 unit identifier 17
03 function code: read holding registers
00 6B starting address 107 (register 108)
00 03 quantity: 3 registersResponse: 00 01 00 00 00 09 11 03 06 02 2B 00 00 00 64
00 01 transaction identifier 1, echoed
00 00 protocol identifier
00 09 length: 9 bytes follow
11 unit identifier 17, echoed
03 function code, echoed
06 byte count: 6 data bytes
02 2B register 108 = 555
00 00 register 109 = 0
00 64 register 110 = 100Compare this with the serial frame for the same read on the Modbus RTU page. From the unit identifier through the last data byte, the bytes are identical. The serial frame ends with a CRC; the TCP frame starts with six extra header bytes.
An exception looks the same as on serial, inside the header:
Exception: 00 01 00 00 00 03 11 83 02
00 03 length: 3 bytes follow
83 0x03 + 0x80
02 exception code 02, illegal data addressWhat is the unit ID for?
On a serial line the first byte is the device address. On Ethernet the IP address already identifies the server, so the unit identifier has a different job: it tells a gateway which serial device behind it should receive the request.
There are two cases.
For a device with its own Ethernet port, the unit identifier carries no routing information. The Modbus Organization’s TCP/IP implementation guide says to use 0xFF for such a server. In the field, behaviour varies: some devices ignore the byte, some answer only to 1, some answer only to the value set in their configuration. If a device accepts the TCP connection but never answers a request, try unit 1 and unit 255 before suspecting the network.
For a serial-to-Ethernet gateway, the unit identifier is the RS-485 address of the target device. The gateway strips the MBAP header, appends a CRC, sends the frame on the serial side, and wraps the reply. If the addressed serial device does not answer, the gateway returns exception 0B, gateway target device failed to respond. If the gateway cannot route the request at all, it returns 0A, gateway path unavailable. Both mean the Ethernet side is working and the problem is behind the gateway: the unit identifier, the serial settings or the wiring.
Connections, transaction IDs and timeouts
The client opens the TCP connection and normally keeps it open for the life of the session. Opening and closing a connection for every poll works but wastes time and can exhaust small devices.
Several clients can connect to one server at the same time, which serial Modbus cannot do. Embedded servers often accept only a handful of connections, though, and a device whose sockets are all held by a SCADA system, an HMI and a forgotten laptop will refuse the next client. A connection that is refused while a ping succeeds points to this, or to a firewall on port 502.
The transaction identifier lets a client send a second request before the first has been answered and still match the replies. Whether a given server handles more than one outstanding request per connection depends on the device; many process requests strictly one at a time. Sending one request and waiting for its response is the safe default.
TCP delivers bytes in order or not at all, but it does not promise when. The client still needs its own response timeout per request. On a local network a direct server answers in milliseconds. Through a gateway, the answer takes as long as the serial transaction behind it, so the timeout has to cover the serial round trip, and a gateway that serves a slow bus should not be polled faster than the bus can keep up.
Security
Modbus TCP has no authentication and no encryption. Any host that can open port 502 on a device can read its registers and write its coils. Treat network reachability as the only access control: keep Modbus devices on a control network or VLAN, and do not forward port 502 through a router to the internet. A separate specification, Modbus/TCP Security, wraps the protocol in TLS on port 802, but device support is limited.
Common problems
| Symptom | Likely cause |
|---|---|
| Connection refused, device answers ping | Server disabled, connection limit reached, or a firewall on port 502 |
| Connects, then every request times out | Wrong unit identifier |
Exception 0B from a gateway | Wrong unit identifier for the serial device, or a serial fault behind the gateway |
| Values shifted by one register | Register numbering starts at 1, addresses at 0 |
| Floats read as nonsense | Word order of the 32-bit value |
| Garbage or no reply through a serial device server | The server is passing raw RTU frames over TCP, not Modbus TCP |
The last one deserves a note. Some serial device servers do not translate; they tunnel the RTU frame, CRC included, inside a TCP stream. That is not Modbus TCP, and a Modbus TCP client cannot talk to it. The RTU and TCP comparison covers the difference.
Reading Modbus TCP with ControlCom
ControlCom Connect is hosted, so the polling is done on site by the ControlCom Edge Server. Its Modbus TCP client takes an Endpoint IP and Endpoint Port (502), a polling Interval in milliseconds, a Timeout in seconds and a Client Concurrency setting, which is the number of requests allowed in flight at once and defaults to 1. Each device behind the endpoint is a unit with its own Unit ID, so one client can address every serial device behind a gateway. The Edge Server also has an ad-hoc Modbus TCP poller for checking a register map against a live device before a client is configured.
The Modbus client documentation has the full field list. The platform overview shows what happens to the values afterwards, and the page for controls engineers describes the commissioning workflow.
Point the Edge Server at a PLC or gateway on port 502, map each register to a variable, and the values are stored, trended and alarmed on in ControlCom Connect.