TapHome

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.

ScriptRunsTypical content
Initialize (module, device)Once after startMODBUSWNE for configuration registers.
Read (module, device)On every pollAlarm registers and ADDERROR.
Read value (device)On every pollMODBUSR and a conversion. The last statement is the device value.
Write value (device)When the value changes, and on every poll with Periodic writeMODBUSW of the device variable, for example the set point.
Service attributeWhen the service settings openMODBUSR turned into text.
Service actionWhen its button is pressedMODBUSW 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, ADDWARNING and ADDINFO work 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.

ModeEffect on scripts
NormalRegisters used by the scripts are read in grouped requests before the scripts run. Reading the same register twice in one run costs one transaction.
IsolatedLike Normal, but the device’s registers are not merged with registers of other devices.
No prefetchNo 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.
  • MODBUSWNE and MODBUSWBLOCKNE compare 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.