Master — modbus.connect() / modbus.read() / modbus.write() poll and
command remote Modbus devices over TCP, or over the RS-485 serial ports
(Modbus RTU — initialise the port with serial.init() first; see the
Serial Modbus (RS-485) example).
Slave data table — modbus.setData() / modbus.setLock() expose a
register table, backed by a bin buffer your script updates, that remote
Modbus TCP clients can read and write.
Register data is packed binary — use the bin buffer read/write methods
with format strings like "F32BADC" or "U32DCBA" to convert between
registers and numbers. Test slave tables with the free
modpoll tool.
Function codes 1, 2 and 15 expect a bit array rather than register data.
Functions
modbus.connect() — Opens a Modbus TCP connection to a remote device.
modbus.read() — Reads registers or coils from a Modbus slave.
modbus.write() — Writes registers or coils to a Modbus slave.
modbus.setData() — Backs the device Modbus slave register table with a script-managed bin buffer.
modbus.setLock() — Locks/unlocks the slave data table so clients never read a half-updated table.
modbus.disconnect() — Closes the master connection to the remote Modbus device.
modbus.read(port, unitId, fc, startReg, count) → { status, data }
Reads registers or coils from a Modbus slave.
Reads from a connected slave and returns { status, data } where status is
0 on success and data is a binary buffer. Parse the buffer with
data.seek(0) then repeated data.read(format) calls.
Arguments
Name
Type
Required
Default
Description
port
number
yes
—
Communication port: 0 = TCP, >0 = Hardware Serial Port (serial ports need serial.init() first).
unitId
number
yes
—
Slave unit ID.
fc
number
yes
—
Modbus function code, e.g. 3/4 = read registers. FCs 1/2 return a bit array.
startReg
number
yes
—
Starting register address, e.g. 0x88B9.
count
number
yes
—
Number of registers or coils to read.
Returns
object
{ status, data } — status 0 = success; data is a bin buffer.
Example
let ret = modbus.read(0, // 0=tcp, >0 = physical serial port
1, // slave unitID
4, // function code
0x88B9, // start register
30); // register count
if(ret.status === 0){
ret.data.seek(0);
let data = {
meanWindSpeed: ret.data.read("U32DCBA")/10, // m/s
meanWindDirection: ret.data.read("U32")/10, // degrees
airTemp: ret.data.read("S32")/10, // °C
};
print(string(data));
} else {
print("Read Failed, err=", string(ret));
}
Backs the device Modbus slave register table with a script-managed bin buffer.
Points the device's Modbus TCP slave data table at a buffer allocated with
bin.alloc(). Remote clients then read and write your table; your script
updates it through the buffer's seek/read/write methods, guarded by
modbus.setLock().
Arguments
Name
Type
Required
Default
Description
buffer
bin buffer
yes
—
Buffer from bin.alloc(bytes) sized for your registers (4 bytes per 32-bit value).
Example
let mbData = bin.alloc(20*4); // room for float registers
modbus.setData(mbData); // expose as the slave data table
Locks/unlocks the slave data table so clients never read a half-updated table.
Lock the table before updating it from your script, then unlock so clients can
read and write again. modbus.setLock(true) returns true when the lock was
acquired.