modbus.read()
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));
}