Modbus TCP partial receive MBAP length framing - PLC Playground troubleshooting guide

Modbus TCP Works Until Replies Split? Treat TCP as a Stream

One network receive call does not necessarily contain one complete Modbus TCP reply. If a custom client fails intermittently on otherwise valid traffic, inspect stream framing and length handling.

Original parser example; use the current protocol specification and supported libraries.

What is happening?

TCP transports an ordered byte stream rather than application-message boundaries. A Modbus TCP parser must accumulate the required header and payload, handle partial reads, and preserve extra bytes belonging to subsequent messages.

Check these five things

  1. Capture the application receive lengths and parsing decisions.

  2. Use the MBAP length according to the specification.

  3. Retain partial data until a complete message is available.

  4. Handle multiple messages in one receive buffer.

  5. Test fragmentation and coalescing in a controlled software environment.

Worked example

A teaching reply arrives as the first four bytes in one receive and the rest in another. A parser expecting a full header immediately rejects the first chunk. The network has not lost the reply; the application confused transport chunks with messages.

Quick diagnostic reference

Receive pattern Required behavior
Partial header Accumulate
Partial payload Wait for remaining bytes
Two messages Parse both boundaries

The mistake to avoid

Adding a fixed sleep before reading may make a laboratory test pass while leaving the framing bug. Correct buffering is more reliable than assuming packet timing.

How to verify the fix

Replay fragmented, combined, truncated, and malformed inputs through the parser. Verify bounded lengths and clear timeout handling without discarding valid following messages.

Field-work boundary: These are educational diagnostic guides. Use the exact equipment manuals and approved site procedures. Electrical testing and machinery changes belong to qualified, authorized personnel; control hazardous energy and validate affected functions before release.

Technical reference

Modbus Organization: TCP messaging implementation guide. The examples and diagnostic tables above are original teaching material; they do not replace the product manual.

Continue troubleshooting

Previous Post