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

modbus.connect()

modbus.connect(host, port, unitId) → number

Opens a Modbus TCP connection to a remote device.

Arguments

NameTypeRequiredDefaultDescription
host string yes IP address of the Modbus device, e.g. "172.16.220.111".
port number yes TCP port, normally 502.
unitId number yes Slave unit ID.

Returns

number 0 on success, otherwise an error code.

Example

let ret = modbus.connect("172.16.220.111", 502, 1);
if(ret === 0){
  // read / write ...
  modbus.disconnect();
} else {
  print("Modbus Connect Failed, err=", ret);
}