Troubleshooting
Speeding Up Modbus RTU Writes Behind Polled Reads
Short answer
The writes are slow because the Modbus client driver owns the serial port and services one enabled instruction at a time, so a write queues behind every enabled read. Put each transaction in its own stage, put the write stages first, and advance the chain on completion; a write then waits at most one in-flight transaction.
On this page
- Why do my Modbus writes wait behind the polled reads?
- Is there a serial buffer I can flush to let a write go first?
- Is an 83 ms slot long enough for one transaction?
- What if the delay is timeouts, not scheduling?
- How do I sequence the port so writes go first?
- Should I combine the slave programs or convert them to tasks?
- How do I keep change-triggered writes from losing a command?
- When is a second port the right fix?
- Questions asked
The lag is a service-order problem, not a buffer problem. The Modbus client driver owns the serial port and executes exactly one enabled instruction at a time, and a timer/counter scheduler advances on time whether or not the transaction finished — so a write waits out a whole sweep of reads, and a missed slot idles a whole polling interval. Consolidate the port's transactions into one stage-sequenced program with the write stages first and advancement tied to completion; after that, a write waits behind at most one in-flight transaction. What remains beyond that is a timeout or a downstream delay, and both have their own checks below.
Why do my Modbus writes wait behind the polled reads? #
Your writes wait because the Modbus client driver owns the serial port and services exactly one enabled instruction at a time — enabling an instruction only requests service — so a write queued behind eleven enabled reads waits out a full sweep, about 0.4 s at the timing worked out below.
Modbus RTU on a multidrop RS-485 pair is half duplex with one outstanding transaction: the request must finish on the wire before anything else can use the bus. On top of that limit, this platform arbitrates the port internally. Enabling an MRX read or an MWX write only raises a request for service; the driver executes one requesting instruction, then moves to the next after it completes or errors. Enable every instruction at once and you get a plain round-robin in code execution order — exactly what continuously enabled reads across separate program blocks produced before the timer/counter existed. The same single-transaction constraint is why Modbus RTU drive polling on one serial port is slow.
Count instructions, not slaves: a slave polled with several separate reads contributes several transactions, and each armed write adds one more place in line.
Is there a serial buffer I can flush to let a write go first? #
No — there is no queue of complete request frames sitting in the port to flush; an enabled instruction only raises a request for service, and the driver starts the next request after the current one completes or errors.
Nothing in this mechanism serializes whole messages ahead of time, so there is no backlog to purge. You can withdraw a request that has not started by disabling its instruction — that changes what the driver picks next, which is a scheduling edit. Clearing or resetting the port mid-exchange is a different operation: it aborts a transaction that is already on the wire and belongs to fault recovery, not to priority. Let the current transaction reach a defined completion or error outcome, then spend the effort on which instruction requests service next.
Is an 83 ms slot long enough for one transaction? #
Not the slot length itself: a clean 8-register read at 19200 baud takes about 39 ms end to end, so your 83 ms slot fits one transaction with margin — provided the scheduler advances on completion instead of on the timer.
Do the arithmetic at the settings the segment is locked to, using the RTU 11-bit character (1 start, 8 data, 1 parity, 1 stop):
- One character at 19200 baud: 11 bits ÷ 19200 bps = 0.573 ms.
- Read request, function 03: 8 bytes × 0.573 ms = 4.6 ms.
- Response carrying 8 registers (address, function, byte count, 16 data bytes, CRC): 21 bytes × 0.573 ms = 12.0 ms.
- Interframe silence: the Modbus over Serial Line specification (V1.02) requires at least 3.5 character times of quiet between frames, so 3.5 × 0.573 ms = 2.0 ms.
- Device turnaround before the response starts: device-specific; 20 ms is a realistic drive value.
Total: 4.6 + 12.0 + 2.0 + 20 = 38.6 ms, so call one clean read 39 ms. What defeats the scheme is that your counter advances on its own timer: a read that starts late or retries misses its window and idles until the slot comes round next interval, and a write armed mid-sweep queues behind the reads already in line. Measure the elapsed time from service start to completion for each transaction — successes and failures — with the instruction's status bits and the port diagnostics in the programming software; that measured number, not the nominal slot, is what your scheduler must tolerate.
| What you observe | Likely mechanism | Next check |
|---|---|---|
| Counter advances while the selected read is still executing | Scheduling is time-driven, not completion-driven | Replace time slots with completion-controlled sequencing |
| Write waits while reads complete cleanly | Reads sit ahead of the write in service order | Put write stages first in the chain |
| One slave's transactions run far longer than the others | Response time, block size, retries, or a response fault dominates | Inspect that slave's status and physical path |
| Write completes promptly but the machine reacts late | Delay is downstream of the port | Check the target register, device state, and confirmation feedback |
What if the delay is timeouts, not scheduling? #
Fix the link before the scheduler: if exchanges are failing or timing out, ordering instructions cannot help, because one dead slave with retries can hold the bus for 1.5 s at common settings — time that no priority scheme recovers.
Run the failure arithmetic once with your configured values: at a 500 ms timeout with 2 retries, one unresponsive slave occupies the port for 3 × 500 ms = 1500 ms, longer than a whole polling cycle. Before touching sequencing, check:
- slave addresses, with no duplicate on the segment;
- serial settings that match on every node;
- wiring, signal common, and termination appropriate to the installation;
- the suspect device alone, with every other request disabled.
The decision: exchanges that succeed without retries, plus writes that still wait, mean pure scheduling — continue to the next section. Timeouts or retries dominating mean a physical or configuration fault; sequencing on top of a failing bus only reorders the failures.
How do I sequence the port so writes go first? #
Put every transaction for that port in one program, each in its own stage, order the write stages ahead of the read chain, and advance the chain only when the current instruction reports completion — then a write waits at most one in-flight transaction.
- Move every MRX and MWX for that port into one program, one transaction per stage.
- Put the write stages at the top. A write stage runs its MWX while armed and terminates past the read chain when the write reaches a known outcome.
- Directly after the write stages, put a dummy stage whose only rung jumps to the chain's end stage — each read stage jumps to the next read, so this bypass is what closes the loop when no write is armed.
- In each read stage, enable the MRX, wait for the done or error status, then jump to the next read; the last read jumps back to the top.
- Arm write stages from your pending-command latch, never from a timer.
- For reads that only need periodic refresh, the instruction's own interval setting with all boxes enabled is the simple case — plain round-robin often meets an undemanding requirement without any staging.
Some platforms expose a port-busy flag you sequence on manually; here the arbitration is internal, so code order and enable conditions are the only levers. One boundary as commands get faster: priority writes improve response, they do not turn a serial link into a safety function — stops and interlocks stay in the wired safety layer.
Should I combine the slave programs or convert them to tasks? #
Neither move adds serial bandwidth: programs and tasks change when rungs are scanned, while port throughput is set by your enable conditions and the transaction times, so consolidate for visibility, not speed.
The original 10–15 s lag came from every slave's reads being enabled at once, which the driver simply serviced round-robin in code order; relocating that same enabled set into tasks, or into a differently arranged program, changes nothing on the wire. Consolidating the port's transactions into one program is still worth doing, because the stage chain, the jump targets, and the write arming logic become one visible block you can troubleshoot at a glance. Expect maintainability, not milliseconds.
How do I keep change-triggered writes from losing a command? #
Latch the command: record the value to send, mark the write pending the moment the value changes, and clear the pending bit only when the write succeeds with the transmitted value still matching current demand.
The delta contact that fires an MWX only on change was the right traffic cut — repeated writes of unchanged values consume the same transaction capacity as reads. But a change indication is an event, and the port may be busy when it happens, so hold the work:
- Record the value to transmit and set a pending bit when the change is detected.
- Keep the request pending while it waits for service.
- On success, clear pending only if the transmitted value still matches current demand.
- On failure, retain or report the unresolved command per the machine's retry and fault policy.
Decide what a change means before reusing one pattern: a setpoint update can collapse to latest-value handling, while distinct commands — start, stop, recipe steps — each matter and need event-by-event handling. Silently discarding intermediate commands is how a machine ends up in the wrong mode.
When is a second port the right fix? #
Split when the numbers say so: if the measured transaction times for one full sweep exceed the refresh period the process needs, or the baud-capped slave's slow turnaround drags everything, move that slave — or the writes — to a second port.
Add up the measured times: at the example timing, 12 × 38.6 ms = 463 ms of busy bus per sweep, under half the polling interval but with no headroom for writes, retries, or a faster refresh target — and it is why the seven-slave system always felt quicker, its sweep being that much shorter. The moment that total passes the refresh period you actually need, stop tuning the scheduler. Every node on one two-wire segment shares the same serial settings, so the shared segment stays at the slowest device's speed; a second physical network is an architecture change, not a parameter edit. The mechanics of how a PLC runs two Modbus networks at the same time are their own subject.
Questions asked
How do I measure the elapsed time of one Modbus RTU transaction in the PLC?
Start a timer when the instruction's active or busy status first turns on for that request, and read the accumulated value when the success or error status for the same request fires. Log failures separately, because a timeout with retries runs hundreds of milliseconds to seconds while a clean exchange runs tens. Compare the measured worst case against your slot time before shortening any timer.
Can I raise the baud rate above 19200 on just the faster slaves?
Not on one two-wire segment. Every node on an RS-485 multidrop bus must use identical baud, parity, and stop bits, so the segment stays at the slowest device's settings, and raising only the faster nodes corrupts frames for everyone. The change that buys speed is a second port for the capped slave or for the writes, with each segment then set to its own common speed.
What write latency should I expect after sequencing writes first?
With write stages first, the worst case is one in-flight transaction plus the write's own exchange. At the worked-example timing, that is about 39 ms ahead of the write plus roughly 9 ms of wire time for a single-register write and its echo, so comfortably under 100 ms total. Retest after the change; any remaining multi-second lag is a timeout or a downstream delay, not scheduling.
Does the number of programs or tasks sharing the port change its throughput?
No. Program blocks and tasks change when rungs are scanned, not how the driver arbitrates; enabled instructions are still serviced one at a time in code execution order. That order is the useful part: if you keep plain round-robin, placing the MWX write blocks physically ahead of the MRX read blocks in one program gives writes a soft priority without any staging.
How many registers should I read per poll to keep the sweep short?
Read only what the logic uses each cycle. An 8-register block costs about 12 ms of wire time at 19200 with the 11-bit character, and a 32-register block about 41 ms, which is 71 bytes times 0.573 ms, before turnaround. Twelve slaves reading large blocks push the sweep past a comfortable margin in a 1 s cycle, so drop unused registers or split data into fast and slow groups on different intervals.