Serial Modbus (RS-485)

Polls a Modbus RTU slave directly over the RS-485 port: initialise the port with serial.init(), then use modbus.read() with the matching port number and parse the returned registers as word-swapped floats.

let port = 1;  // Available ports vary depending on the device. Refer to the serial.init documentation

let ret = serial.init(port, 9600);

while(run()){
  let res = modbus.read(port,
                        1,  // slave unitID
                        3,  // function code
                        8,  // start register
                        10  // register count
                       );
  print(res.status);

  if(res.status === 0){
    res.data.seek(0);
    let registers = res.data.read("F32BADC", 8);

    print("voltage (reg0)=", registers[0]);
  }

  waitMS(1000);
}