TCP Framing & Gateways

Dissect the Modbus TCP frame: 7-byte MBAP header + PDU, transaction ID and length field, and how an RTU↔TCP gateway bridges them

Chapter Objectives

Scenario: The Courier Waybill Envelope

The same command (function code + address + quantity), when sent over Ethernet, puts on a different envelope—Modbus TCP. A TCP frame is like a courier package: a waybill with 7 pieces of information is attached at the front (the MBAP header), it states the "length" so the stream can be split precisely, and no stamp (CRC) is needed, because the TCP/IP stack already guarantees data integrity.

Why It Matters

Modern factories, SCADA systems, and cloud platforms almost all use TCP. It supports multiple concurrent clients, crosses network segments, and feeds directly into databases and the web. Understand the MBAP header and gateways and you can seamlessly connect "serial devices in the workshop" to "systems in the office".

Core Concepts: TCP Frame Structure

MBAP header (7 bytes)Transaction ID 2B · Protocol ID 2B · Length 2B · Unit ID 1B
FC1B
Datavariable

Transaction ID: the client numbers this request and the response carries the same number back, which is how questions and answers are paired on concurrent connections; Protocol ID: fixed at 0x0000 for Modbus; Length: how many bytes follow (unit ID + PDU), not counting itself; Unit ID: carries the RTU slave address when a gateway bridges a serial line; for a pure TCP slave it is usually 1 or 255.

Protocol Details and Byte-Level Examples

The Same Command: Byte-by-Byte Dissection of the TCP Version

Command: "read 2 holding registers from slave 5, starting at 0x0000" (TCP 12 bytes = MBAP header + PDU, transaction ID = 1):

■ MBAP Header ■ Function Code ■ Data
Transaction ID = 1 (2B) 00 01
Protocol ID = 0x0000 (2B) 00 00
Length = 6 (2B) 00 06
Unit ID = 5 (1B) 05
FC 03 (1B) 03
Starting address + quantity (4B) 00 00 00 02
ByteValueMeaning
0–100 01Transaction ID
2–300 00Protocol ID
4–500 06Length (unit ID + PDU)
605Unit ID
703Function code
8–900 00Starting address
10–1100 02Quantity

Length = 0x0006 = 6 bytes follow (1 byte unit ID + 5 bytes PDU). TCP has no CRC—data integrity is the responsibility of the TCP/IP stack. Compared with Chapter 6, the same content takes 8 bytes in RTU (2 extra CRC bytes, but 7 fewer MBAP bytes).

Key Differences at a Glance

AspectRTUTCP
Header1 byte (address)7 bytes (MBAP)
Trailer2-byte CRC-16None
Overhead per frame3 bytes7 bytes
Frame boundary≥3.5 characters of silenceLength field
Transaction matchingImplicit (one question at a time)Explicit (transaction ID)
Multiple mastersNot supportedMultiple clients supported

Real-World Applications

Case 1: Main Storyline—TCP Plus a Gateway in the Office

What Ms. Wang's gateway does: receive a TCP request → strip off the MBAP header → take out the PDU → convert it into an RTU frame for the slave indicated by the unit ID → receive the response → wrap it in a new MBAP header → return it to the TCP client. As far as the office app is concerned, it only ever speaks TCP; as far as the sensors in the greenhouse are concerned, it only ever speaks RTU—the gateway is the translator between the two.

Choosing Between Them

Hands-On Lab Guide

  1. Open the Lab page, switch the protocol to TCP, operation "Read Holding Registers";
  2. Unit ID 5, starting address 0x0000, quantity 2 → expected 00 01 00 00 00 06 05 03 00 00 00 02 (the transaction ID may differ);
  3. Copy the hex into the "Parse" area and confirm each MBAP field matches this chapter's table;
  4. Switch back to RTU, build the same command, and compare the two "envelopes".

Common Misconceptions

Quiz Anchors

  1. Which field in the MBAP header pairs requests with responses on concurrent connections? Who decides its value?
  2. For a TCP request that "reads 2 holding registers", what should the MBAP length field be? Why?
  3. When bridged through a gateway to RTU, which field of the RTU frame does the TCP frame's "unit ID" actually correspond to?

Chapter Summary and What's Next

In this chapter you learned about the TCP "courier waybill": a 7-byte MBAP header (transaction ID / protocol ID / length / unit ID) + PDU, framed by the length field, paired by the transaction ID, and with no CRC. You also saw clearly how an RTU↔TCP gateway acts as a translator.

That wraps up the theory. In the next chapter, the doors of the Lab open—go and assemble this chapter's frames by hand, and see whether your fingers agree with the protocol specification.