modbus.write()
modbus.write(port, unitId, functionCode, startReg, count, data) → number
Writes registers or coils to a Modbus slave.
Writes packed binary data to the slave. Build the buffer from a numeric
array with bin.fromArray(array, format, byteOrder).
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. |
| functionCode | number | yes | — | Function code, e.g. 16 = write multiple registers. FC 15 expects a bit array. |
| startReg | number | yes | — | Starting register address. |
| count | number | yes | — | Number of registers or coils to write. |
| data | bin buffer | yes | — | Packed data, e.g. from bin.fromArray([1,2,3], "U16", "AB"). |
Returns
number
0 on success, otherwise an error code.
Example
let arrayData = [1,2,3,4,5,6,7,8];
let data = bin.fromArray(arrayData, "U16", "AB");
let ret = modbus.write(0, 1, 16, 0, arrayData.length, data);
if(ret === 0){ print("Write Successful"); }
else { print("Modbus Write Failed, err=", ret); }