This shows you the differences between two versions of the page.
— | sdk:scripts:modbus-rtu-slave [2015/05/05 15:04] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== SDK Script modbus-rtu-slave.are ====== | ||
+ | <code c modbus-rtu-slave.are> | ||
+ | /* DESC: This script implements a modbus slave server | ||
+ | * Copyright (C) 2015 NetModule AG, Switzerland | ||
+ | */ | ||
+ | |||
+ | DEV = " | ||
+ | SLAVEID = 1; | ||
+ | DEBUG = 0; | ||
+ | |||
+ | /* check serial port config */ | ||
+ | st = nb_config_get(" | ||
+ | if (st != " | ||
+ | nb_syslog(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | /* set attributes */ | ||
+ | ret = nb_serial_setattr(DEV, | ||
+ | |||
+ | if (ret != 0) { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | /* open serial port */ | ||
+ | fd = nb_serial_open(DEV); | ||
+ | if (fd < 0) { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | status = nb_config_get(" | ||
+ | if (status == " | ||
+ | printf(" | ||
+ | } else { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | /* register fd for modbus functions */ | ||
+ | if (nb_modbus_register(fd, | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | if (DEBUG > 0) { | ||
+ | /* enable debug mode */ | ||
+ | if (nb_modbus_set_debug(fd, | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /* set slave id */ | ||
+ | if (nb_modbus_set_slave(fd, | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | for (ctr = 0;;ctr++) { | ||
+ | request = nb_modbus_receive(fd); | ||
+ | if (request == NULL) { | ||
+ | printf(" | ||
+ | break; | ||
+ | } | ||
+ | | ||
+ | resp = mkstruct( | ||
+ | " | ||
+ | ( | ||
+ | ctr%2, 0, 0, 0, 1, 1, 1, 1, | ||
+ | 0 , 0, 0, 0, 1, 1, 1, 1 | ||
+ | ), | ||
+ | " | ||
+ | ( | ||
+ | ctr%2, 1, 0, 1, 0, 1, 0, 1, | ||
+ | 0 , 1, 0, 1, 0, 1, 0, 1 | ||
+ | ), | ||
+ | " | ||
+ | ( | ||
+ | ctr, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, | ||
+ | 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F | ||
+ | ), | ||
+ | " | ||
+ | ( | ||
+ | ctr, 0xFF01, 0xFF02, 0xFF03, 0xFF04, 0xFF05, 0xFF06, 0xFF07, | ||
+ | 0xFF08, 0xFF09, 0xFF0A, 0xFF0B, 0xFF0C, 0xFF0D, 0xFF0E, 0xFF0F | ||
+ | ) | ||
+ | ); | ||
+ | |||
+ | ret = nb_modbus_reply(fd, | ||
+ | if (ret < 0) { | ||
+ | printf(" | ||
+ | } else { | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | nb_modbus_unregister(fd); | ||
+ | close(fd); | ||
+ | exit(0); | ||
+ | |||
+ | |||
+ | </ | ||