📦 Archived documentation: r1 (committed 2026-07-24) View current documentation →

Modbus TCP Data Table From Logic

Creates a Modbus slave register table backed by a bin buffer that the script updates and remote Modbus TCP clients can read and write. Includes modpoll test commands.

// This example creates a modbus data table that can be updated from logic and be read and written remotely via modbusTCP

let mbData = bin.alloc(20*4); // Allocate a modbus buffer with room for 18 float registers
modbus.setData(mbData); // Set the modbus data table to use our variable

while(run()){


if(modbus.setLock(true)){ // Lock the table from reads while data is being updated


  mbData.seek(0); // set the file position to the begining of the data table (byte 0)

  // write some values to our register table
  mbData.write("F32BADC",[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]); // append an array of values as float32's
  mbData.write("U32BADC",23.33); // append a single value as a unsigned 32 bit int with badc byte order

  // read 2 values back from the end of our register table (these values could be written to remotely using modbus set the holding register functions)
  let reg17_18 = mbData.read("F32BADC",2);
  print(string(reg17_18)); // print the values;

  modbus.setLock(false); // unlock the data table so that clients can read and write again
}





  waitMS(1000);
}

// Test using the free modpoll tool https://www.modbusdriver.com/modpoll.html

// read our devices register table
// modpoll.exe -0 -m tcp -t 4:float -r 0 -c 18 -a 0 -1 192.168.1.20

// write to the last two floats in our register table
// modpoll.exe -0 -m tcp -t 4:float -r32  -u -a 0 -1 192.168.1.20 123.456 456.789