diff --git a/examples/arduino/README.md b/examples/arduino/README.md index 5e02ea7..f1ca0d5 100644 --- a/examples/arduino/README.md +++ b/examples/arduino/README.md @@ -1,4 +1,6 @@ ## Arduino examples This folder contains nanoMODBUS examples for Arduino. -To build and load a sketch with the Arduino IDE, copy `nanomodbus.c` and `nanomodbus.h` inside its folder. \ No newline at end of file +To build and load a sketch with the Arduino IDE, copy `nanomodbus.c` and `nanomodbus.h` inside its folder. + +`client-rtu` and `server-rtu` are meant to be used with two Arduinos connected via their TX0/RX0 serial pins. diff --git a/examples/arduino/client-rtu/client-rtu.ino b/examples/arduino/client-rtu/client-rtu.ino index b113baf..808db8c 100644 --- a/examples/arduino/client-rtu/client-rtu.ino +++ b/examples/arduino/client-rtu/client-rtu.ino @@ -33,7 +33,7 @@ void onError() { void setup() { pinMode (LED_BUILTIN, OUTPUT); - digitalWrite(LED_BUILTIN, HIGH); + digitalWrite(LED_BUILTIN, LOW); Serial.begin(9600); while (!Serial); @@ -82,9 +82,9 @@ void loop() { if (err != NMBS_ERROR_NONE) onError(); - // Turn off the led on success - digitalWrite(LED_BUILTIN, LOW); + // On success, keep the led on + digitalWrite(LED_BUILTIN, HIGH); - // No need to destroy the nmbs instance, bye bye + // No need to destroy the nmbs instance, terminate the program exit(0); } diff --git a/examples/arduino/server-rtu/server-rtu.ino b/examples/arduino/server-rtu/server-rtu.ino index ea2cc9c..166f3a5 100644 --- a/examples/arduino/server-rtu/server-rtu.ino +++ b/examples/arduino/server-rtu/server-rtu.ino @@ -1,5 +1,5 @@ /* - This example application sets up an RTU server and polls from modbus requests + This example application sets up an RTU server and handles modbus requests This server supports the following function codes: FC 01 (0x01) Read Coils @@ -35,12 +35,9 @@ int32_t write_serial(const uint8_t* buf, uint16_t count, int32_t byte_timeout_ms void onError() { - // Make the LED blink on error + // Set the led ON on error while (true) { digitalWrite(LED_BUILTIN, HIGH); - delay(1000); - digitalWrite(LED_BUILTIN, LOW); - delay(1000); } } @@ -98,7 +95,6 @@ nmbs_error handle_write_multiple_registers(uint16_t address, uint16_t quantity, void setup() { pinMode (LED_BUILTIN, OUTPUT); - digitalWrite(LED_BUILTIN, HIGH); Serial.begin(9600); while (!Serial); @@ -112,7 +108,6 @@ void loop() { platform_conf.write = write_serial; platform_conf.arg = NULL; - // These functions are defined in server.h nmbs_callbacks callbacks = {0}; callbacks.read_coils = handle_read_coils; callbacks.write_multiple_coils = handle_write_multiple_coils; @@ -129,16 +124,18 @@ void loop() { nmbs_set_read_timeout(&nmbs, 1000); nmbs_set_byte_timeout(&nmbs, 100); + 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; + + digitalWrite(LED_BUILTIN, led); + led = !led; } - // Turn off the led at the end - digitalWrite(LED_BUILTIN, LOW); - - // No need to destroy the nmbs instance, bye bye + // No need to destroy the nmbs instance, terminate the program exit(0); }