diff --git a/examples/arduino/server-rtu/server-rtu.ino b/examples/arduino/server-rtu/server-rtu.ino index ba2ebf3..15ac5be 100644 --- a/examples/arduino/server-rtu/server-rtu.ino +++ b/examples/arduino/server-rtu/server-rtu.ino @@ -19,125 +19,129 @@ // A single nmbs_bitfield variable can keep 2000 coils nmbs_bitfield server_coils = {0}; -uint16_t server_registers[REGS_ADDR_MAX] = {0}; +uint16_t server_registers[REGS_ADDR_MAX + 1] = {0}; int32_t read_serial(uint8_t* buf, uint16_t count, int32_t byte_timeout_ms, void* arg) { - Serial.setTimeout(byte_timeout_ms); - return Serial.readBytes(buf, count); + Serial.setTimeout(byte_timeout_ms); + return Serial.readBytes(buf, count); } int32_t write_serial(const uint8_t* buf, uint16_t count, int32_t byte_timeout_ms, void* arg) { - Serial.setTimeout(byte_timeout_ms); - return Serial.write(buf, count); + Serial.setTimeout(byte_timeout_ms); + return Serial.write(buf, count); } void onError() { - // Set the led ON on error - while (true) { - digitalWrite(LED_BUILTIN, HIGH); - } + // Set the led ON on error + while (true) { + digitalWrite(LED_BUILTIN, HIGH); + } } -nmbs_error handle_read_coils(uint16_t address, uint16_t quantity, nmbs_bitfield coils_out, uint8_t unit_id, void *arg) { - if (address + quantity > COILS_ADDR_MAX + 1) - return NMBS_EXCEPTION_ILLEGAL_DATA_ADDRESS; +nmbs_error handle_read_coils(uint16_t address, uint16_t quantity, nmbs_bitfield coils_out, uint8_t unit_id, void* arg) { + if (address + quantity > COILS_ADDR_MAX + 1) + return NMBS_EXCEPTION_ILLEGAL_DATA_ADDRESS; - // Read our coils values into coils_out - for (int i = 0; i < quantity; i++) { - bool value = nmbs_bitfield_read(server_coils, address + i); - nmbs_bitfield_write(coils_out, i, value); - } + // Read our coils values into coils_out + for (int i = 0; i < quantity; i++) { + bool value = nmbs_bitfield_read(server_coils, address + i); + nmbs_bitfield_write(coils_out, i, value); + } - return NMBS_ERROR_NONE; + return NMBS_ERROR_NONE; } -nmbs_error handle_write_multiple_coils(uint16_t address, uint16_t quantity, const nmbs_bitfield coils, uint8_t unit_id, void *arg) { - if (address + quantity > COILS_ADDR_MAX + 1) - return NMBS_EXCEPTION_ILLEGAL_DATA_ADDRESS; +nmbs_error handle_write_multiple_coils(uint16_t address, uint16_t quantity, const nmbs_bitfield coils, uint8_t unit_id, + void* arg) { + if (address + quantity > COILS_ADDR_MAX + 1) + return NMBS_EXCEPTION_ILLEGAL_DATA_ADDRESS; - // Write coils values to our server_coils - for (int i = 0; i < quantity; i++) { - nmbs_bitfield_write(server_coils, address + i, nmbs_bitfield_read(coils, i)); - } + // Write coils values to our server_coils + for (int i = 0; i < quantity; i++) { + nmbs_bitfield_write(server_coils, address + i, nmbs_bitfield_read(coils, i)); + } - return NMBS_ERROR_NONE; + return NMBS_ERROR_NONE; } -nmbs_error handler_read_holding_registers(uint16_t address, uint16_t quantity, uint16_t* registers_out, uint8_t unit_id, void *arg) { - if (address + quantity > REGS_ADDR_MAX + 1) - return NMBS_EXCEPTION_ILLEGAL_DATA_ADDRESS; +nmbs_error handler_read_holding_registers(uint16_t address, uint16_t quantity, uint16_t* registers_out, uint8_t unit_id, + void* arg) { + if (address + quantity > REGS_ADDR_MAX + 1) + return NMBS_EXCEPTION_ILLEGAL_DATA_ADDRESS; - // Read our registers values into registers_out - for (int i = 0; i < quantity; i++) - registers_out[i] = server_registers[address + i]; + // Read our registers values into registers_out + for (int i = 0; i < quantity; i++) + registers_out[i] = server_registers[address + i]; - return NMBS_ERROR_NONE; + return NMBS_ERROR_NONE; } -nmbs_error handle_write_multiple_registers(uint16_t address, uint16_t quantity, const uint16_t* registers, uint8_t unit_id, void *arg) { - if (address + quantity > REGS_ADDR_MAX + 1) - return NMBS_EXCEPTION_ILLEGAL_DATA_ADDRESS; +nmbs_error handle_write_multiple_registers(uint16_t address, uint16_t quantity, const uint16_t* registers, + uint8_t unit_id, void* arg) { + if (address + quantity > REGS_ADDR_MAX + 1) + return NMBS_EXCEPTION_ILLEGAL_DATA_ADDRESS; - // Write registers values to our server_registers - for (int i = 0; i < quantity; i++) - server_registers[address + i] = registers[i]; + // Write registers values to our server_registers + for (int i = 0; i < quantity; i++) + server_registers[address + i] = registers[i]; - return NMBS_ERROR_NONE; + return NMBS_ERROR_NONE; } void setup() { - pinMode (LED_BUILTIN, OUTPUT); + pinMode(LED_BUILTIN, OUTPUT); - Serial.begin(9600); - while (!Serial); + Serial.begin(9600); + while (!Serial) + ; } void loop() { - nmbs_platform_conf platform_conf; - nmbs_platform_conf_create(&platform_conf); - platform_conf.transport = NMBS_TRANSPORT_RTU; - platform_conf.read = read_serial; - platform_conf.write = write_serial; - platform_conf.arg = NULL; + nmbs_platform_conf platform_conf; + nmbs_platform_conf_create(&platform_conf); + platform_conf.transport = NMBS_TRANSPORT_RTU; + platform_conf.read = read_serial; + platform_conf.write = write_serial; + platform_conf.arg = NULL; - nmbs_callbacks callbacks; - nmbs_callbacks_create(&callbacks); - callbacks.read_coils = handle_read_coils; - callbacks.write_multiple_coils = handle_write_multiple_coils; - callbacks.read_holding_registers = handler_read_holding_registers; - callbacks.write_multiple_registers = handle_write_multiple_registers; + nmbs_callbacks callbacks; + nmbs_callbacks_create(&callbacks); + callbacks.read_coils = handle_read_coils; + callbacks.write_multiple_coils = handle_write_multiple_coils; + callbacks.read_holding_registers = handler_read_holding_registers; + callbacks.write_multiple_registers = handle_write_multiple_registers; - // Create the modbus server - nmbs_t nmbs; - nmbs_error err = nmbs_server_create(&nmbs, RTU_SERVER_ADDRESS, &platform_conf, &callbacks); - if (err != NMBS_ERROR_NONE) { - onError(); - } + // Create the modbus server + nmbs_t nmbs; + nmbs_error err = nmbs_server_create(&nmbs, RTU_SERVER_ADDRESS, &platform_conf, &callbacks); + if (err != NMBS_ERROR_NONE) { + onError(); + } - nmbs_set_read_timeout(&nmbs, 1000); - nmbs_set_byte_timeout(&nmbs, 100); + nmbs_set_read_timeout(&nmbs, 1000); + nmbs_set_byte_timeout(&nmbs, 100); - bool led = false; + bool led = false; - while (true) { - err = nmbs_server_poll(&nmbs); - // This will probably never happen, since we don't return < 0 in our platform funcs - if (err == NMBS_ERROR_TRANSPORT) - break; + while (true) { + err = nmbs_server_poll(&nmbs); + // This will probably never happen, since we don't return < 0 in our platform funcs + if (err == NMBS_ERROR_TRANSPORT) + break; - digitalWrite(LED_BUILTIN, led); - led = !led; - } + digitalWrite(LED_BUILTIN, led); + led = !led; + } - // No need to destroy the nmbs instance, terminate the program - exit(0); + // No need to destroy the nmbs instance, terminate the program + exit(0); } diff --git a/examples/linux/server-tcp.c b/examples/linux/server-tcp.c index 50dd13b..7743c82 100644 --- a/examples/linux/server-tcp.c +++ b/examples/linux/server-tcp.c @@ -21,7 +21,7 @@ // A single nmbs_bitfield variable can keep 2000 coils bool terminate = false; nmbs_bitfield server_coils = {0}; -uint16_t server_registers[REGS_ADDR_MAX] = {0}; +uint16_t server_registers[REGS_ADDR_MAX + 1] = {0}; uint16_t server_file[FILE_SIZE_MAX];