
Recall first from Modbus and CAN in the industrial stack
- Modbus RTU carries Modbus operations over a serial link, while Modbus TCP carries them over TCP/IP.
- CAN carries local messages whose payload meanings are defined by a higher-level protocol or device documentation.
- A Modbus client initiates reads and writes against a server, which can expose two bit tables and two 16-bit register tables.
Modbus RTU on a shared serial bus
3 min readModbus is one protocol in two transports; this lesson separates the serial and TCP framings.
A Linux program reading a connected TCP socket can receive half a Modbus message in one read and the rest in the next. It can also receive bytes from two messages in one read. Linux tcp(7) describes TCP as a stream that preserves byte order without preserving record boundaries. On a serial link, a different problem arises. The receiver needs to know how long the line was quiet between bytes. The Modbus specifications define different framing rules for these two cases.
Both variants carry a the function code followed by the data defined for that function, abbreviated PDU. The PDU says what operation the request asks for or what the response reports. The transport adds fields around it to form the complete message, called the application data unit, or ADU. For example, the request PDU 03 00 00 00 0A asks to read ten holding registers starting at protocol address zero. You can use those same five PDU bytes in either variant.
On a typical two-wire RS-485 link, the master and the devices share one differential pair for transmission in both directions. This is half-duplex operation. The UART serialises each byte with a start bit, eight data bits, a parity bit if configured, and the configured stop bits. Every device on the link needs matching settings. 8E1 means eight data bits, even parity and one stop bit. The UART character format tells the receiving hardware how to recover one byte. RTU framing tells it which bytes belong to the same Modbus message.
An RTU frame contains one address byte, the PDU and two CRC bytes, in that order. Here is an illustrative request written as hexadecimal bytes.
01 03 00 00 00 0A C5 CD
address PDU CRC low, CRC high
The master sends this request to device address 01. Individual serial devices use addresses 1 through 247, with a unique address for each device on the same bus. The addressed device puts its own address in the response. Address zero is reserved for broadcast requests. Broadcasts apply to write operations and elicit no reply, so a master schedules the next request using a broadcast turnaround delay. A read such as function 03 uses an individual address because it needs a reply from a specific device.
RTU uses CRC-16/Modbus, a 16-bit cyclic redundancy check. It covers the address and every PDU byte. The sender appends the low byte first, so the CRC value 0xCDC5 appears as C5 CD. (Computing CRC-16/Modbus, polynomial 0xA001 reflected, initial value 0xFFFF, over 01 03 00 00 00 0A gives 0xCDC5, which matches the bytes shown.) The receiver calculates the CRC over the received address and PDU, then compares it with the transmitted value. A mismatch makes the frame invalid. UART parity, when enabled, checks an individual character. The Modbus CRC checks the address and PDU together.
RTU timing and direction changes
3 min readThe Modbus serial line specification defines two timing intervals. A silent interval of at least 3.5 character times separates frames. Within an RTU message, a gap of more than 1.5 character times makes the frame incomplete and the receiver should discard it. A serial reader therefore needs both the byte values and enough timing information to locate their boundaries. The address byte can have the same value as a byte within the data, so searching for a familiar address cannot find the start reliably.
A character time includes everything the UART sends for one byte. At 9,600 baud with 8E1, a character uses 11 bits: one start bit, eight data bits, one parity bit and one stop bit. Dividing 11 by 9,600 gives about 1.146 ms per character. The 1.5-character interval is about 1.719 ms and the 3.5-character interval is about 4.010 ms. The specification recommends those character-based timings through 19,200 baud. Above 19,200 baud it recommends fixed intervals of 750 µs for the inter-character timeout and 1.75 ms for the inter-frame delay.
The master enables its RS-485 transmitter, sends the request and releases the pair after the last stop bit has left the UART. The addressed device observes the frame boundary, processes the request and enables its own transmitter to send the response. It then releases the pair so the master can transmit again. Releasing the transmitter early can cut off the end of the request, including the CRC. Keeping it enabled too long can overlap the beginning of the reply.
The turnaround budget includes direction changes and device processing. The 3.5-character interval sets a minimum separation between frames. A unicast response timeout allows time for the device to process the request and for its reply to arrive. After a broadcast, the turnaround delay gives devices time to execute the write before the next request. These settings describe different events. For example, a 4 ms frame gap at 9,600 baud gives roughly the required separation, while the device may need longer to prepare its reply.
On Linux, a successful write() can mean that the driver has accepted bytes for transmission while the UART is still sending them. tcdrain() waits for queued terminal output to be transmitted. A UART driver with RS-485 support can control direction around transmission and may support configurable delays before and after sending. On receive, serial drivers and USB adapters can deliver bytes to the program in batches. The time between two user-space reads then describes when batches reached the program. The actual silence between characters is measured on the serial link.
Modbus TCP headers and concurrent connections
3 min readA Modbus TCP server normally listens on TCP port 502. The master acts as a TCP client and opens a connection to the server. Each Modbus message begins with a seven-byte the Modbus Application Protocol header containing the transaction identifier, protocol identifier, length and unit identifier. The PDU follows the header, and the message ends after the PDU. The RTU CRC and serial silence intervals are absent from this format.
Here is the same read request as a Modbus TCP message, using transaction identifier 0x002A and unit identifier 01.
00 2A 00 00 00 06 01 03 00 00 00 0A
trans. proto. length unit PDU
The client chooses the two-byte Transaction Identifier. The server copies it into the reply so the client can match the reply to the request. The two-byte Protocol Identifier is zero for Modbus. The two-byte Length counts all bytes that follow the Length field, starting with the one-byte Unit Identifier. All three two-byte fields are sent most significant byte first. In the example, 00 06 means one unit byte and five PDU bytes, for a complete message of 6 + 6 = 12 bytes. The response has its own length, calculated from its own PDU.
A TCP receiver can read the first six bytes to learn the length. If it has fewer than six, it keeps those bytes and waits for more. After reading a valid length, it waits until it has the entire message, consumes exactly 6 + Length bytes, and keeps any bytes left over for the next message. For instance, if one read provides the first two bytes of the example and the next provides everything else plus another complete request, the first message still ends at byte 12. The next buffered byte begins the next transaction identifier. The same procedure works regardless of where TCP divides the bytes.
The Unit Identifier matters when the TCP server is a gateway. The connection selects the gateway by its IP address and TCP port. The unit identifier can tell that gateway which downstream serial device to address. With the example above, a gateway mapping unit 01 to RTU address 1 sends a serial request with address 01 and an RTU CRC, then returns the result with an MBAP header. When talking directly to a TCP device, follow the device's documented unit identifier convention. Some direct servers ignore the field and others expect a configured value.
One master can keep several TCP connections open, for example one to a direct device and another to a serial gateway. It can issue a request on each before either reply arrives. Each connection has its own receive buffer and pending requests. Transaction 0x002A on one connection can be tracked independently of transaction 0x002A on another. Within a connection that supports multiple outstanding requests, keep their transaction identifiers distinct while they are pending. A client can also send one request at a time per connection and still make progress on several connections concurrently. A gateway serving one downstream two-wire RTU bus still has to schedule serial exchanges on that shared pair.
The next lesson, libmodbus and register mapping, uses these two transport formats to send operations through a common library and interpret the register addresses those operations carry.
Key takeaways
- RTU uses timed silence for frame boundaries and sends its CRC-16 low byte first.
- On a two-wire RS-485 bus, the transmitting device releases the bus after its final stop bit so the next device can transmit.
- The MBAP Length field counts the unit identifier and the PDU.
- A Modbus TCP client can maintain several connections and associate replies with pending requests on each connection.
Reflect
- Explain to a colleague, in two sentences: identify the address, PDU and CRC bytes in a Modbus RTU frame.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- Man pagetermios(3): serial settings and transmission drain
- Man pagetcp(7): TCP stream semantics
- Kernel documentationRS485 Serial Communications: Linux driver support
- ReferenceModbus: RTU and TCP framing
Official documentation first. Links checked when the lesson was published.