Modbus scripts and prefetch
The scripts of a TapHome Modbus module and device — initialize, read, read value, write value, service attributes and service actions — and how prefetch and the register cache affect them.
A Modbus module and each of its devices have several scripts, and the Modbus functions work in all of them. How often the scripts run and which registers they read from the bus depends on the poll interval and the prefetch mode of the device. When each script runs and which variables it sees is described in detail in Modbus integration.
Scripts of Modbus modules and devices
Each Modbus script has its own moment to run and its own typical content.
| Script | Runs | Typical content |
|---|---|---|
| Initialize (module, device) | Once after start | MODBUSWNE for configuration registers. |
| Read (module, device) | On every poll | Alarm registers and ADDERROR. |
| Read value (device) | On every poll | MODBUSR and a conversion. The last statement is the device value. |
| Write value (device) | When the value changes, and on every poll with Periodic write | MODBUSW of the device variable, for example the set point. |
| Service attribute | When the service settings open | MODBUSR turned into text. |
| Service action | When its button is pressed | MODBUSW with the action parameters, available by their abbreviation. |
Remarks
- Variables defined on the module or the device keep their value between script runs — use them to pass a value from a read script to a service attribute. Module variables can be collections from Core 2026.1, build 39507.
ADDERROR,ADDWARNINGandADDINFOwork only in some of these scripts — see ADDERROR.
Example — read value script of a temperature sensor
MODBUSR(H, 20, Int16) / 10;Example — service action with a parameter ID
MODBUSW(H, 20, Float, ID);Prefetch and the register cache
The prefetch mode of a Modbus device decides whether its scripts read registers from the module’s cache or from the bus.
| Mode | Effect on scripts |
|---|---|
| Normal | Registers used by the scripts are read in grouped requests before the scripts run. Reading the same register twice in one run costs one transaction. |
| Isolated | Like Normal, but the device’s registers are not merged with registers of other devices. |
| No prefetch | No cache. Every MODBUSR and MODBUSRBLOCK is a real transaction, in the order written. |
Remarks
- Choose No prefetch for devices that need an exact sequence of transactions; then store a value read once in a variable instead of reading it again.
- Prefer one block read over a loop of single reads —
MODBUSRBLOCK(H, 100, 100)replaces 100 transactions. MODBUSWNEandMODBUSWBLOCKNEcompare with the cached value — see MODBUSWNE.- The size of grouped requests and the prefetch modes are set as described in Prefetch.
Example — one block instead of four reads
collection<uint16> data := MODBUSRBLOCK(H, 100, 4);
VAR voltage := UNPACKVALUE(data, 0, Uint16) / 10;
VAR current := UNPACKVALUE(data, 1, Uint16) / 100;
VAR power := UNPACKVALUE(data, 2, BigEndianFloat);See also — Modbus data types, Examples.