02-24-2026, 05:47 PM
@Rudy M
Your current approach (continuous loop + _LIMIT pollHz!) is perfectly fine and is the simplest, most robust way to poll MODBUS sensors on a PC. For MODBUS RTU there is no “push” unless you enable auto-report, so polling is the normal model.
I would not use ON TIMER ... GOSUB for serial MODBUS polling. Reason: TIMER events can fire while you’re still reading/writing the port, which can create re-entrancy problems (overlapping requests, partial frames, CRC fails) unless you add a lock/busy flag and very careful buffering. A single-threaded main loop is safer.
Recommended pattern:
Keep a main loop that polls at a fixed rate: e.g. 2 Hz (every 0.5 s) or 1 Hz.
After each successful read, copy results into Temp!(0..7) (and maybe store a timestamp).
The rest of your program (control logic / GUI) just reads the latest Temp!() values. If a read fails, keep the old values and set an error flag or “age” counter.
So: if you want “every 0.5 seconds”, don’t use ON TIMER. Just set:
pollHz! = 2 (because 2 polls per second = 0.5 s)
or use Timer (not ON Timer but Timer only):
pseudocode:
If timer < oldtimer then
oldtimer = timer + .5
read and print temperatures
end if
Extra practical notes:
MODBUS requests should not be too fast; 0.5–1.0 s is a good start for stability.
If you later run multiple devices (relays + sensors) on the same RS485 bus, keep polling orderly: one request at a time, wait for reply, then next request.
Your current approach (continuous loop + _LIMIT pollHz!) is perfectly fine and is the simplest, most robust way to poll MODBUS sensors on a PC. For MODBUS RTU there is no “push” unless you enable auto-report, so polling is the normal model.
I would not use ON TIMER ... GOSUB for serial MODBUS polling. Reason: TIMER events can fire while you’re still reading/writing the port, which can create re-entrancy problems (overlapping requests, partial frames, CRC fails) unless you add a lock/busy flag and very careful buffering. A single-threaded main loop is safer.
Recommended pattern:
Keep a main loop that polls at a fixed rate: e.g. 2 Hz (every 0.5 s) or 1 Hz.
After each successful read, copy results into Temp!(0..7) (and maybe store a timestamp).
The rest of your program (control logic / GUI) just reads the latest Temp!() values. If a read fails, keep the old values and set an error flag or “age” counter.
So: if you want “every 0.5 seconds”, don’t use ON TIMER. Just set:
pollHz! = 2 (because 2 polls per second = 0.5 s)
or use Timer (not ON Timer but Timer only):
pseudocode:
If timer < oldtimer then
oldtimer = timer + .5
read and print temperatures
end if
Extra practical notes:
MODBUS requests should not be too fast; 0.5–1.0 s is a good start for stability.
If you later run multiple devices (relays + sensors) on the same RS485 bus, keep polling orderly: one request at a time, wait for reply, then next request.

