SDK Script modbus-rtu-master.are

modbus-rtu-master.are
/* DESC: This script can be used to read messages from the serial port.
 * Copyright (C) 2015 NetModule AG, Switzerland
 */
 
DEV = "SERIAL1";
SLAVEID = 1;
 
/* check serial port config */
st = nb_config_get("serial.0.status");
if (st != "2") {
    nb_syslog("Serial port is not enabled for us");
    exit(1);
}
 
/* set attributes */
ret = nb_serial_setattr(DEV, 115200, 8, 1, 0, 0);
 
if (ret != 0) {
    printf("Could not set serial attributes: %i \n",ret);
    exit(1);
}
 
/* open serial port */
fd = nb_serial_open(DEV);
if (fd < 0) {
    printf("Unable to open %s\n", DEV);
    exit(1);
}
 
/* register fd for modbus functions */
if (nb_modbus_register(fd, MODBUS_TYPE_RTU) < 0) {
    printf("Unable to register to modbus\n");
    exit(1);
}
 
/* set slave id */
if (nb_modbus_set_slave(fd, SLAVEID) < 0) {
    printf("Unable to set slave identifier\n");
    exit(1);
}
 
/* write 3 registers start at address 0*/
data = mkarray(0, 1, 2);
nb_modbus_write_regs(fd, 0, length(data), data);
 
/* read 5 registers from address 0*/
data = nb_modbus_read_regs(fd, 5, 5);
 
/* print data */
dump(data);
 
/* unregister, close fd and exit */
nb_modbus_unregister(fd);
 
close(fd);
exit(0);