Fixes to arduino examples
This commit is contained in:
parent
2a149a93ec
commit
7045813f8b
@ -2,3 +2,5 @@
|
|||||||
|
|
||||||
This folder contains nanoMODBUS examples for Arduino.
|
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.
|
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.
|
||||||
|
|||||||
@ -33,7 +33,7 @@ void onError() {
|
|||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode (LED_BUILTIN, OUTPUT);
|
pinMode (LED_BUILTIN, OUTPUT);
|
||||||
digitalWrite(LED_BUILTIN, HIGH);
|
digitalWrite(LED_BUILTIN, LOW);
|
||||||
|
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
while (!Serial);
|
while (!Serial);
|
||||||
@ -82,9 +82,9 @@ void loop() {
|
|||||||
if (err != NMBS_ERROR_NONE)
|
if (err != NMBS_ERROR_NONE)
|
||||||
onError();
|
onError();
|
||||||
|
|
||||||
// Turn off the led on success
|
// On success, keep the led on
|
||||||
digitalWrite(LED_BUILTIN, LOW);
|
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);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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:
|
This server supports the following function codes:
|
||||||
FC 01 (0x01) Read Coils
|
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() {
|
void onError() {
|
||||||
// Make the LED blink on error
|
// Set the led ON on error
|
||||||
while (true) {
|
while (true) {
|
||||||
digitalWrite(LED_BUILTIN, HIGH);
|
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() {
|
void setup() {
|
||||||
pinMode (LED_BUILTIN, OUTPUT);
|
pinMode (LED_BUILTIN, OUTPUT);
|
||||||
digitalWrite(LED_BUILTIN, HIGH);
|
|
||||||
|
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
while (!Serial);
|
while (!Serial);
|
||||||
@ -112,7 +108,6 @@ void loop() {
|
|||||||
platform_conf.write = write_serial;
|
platform_conf.write = write_serial;
|
||||||
platform_conf.arg = NULL;
|
platform_conf.arg = NULL;
|
||||||
|
|
||||||
// These functions are defined in server.h
|
|
||||||
nmbs_callbacks callbacks = {0};
|
nmbs_callbacks callbacks = {0};
|
||||||
callbacks.read_coils = handle_read_coils;
|
callbacks.read_coils = handle_read_coils;
|
||||||
callbacks.write_multiple_coils = handle_write_multiple_coils;
|
callbacks.write_multiple_coils = handle_write_multiple_coils;
|
||||||
@ -129,16 +124,18 @@ void loop() {
|
|||||||
nmbs_set_read_timeout(&nmbs, 1000);
|
nmbs_set_read_timeout(&nmbs, 1000);
|
||||||
nmbs_set_byte_timeout(&nmbs, 100);
|
nmbs_set_byte_timeout(&nmbs, 100);
|
||||||
|
|
||||||
|
bool led = false;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
err = nmbs_server_poll(&nmbs);
|
err = nmbs_server_poll(&nmbs);
|
||||||
// This will probably never happen, since we don't return < 0 in our platform funcs
|
// This will probably never happen, since we don't return < 0 in our platform funcs
|
||||||
if (err == NMBS_ERROR_TRANSPORT)
|
if (err == NMBS_ERROR_TRANSPORT)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
digitalWrite(LED_BUILTIN, led);
|
||||||
|
led = !led;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Turn off the led at the end
|
// No need to destroy the nmbs instance, terminate the program
|
||||||
digitalWrite(LED_BUILTIN, LOW);
|
|
||||||
|
|
||||||
// No need to destroy the nmbs instance, bye bye
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user