Protocol ExplorerGuidesPricing

Modbus Protocol: Function Codes and TCP Frames

Modbus defines request and response messages used by industrial devices. Modbus TCP carries those messages over a TCP connection with an additional MBAP header.

Updated · Editorial approach and simulation limits

A function code identifies the requested operation

A Modbus request identifies a unit and function, then includes data such as a starting address, quantity or write value. The server returns a response for that function or an exception response when it cannot complete the request. A function code is therefore the first clue to what the client asked the device to do.

Read-coil, read-register and write functions operate on different data models. Register references in a user interface may use one-based labels while the protocol carries a zero-based address, so verify the device documentation before mapping a displayed register to a packet offset.

Modbus TCP adds an MBAP header

On TCP, the Modbus Application Protocol header carries a transaction identifier, protocol identifier, length and unit identifier before the function data. The transaction identifier helps match a response to a request when more than one transaction is in flight; TCP stream framing still has to be respected.

TCP delivers bytes, not Modbus message boundaries. A receiver uses the length information to parse an application message even if the stream is split across segments or several messages arrive together. A packet trace should reassemble the TCP stream before interpreting the MBAP length.

Operational safety for industrial traffic

A Modbus response indicates what the device returned for that request, not whether a control action was safe or physically completed. Review unit identifiers, function codes, exception responses and the device’s own state before drawing operational conclusions.

Modbus/TCP should be deployed with network segmentation and access controls appropriate to the environment; the classic protocol does not provide user authentication or confidentiality as part of the basic message. Do not send test writes to a production controller. Protocol Explorer’s sequence is static and does not contact equipment.

Understand the PDU before interpreting register values

A Modbus Protocol Data Unit contains a function code followed by function-specific data. A client request might ask to read a range, write a value or report a device identifier. A normal response carries the matching function code and response data. If the server cannot perform the request, an exception response sets the high bit of the requested function code and adds an exception code. For example, an exception to function 03 appears as function 83 in hexadecimal; exception codes distinguish an unsupported function, invalid address, invalid value or device failure.

That response is a protocol-level result, not a guarantee about physical equipment. A successful write response means the server accepted the Modbus operation according to its implementation. The application should still check device state, interlocks and feedback before concluding that a physical process reached its target.

Separate the four data tables from vendor labels

The common data model distinguishes coils, discrete inputs, input registers and holding registers. Coils and discrete inputs are bit-oriented; input and holding registers are 16-bit values. Read and write permissions depend on the function and the device. The protocol defines how the client addresses an item, but the device manual supplies the engineering meaning, scaling, units and valid ranges.

Human-facing documentation may label the first holding register as 40001 while the request carries a zero-based starting address. That convention is not universal across configuration tools. Confirm the exact offset with the device vendor and compare it with the packet before diagnosing an off-by-one error. For values wider than one register, the order of bytes or registers can also be vendor-specific.

Trace a safe read request in Modbus TCP

This request asks unit 1 to read two holding registers beginning at protocol address 100. It is an illustrative read, not a command to send to a controller.

00 2A  00 00  00 06  01  03  00 64  00 02
| TID | PID | Length | UID | FC | Start | Qty |

The transaction identifier is 0x002A, the protocol identifier is zero, the length counts the unit identifier and PDU, and the unit identifier is 1. Function 03 requests holding registers; the address and quantity fields are two bytes each. A normal example response would repeat transaction identifier 0x002A and function 03, then provide a byte count of 04 followed by two 16-bit values. Matching the identifier and unit helps correlate requests and responses when a gateway or client has multiple operations in flight.

TCP carries a byte stream, so MBAP length matters

Modbus TCP places a seven-byte MBAP header before the PDU. It contains the transaction identifier, protocol identifier, length and unit identifier. TCP does not preserve application-message boundaries: one Modbus message can span multiple TCP segments, or a single segment can contain parts of multiple messages. A receiver uses the MBAP length to recover the message boundary after reassembling the stream.

In a packet analyzer, first follow and reassemble the TCP conversation, then parse the MBAP header. Treating each TCP segment as one Modbus message can produce false function codes, impossible lengths or orphaned responses. The transaction identifier is useful correlation data, while TCP sequence and acknowledgment numbers describe delivery of the byte stream below it.

Unit identifiers help gateways reach downstream devices

On a direct Modbus TCP connection, the unit identifier may be a simple device selector. A TCP-to-serial gateway can use it to select a downstream serial unit, so the value may matter even when the client’s socket connects to one gateway address. The gateway’s routing table and serial-line settings determine what happens next.

Do not assume that every unit identifier has a global meaning or that port 502 alone identifies a valid Modbus exchange. Confirm the endpoint role, gateway mapping, request function and response. If the traffic is serial Modbus RTU rather than TCP, its framing and checksum rules differ; it does not use the TCP MBAP header.

Classic Modbus TCP and Modbus Security are different deployments

The basic Modbus/TCP message format does not itself authenticate users or encrypt the connection. Network segmentation, allowlisted peers, read-only roles where supported and controlled gateways are therefore important deployment controls. Modbus Organization also publishes Modbus Security, which encapsulates Modbus messages in TLS, uses X.509 certificates for client and server authentication, and is assigned system port 802. Read the Modbus Application Protocol Specification for function-code and message requirements. A conventional TCP trace on port 502 and a TLS-protected Modbus Security trace should not be described as the same security mode.

Industrial networks can cause physical consequences. Review whether a function is read-only or writes state, and do not test write functions on a production controller. Protocol Explorer’s sequence is static and does not connect to industrial equipment.

A practical order for reading a Modbus trace

  1. Identify the transport and reassemble the TCP stream if the exchange is Modbus TCP.
  2. Read the MBAP length and transaction identifier, then find the unit identifier.
  3. Interpret the function code and address using the device’s register map.
  4. Match the response by transaction identifier and check for an exception function.
  5. Compare returned values with device state and scaling documentation before drawing an operational conclusion.

This order keeps framing errors, address conventions and application meaning separate. It also avoids treating one successful response as proof that a control-system workflow completed safely.

Explore Modbus interactively

Follow the matching exchange packet by packet. The animation is a scripted teaching model with illustrative timing, not a capture of your network.

Open the Modbus animation · Compare Free and Pro

Specification and source

For exact protocol requirements, consult the primary document: Modbus Organization: Modbus Specifications. This page summarizes selected behavior and is not a substitute for the specification.

Related protocol lessons

Browse all application protocols · Learn to read framed messages inside a transport stream