Time of one transaction
Polling Design: Timing, Timeouts and Retries
On this page
An electrician switches off one energy meter for maintenance. From that minute, the trend of every other meter on the same cable has one point every four seconds where it had three points every second, and a drive fault reaches the screen four seconds late. No setting was changed: the gateway now waits one second, four times, for a meter that cannot answer, and every healthy device waits behind it. This lesson calculates the time of a poll cycle, and chooses the timeout, the retry count and the poll groups so that one silent node costs almost nothing. #Modbus #Polling #Timeouts
Learning Objectives
By the end of this lesson, you will be able to:
- Calculate the time of one Modbus RTU transaction and of a full poll cycle at a given baud rate
- Group registers into reads that use the bus well, inside the limit of 125 registers
- Choose a timeout and a retry count from the longest valid response
- Predict what one dead node does to the cycle, and reduce its cost with a back-off
- Plan fast and slow poll groups, and decide when a higher baud rate is worth it
Real-World System Problem: Five devices on one cable
A compressor house has one RS-485 bus: 180 m of shielded twisted pair with a gateway at one end. On it are four energy meters at addresses 5, 6, 7 and 8, and a variable speed drive at address 10. The gateway is the Modbus client (older texts say “master”) and the five devices are servers (“slaves”). It asks each device in turn, stores each reading with its time, and passes the readings upward.
Only one device may send at a time, so the gateway cannot ask two meters together. Each question and its answer take tens of milliseconds. One pass through all the devices is a poll cycle, and the length of the cycle decides how old a reading can be when it reaches a person or an alarm.
The design is tested on the day a device is switched off or a cable is cut: then the timeout and the retry count decide whether the other four devices are still watched.
Fundamental Theory: Where the time of a poll goes
One transaction
A transaction is one request and its response. It has four parts that follow each other on the bus:
- the client sends the request,
- the line stays silent for the silent interval, which tells the server that the request has ended,
- the server prepares its answer, which takes its turnaround time,
- the server sends the response.
One character on Modbus RTU is 11 bits: a start bit, eight data bits, a parity bit and a stop bit. The time of one character is therefore fixed by the baud rate.
Above 19200 baud the serial line guide recommends a fixed silent interval of 1.750 ms, because 3.5 characters become too short for simple devices to measure. So at 38400 baud, use 1.750 ms and not
A function 03 request is always 8 bytes: address, function code, two bytes of start address, two bytes of quantity and two bytes of CRC. The response to a read of
The poll cycle
The poll cycle is the sum of the transactions in it.
Time of a poll cycle
The client must leave at least one silent interval after a response before it speaks again, and many clients have a setting for a longer pause. The applications of this lesson take
A client can start the next cycle as soon as the last one ends, or it can start a cycle on a fixed period, for example every second. The second way gives evenly spaced readings, but only while the cycle is shorter than the period.
One large read against several small reads
Write the transaction time as a fixed cost plus a cost for each register.
Cost of a read of N registers
The 16.5 characters are the 8 bytes of the request, the 3.5 characters of silence and the 5 bytes around the data in the response. This form holds up to 19200 baud.
At 9600 baud with a turnaround of 20 ms, the fixed cost is
Take the nine registers of a meter, 40001 to 40009:
- one read of nine registers:
, - nine reads of one register each:
.
The single read is more than six times faster, and its nine values come from the same instant.
There is an upper limit. Function 03 can return at most 125 registers in one response, because a serial frame is limited to 256 bytes and
Three things stop you from always using one large read:
- Gaps in the map. Some servers return an exception when a read includes an address they do not define. Meter A has no register 40010, so its map is read as two blocks: 40001 to 40009 and 40011 to 40014.
- Distance in the map. Reading 100 unwanted registers to reach two wanted ones costs
, far more than a second request at 38.91 ms. A gap is worth reading through only when it is shorter than about 17 registers at these settings, since . - Noise. A long frame has more bits that can be damaged, and one damaged bit loses the whole frame. On a bus with occasional errors, several medium reads lose less than one very long read.
Timeout and retries
The client cannot know that a server is silent; it can only wait, and the timeout is how long. In this course the timeout runs from the last byte of the request to the last byte of the response. Some clients measure to the first byte of the response, so read the manual of yours.
A timeout that is too short cuts off healthy servers. A timeout that is too long gives the bus to a dead one. The lower limit comes from the slowest valid response on the bus.
Choosing the timeout
A retry is a second attempt after a timeout or a bad CRC, to recover from a frame damaged by noise, a rare and short event. One retry is enough: if one frame in a hundred is lost, two losses in a row happen once in ten thousand polls. More retries add nothing against noise and cost a full timeout each against a server that is switched off.
Time lost to a server that does not answer
What one dead node does
The cure has two parts: a short timeout with one retry, and a back-off, which polls a failed node less often until it answers again.
Average cycle with a back-off
While a node is marked dead, its readings must be recorded as missing, with the time of the last good one. A back-off that hides the failure would be worse than a slow cycle.
Fast and slow poll groups
Power and current move within a second. An energy counter is a running total, so it loses nothing if it is read less often. Split the plan into groups.
For this bus, the fast group is the nine-register block of each meter and the status block of the drive: 286.2 ms at 9600 baud. The slow group is the energy block of each meter (four registers from 40011, 48.07 ms each).
| Plan | Longest cycle | Average cycle |
|---|---|---|
| Everything in every cycle | 478.5 ms | 478.5 ms |
| Fast group every cycle, all four energy reads in every tenth cycle | 478.5 ms | 305.4 ms |
| Fast group every cycle, one energy read in each of four cycles out of ten | 334.3 ms | 305.4 ms |
The third plan reads the energy of meter 5 in cycle 1, meter 6 in cycle 2, meter 7 in cycle 3 and meter 8 in cycle 4, and nothing extra in cycles 5 to 10. It has the same average as the second plan and a much shorter longest cycle, so spread the slow reads.
When to raise the baud rate
A higher baud rate shortens the characters and the silent interval. It does not shorten the turnaround. For this bus:
| Baud rate | Meter read | Drive read | Cycle |
|---|---|---|---|
| 9600 | 59.53 ms | 48.07 ms | 286.2 ms |
| 19200 | 39.77 ms | 34.04 ms | 193.1 ms |
| 38400 | 30.63 ms | 27.77 ms | 150.3 ms |
Four times the baud rate gives a cycle that is not even twice as fast. However high the rate, the cycle cannot go below five turnarounds and five silent intervals:
Application 1: The poll cycle of the bus at two baud rates
Step 1: Character time and silent interval
Click to reveal the two basic times
-
At 9600 baud.
One character lasts 1.146 ms. ✅
-
At 19200 baud.
Both values are half of those at 9600 baud. 19200 baud is the last rate at which the silent interval is still 3.5 characters. ✅
Step 2: The two transactions
Click to reveal the transaction times
-
Meter read at 9600 baud. The request is 8 bytes and the response is 23 bytes.
One meter read takes 59.53 ms. ✅
-
Drive read at 9600 baud. The response is 13 bytes.
One drive read takes 48.07 ms. ✅
-
Meter read at 19200 baud.
The characters took half the time. The turnaround did not change. ✅
-
Drive read at 19200 baud.
One drive read takes 34.04 ms. ✅
Step 3: The full cycle
Click to reveal the cycle times
-
At 9600 baud.
The gateway can complete about 3.5 cycles in each second. ✅
-
At 19200 baud.
The cycle is 1.48 times faster, not 2 times. ✅
-
Share of a one-second period. At 9600 baud the bus is busy for
percent of each second, and at 19200 baud for 19.3 percent. Both leave room for the slow group and for retries. ✅
Step 4: Check by adding the parts
Click to reveal the second calculation
-
Count the characters in one cycle. Each meter transaction has
characters and the drive transaction has .There are also 5 silent intervals and 5 turnarounds. ✅
-
Add the three parts at 9600 baud.
The total agrees with Step 3. ✅
-
The same at 19200 baud.
The turnaround is now more than half of the cycle. ✅
-
Check with the script. The Python script below prints a meter read of 59.53 ms, a drive read of 48.07 ms and a cycle of 286.2 ms at 9600 baud, and 39.77 ms, 34.04 ms and 193.1 ms at 19200 baud. ✅
Check it on the running bus. Most gateways stamp each reading with its time. Over an hour of normal running, the time between two readings of the same meter is the real cycle: about 286 ms at 9600 baud here, or 306 ms with the minimum pause. A much longer cycle means a slow server, a pause setting in the client, or a node that times out. Then read the counters of each address: a healthy server must have no timeouts in a day. A few timeouts on a healthy server mean that the timeout is too close to its slowest answer.
Application 2: One meter is switched off
Step 1: The servers that answer
Click to reveal the live part of the cycle
-
Three meters and the drive.
This part is the same for all three settings. ✅
Step 2: Timeout of 1 s and 3 retries
Click to reveal the cycle with setting 1
-
One attempt. The gateway sends the request and waits for the full timeout:
. ✅ -
Four attempts.
The dead meter holds the bus for four seconds in every cycle. ✅
-
The cycle.
The healthy cycle was 286.2 ms. The cycle is now 14.9 times longer, and a fault bit in the drive status can be 4.3 s old before the gateway reads it. ✅
Step 3: Timeout of 200 ms and 1 retry
Click to reveal the cycle with setting 2
-
Is 200 ms safe? The longest valid response on this bus is the 23 bytes of a meter. After the request ends, a healthy meter needs
Three times that is 151 ms, and 200 ms is the next convenient value above it. A healthy server would have to be almost four times slower than normal before the timeout cut it off. ✅
-
Two attempts.
The dead meter now costs under half a second. ✅
-
The cycle.
This is 6.6 times shorter than with setting 1, and still 2.25 times the healthy cycle. ✅
Step 4: Add the back-off
Click to reveal the average cycle with setting 3
-
The pattern. While the meter stays off, the cycles repeat in groups of 11: ten cycles that skip the meter, then one cycle with a single probe. ✅
-
The two kinds of cycle. A cycle that skips the meter takes
. A probe is one attempt, , so a probe cycle takes . ✅ -
The average.
The formula of the theory section gives the same:
. ✅ -
Time to find the meter again. In the worst case the meter is switched on just after a probe has failed. The gateway then runs ten skip cycles and one probe cycle before it hears the meter:
A meter that returns is seen within 2.7 s. ✅
Step 5: Check with limiting cases
Click to reveal the checks
-
No skipping. With
the gateway probes in every cycle, and the formula gives : the probe cycle of Step 4, as it must. ✅ -
Very long skipping. As
grows, the average falls toward , the cycle of a bus that has only four servers. The price is the time to find the meter again, which grows with . ✅ -
Check with the script. The Python script prints 4,263.3 ms for setting 1, 645.0 ms for setting 2, and an average of 245.7 ms with a longest search of 2.70 s for setting 3. All agree with the hand values. ✅
Verify It in Python
The script calculates the transaction and cycle times of Application 1 at three baud rates, the cost of grouping, the three settings of Application 2 and the poll group plans. It saves a bar chart of the cycle time in the four cases.
"""Poll cycle of a Modbus RTU bus: transaction times, a dead node, poll groups."""import numpy as npimport matplotlib.pyplot as plt
BITS = 11 # bits in one RTU characterT_TURN = 20.0 # ms, server turnaroundN_REQ = 8 # bytes in a function 03 request
def char_ms(baud): return 1000.0 * BITS / baud
def silent_ms(baud): """3.5 characters up to 19200 baud, fixed 1.750 ms above it.""" return 3.5 * char_ms(baud) if baud <= 19200 else 1.750
def response_bytes(registers): return 5 + 2 * registers
def read_ms(baud, registers): """Request, silent interval, turnaround, response.""" chars = N_REQ + response_bytes(registers) return chars * char_ms(baud) + silent_ms(baud) + T_TURN
def fail_ms(baud, timeout, attempts): """A server that never answers: each attempt is a request plus the timeout.""" return attempts * (N_REQ * char_ms(baud) + timeout)
# ---------- Application 1: the cycle of bus C ----------print("Application 1: four meters (9 registers) and the drive (4 registers)")cycle = {}for baud in (9600, 19200, 38400): t_m, t_d = read_ms(baud, 9), read_ms(baud, 4) cycle[baud] = 4 * t_m + t_d print(f" {baud:5d} baud: character {char_ms(baud):.3f} ms, " f"silent {silent_ms(baud):.3f} ms, meter {t_m:.2f} ms, " f"drive {t_d:.2f} ms, cycle {cycle[baud]:.1f} ms")print(f" characters in one cycle: {4 * (8 + 23) + (8 + 13)}, " f"turnaround in one cycle: {5 * T_TURN:.0f} ms")print(f" share of a 1 s poll period at 9600 baud: {cycle[9600] / 10:.1f} %")print(f" with a 3.5 character gap after each response: " f"{cycle[9600] + 5 * silent_ms(9600):.1f} ms")
# ---------- One large read against several small reads ----------print("Grouping at 9600 baud")print(f" fixed cost of one read: {read_ms(9600, 0):.2f} ms, " f"each register adds {2 * char_ms(9600):.2f} ms")print(f" 9 registers in 1 read: {read_ms(9600, 9):.2f} ms, " f"in 9 reads: {9 * read_ms(9600, 1):.1f} ms")print(f" 125 registers: response {response_bytes(125)} bytes, " f"{read_ms(9600, 125):.1f} ms")
# ---------- Application 2: meter 7 is switched off ----------BAUD = 9600t_m, t_d = read_ms(BAUD, 9), read_ms(BAUD, 4)alive = 3 * t_m + t_dprint("Application 2: meter 7 off, 9600 baud")print(f" the four live servers: {alive:.1f} ms")slow = fail_ms(BAUD, 1000.0, 4)quick = fail_ms(BAUD, 200.0, 2)probe = fail_ms(BAUD, 200.0, 1)print(f" timeout 1000 ms, 3 retries: dead node {slow:.1f} ms, " f"cycle {alive + slow:.1f} ms")print(f" timeout 200 ms, 1 retry : dead node {quick:.1f} ms, " f"cycle {alive + quick:.1f} ms")SKIP = 10average = (SKIP * alive + (alive + probe)) / (SKIP + 1)print(f" back-off, skip {SKIP} cycles, 1 probe: probe cycle " f"{alive + probe:.1f} ms, average {average:.1f} ms")print(f" longest time to find the meter again: " f"{(SKIP * alive + alive + probe) / 1000:.2f} s")after_request = silent_ms(BAUD) + T_TURN + 23 * char_ms(BAUD)print(f" longest valid response after the request: {after_request:.2f} ms, " f"times 3: {3 * after_request:.0f} ms")
# ---------- Fast and slow poll groups ----------t_e = read_ms(BAUD, 4) # energy block, 4 registersfast = 4 * t_m + t_dprint("Poll groups at 9600 baud (energy block of each meter: " f"{t_e:.2f} ms)")print(f" everything every cycle: {fast + 4 * t_e:.1f} ms")together = [fast] * 9 + [fast + 4 * t_e]spread = [fast + t_e] * 4 + [fast] * 6for name, plan in (("energy in one cycle of ten", together), ("one energy read in four cycles of ten", spread)): print(f" {name}: longest {max(plan):.1f} ms, " f"average {np.mean(plan):.1f} ms")
# ---------- Plot ----------labels = ["all answer", "1000 ms, 3 retries", "200 ms, 1 retry", "back-off (average)"]live = [4 * t_m + t_d, alive, alive, alive]dead = [0.0, slow, quick, probe / (SKIP + 1)]fig, ax = plt.subplots(figsize=(8, 3.6))ax.barh(labels, live, color="tab:blue", label="servers that answer")ax.barh(labels, dead, left=live, color="tab:gray", label="waiting for meter 7")ax.invert_yaxis()ax.set_xlabel("cycle time (ms)")ax.set_title("Poll cycle of the bus at 9600 baud")ax.legend(loc="lower right")fig.tight_layout()fig.savefig("poll_cycle.png", dpi=120)print("Saved poll_cycle.png")Expected output
Application 1: four meters (9 registers) and the drive (4 registers) 9600 baud: character 1.146 ms, silent 4.010 ms, meter 59.53 ms, drive 48.07 ms, cycle 286.2 ms 19200 baud: character 0.573 ms, silent 2.005 ms, meter 39.77 ms, drive 34.04 ms, cycle 193.1 ms 38400 baud: character 0.286 ms, silent 1.750 ms, meter 30.63 ms, drive 27.77 ms, cycle 150.3 ms characters in one cycle: 145, turnaround in one cycle: 100 ms share of a 1 s poll period at 9600 baud: 28.6 % with a 3.5 character gap after each response: 306.2 msGrouping at 9600 baud fixed cost of one read: 38.91 ms, each register adds 2.29 ms 9 registers in 1 read: 59.53 ms, in 9 reads: 370.8 ms 125 registers: response 255 bytes, 325.4 msApplication 2: meter 7 off, 9600 baud the four live servers: 226.7 ms timeout 1000 ms, 3 retries: dead node 4036.7 ms, cycle 4263.3 ms timeout 200 ms, 1 retry : dead node 418.3 ms, cycle 645.0 ms back-off, skip 10 cycles, 1 probe: probe cycle 435.8 ms, average 245.7 ms longest time to find the meter again: 2.70 s longest valid response after the request: 50.36 ms, times 3: 151 msPoll groups at 9600 baud (energy block of each meter: 48.07 ms) everything every cycle: 478.5 ms energy in one cycle of ten: longest 478.5 ms, average 305.4 ms one energy read in four cycles of ten: longest 334.3 ms, average 305.4 msSaved poll_cycle.pngIn the plot, the grey part of each bar is the time spent waiting for the meter that is switched off.
Common Mistakes
- Leaving the timeout and the retry count at the values the software came with. A timeout of 1 s with 3 retries turns one dead node into four seconds of waiting in every cycle.
- Estimating the cycle from the baud rate alone. The turnaround of the servers is often the larger part, and it does not change with the rate.
- Reading each register with its own request. The fixed cost of a request at 9600 baud is equal to about 17 registers of data.
- Asking for more than 125 registers with function 03, or reading across an address the server does not define. Both return an exception, not data.
- Showing the last good value while a node is skipped. A reading that is missing must be recorded as missing.
Summary
- One transaction is the request, the silent interval, the server turnaround and the response, with 11 bits for each character. A nine-register read takes 59.53 ms at 9600 baud and 39.77 ms at 19200 baud with a turnaround of 20 ms.
- The poll cycle is the sum of its transactions: 286.2 ms at 9600 baud and 193.1 ms at 19200 baud for four meters and the drive.
- Each request has a fixed cost, so one read of neighbouring registers is faster than several small reads, up to the limit of 125 registers and without crossing an undefined address.
- Set the timeout at about three times the longest valid response, and use one retry.
- A dead node costs its timeout times its attempts in every cycle. A back-off that skips it for some cycles and probes it once reduces the cost to a few percent.
- Read fast values in every cycle and slow values in spread turns, and raise the baud rate only when the characters, not the turnaround, are the larger part of the cycle.
Exercises
1. The bus of Application 1 is set to 57600 baud. The turnaround stays at 20 ms. Find the meter read, the drive read and the cycle. Take care with the silent interval.
Answer
2. A logger needs 60 neighbouring holding registers from one server at 19200 baud, with a turnaround of 20 ms. Compare one read of 60 registers with six reads of 10 registers.
Answer
One read: the response is 125 bytes, so
3. On the bus of Application 2 at 9600 baud, the meters at addresses 6 and 7 are both switched off. The timeout is 500 ms with 2 retries, and there is no back-off. Find the cycle time.
Answer
The live part is
4. For the back-off of Application 2 (probe of 209.2 ms, live part of 226.7 ms), find the smallest number of skipped cycles
Answer
5. A drive at 19200 baud has a measured turnaround of up to 45 ms. The longest read in the plan is 60 registers. Choose a timeout with the rule of this lesson.
Answer
The response is 125 bytes, which take
References
- Modbus Organization, MODBUS Application Protocol Specification V1.1b3, 2012.
- Modbus Organization, MODBUS over Serial Line Specification and Implementation Guide V1.02, 2006.
- Telecommunications Industry Association, TIA/EIA-485-A: Electrical Characteristics of Generators and Receivers for Use in Balanced Digital Multipoint Systems, 1998.
A gateway polls four meters and a drive on one bus, and one cycle takes 286 ms. Its timeout is 1 s and it retries 3 times. An electrician switches one meter off. How long is the cycle now?
The bar is one cycle, drawn to scale, and the number above each part is the address of the server. The gateway reads each meter, then the four-register status block of the drive at address 10, as in the lesson. The times follow the lesson's rules: 11 bits for each character, a silent interval of 3.5 characters up to 19200 baud and 1.750 ms above it, and no pause after a response. Add the pause of your own client when you plan a real bus. A meter that is off costs the request plus the full timeout for every attempt. With the back-off, the gateway skips that meter for 10 cycles and then sends one request with no retry, so the bar shows that one probe cycle in every 11. While a meter is skipped, its readings must be recorded as missing.
Check yourself
Three questions. Each wrong choice is a mistake people really make, so read the note after you answer.
1. The lesson's bus has a cycle of 286.2 ms at 9600 baud, with a turnaround of 20 ms in every server. The baud rate is doubled to 19200. What is the new cycle?
2. At 9600 baud with a turnaround of 20 ms, one read of nine registers takes 59.53 ms. A client reads the same nine registers with nine requests of one register each. How long does that take?
3. On the lesson's bus a healthy meter needs 50.36 ms from the end of the request to the end of its response. The gateway retries once. Which timeout is the best choice?
- Meter 7 is off. The client sends its request and can only wait: no answer will come.
- The slowest valid answer ends 50 ms after the request. Three times that is 151 ms: set 200 ms.
- One retry is a second request and a second 200 ms. The silent meter costs 418 ms.
- With the usual 1 s timeout and 3 retries, the cycle grows from 286 ms to 4,263 ms.
- With a 200 ms timeout and one retry, the cycle is 645 ms.
- Back-off: skip the dead meter for 10 cycles, then probe once. The average cycle is 246 ms.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.