top of page

Modbus TCP Partial Replies: Buffer by MBAP Length Before Decoding

7 days ago
4 min read

A Modbus TCP client works in a short bench test, then intermittently reports incomplete replies or loses the next reading. Before blaming the instrument or extending every timeout, check the receive logic. A socket read is a chunk of a byte stream, not a promise that exactly one complete Modbus message has arrived. The client needs bounded buffering and protocol framing before it decodes values.


Understand what TCP delivers


RFC 9293, section 3.7 explains that TCP segment boundaries do not have a guaranteed relationship with application read or write buffers. A complete application message may therefore require more than one read, and one read may contain bytes from several messages. TCP preserves the byte stream’s order; it does not supply the application’s message boundaries.


This matters in PLC interfaces, custom data collectors and gateway integrations. Code that treats each receive callback as one reply may discard a legitimate partial message. Code that decodes only the first reply in a larger chunk may leave later bytes unprocessed. Neither symptom, by itself, establishes a bad register map or a faulty sensor.


The Monnit Ethernet wireless sensor gateway pictured below is a catalogue example of Ethernet monitoring equipment. Monnit’s Ethernet Gateway 4 interface guide documents read-only Modbus TCP polling of gateway and sensor information. Confirm generation, firmware and the exact interface configuration separately; this article does not describe an observed defect or a tested capacity of that gateway.


Black Monnit Ethernet wireless sensor gateway with upright antenna and a connected Ethernet accessory on a white background.
Monnit Ethernet wireless sensor gateway with antenna and Ethernet connection accessory - real ProSense Instruments catalogue photograph, not a customer installation or a fault demonstration.

Use the MBAP length correctly


The Modbus Organization’s TCP/IP implementation guide defines a seven-byte MBAP header. Its Length field counts the bytes after the first six header bytes, including the Unit Identifier and the protocol data unit. Consequently, a complete Modbus TCP application data unit occupies six bytes plus that Length value. The Unit Identifier is already included in the count.


As an arithmetic example, a Length value of five gives an eleven-byte application message: 6 + 5 = 11. Adding seven instead of six would make the parser wait for an extra byte, potentially taking it from the following message. This example illustrates framing arithmetic; it is not a register map, captured device response or performance result.


Define the parser’s responsibilities


Keep a separate receive buffer for each connection. Append newly received bytes to that connection’s existing bytes. Once sufficient header information is present, validate the header and declared length against the protocol and implementation’s documented bounds before allocating or waiting for a message of that size. Do not trust an arbitrary length as an unlimited memory request.


If the complete message has not arrived, retain the partial bytes and wait under a bounded completion policy. When a complete message is available, extract exactly its bytes, preserve any remainder and continue looking for another complete message. Decode only after framing and validation have succeeded.


Keep these responsibilities separate from transaction matching. A correctly framed reply still needs to match the outstanding request and pass the function-specific checks. Conversely, recognising a transaction identifier does not make a truncated message safe to decode. Use the maintained driver’s documented behaviour where available rather than adding an unverified workaround around it.


Test chunk boundaries without disturbing production


Use a client test harness or isolated simulator with known complete messages. First deliver the reference message in one chunk. Then deliver exactly the same bytes split into several chunks, including a split within the header. The decoded result should remain the same, and no value should be accepted before the required bytes are present.


Next, concatenate two complete reference messages and deliver them together. Verify that both are processed once and routed through normal response validation. Also test a chunk containing one complete message followed by only part of the next; the remainder must survive until more bytes arrive. These are proposed software acceptance tests, not claims about how a particular gateway divides its traffic.


Add failure cases: an invalid declared length, a connection closing with a partial message, and a reconnect after an incomplete exchange. Confirm bounded resource use, an explicit error outcome and no mixing of old connection bytes into a new session. Document the chosen recovery policy rather than silently skipping arbitrary bytes until a plausible value appears.


Collect evidence that identifies the failing layer


Record the client and driver versions, endpoint, timestamps, chunk sizes, accumulated byte count, declared length and parser outcome. Preserve a short authorised trace for comparison. Limit captures to the relevant test traffic and handle device identifiers and network details under the site’s information rules.


Do not assume a packet capture has the same boundaries as the application’s read callbacks. Correlate transport evidence with the client’s own diagnostic log. Avoid attempts to reproduce the fault by flooding an operating controller, changing outputs or weakening network controls. Have competent personnel approve any operational testing.


For supplier assistance, provide ProSense the exact gateway or instrument, client software version and a sanitised example showing the bytes and chunk boundaries. A repeatable split-message test is more useful than a screenshot of a generic timeout. Verify framing first, then transaction association, register interpretation and measurement freshness; each answers a different question.


Need help with your application?


ProSense Instruments - explore instruments, sensors, data loggers and test equipment.


ProSense Solutions - discuss complete monitoring and automation systems.


ProSight Software - explore cloud dashboards, alarms and reporting. Device integration must be confirmed for your application.



Project enquiries: sales@prosense.com.au. Software enquiries: info@prosightsoftware.com.

Comments


bottom of page