diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/arduino/arduino.ino | 25 | ||||
| -rw-r--r-- | examples/stm32/main.c | 24 |
2 files changed, 31 insertions, 18 deletions
diff --git a/examples/arduino/arduino.ino b/examples/arduino/arduino.ino index 2f4c3f7..88cbcaa 100644 --- a/examples/arduino/arduino.ino +++ b/examples/arduino/arduino.ino @@ -1,5 +1,6 @@ #include <modbus.h> +// #include <SoftwareSerial.h> /* Baud rate for UART */ #define BAUD 9600 @@ -25,15 +26,16 @@ modbus_slave_device modbus_dev_single; * in a single point-to-point connection? */ /* Not illustrated beyond the definition, but should be clear enough */ modbus_slave_device modbus_dev_multiple[10]; +// SoftwareSerial myserial(5, 6); uint16_t reg; void setup() { Serial.begin(BAUD); - + // myserial.begin(BAUD); /* Set the slave ID to 1 */ - modbus_dev_single.slave_id = 1; + modbus_dev_single.slave_id = 3; /* Attach the default HardwareSerial class instance to this node */ modbus_dev_single.modbus_uart = &Serial; @@ -47,27 +49,34 @@ void loop() if (status == MB_OK) { /* Do something with data in the response buffer */ /* ... */ + // Serial.write((uint8_t)modbus_dev_single.response_buffer); ClearResponseBuffer(&modbus_dev_single); } } MB_StatusTypeDef ArduinoSerial_Tx(void* serial, uint16_t* buf, uint8_t* len, uint8_t* timeout) { - HardwareSerial* _serial = static_cast<HardwareSerial*>(serial); - if (_serial->availableForWrite()) { - _serial->write((uint8_t)*buf); - } - _serial->flush(); + // SoftwareSerial *_serial = static_cast<SoftwareSerial*>(serial); + Stream *_serial = static_cast<Stream*>(serial); + // HardwareSerial* _serial = static_cast<HardwareSerial*>(serial); + _serial->write((uint8_t)*buf); + // if (_serial->availableForWrite()) { + // _serial->write((uint8_t)*buf); + // } + // _serial->flush(); /* HardwareSerial class funcs do not return any UART errors, sadly */ return MB_OK; } MB_StatusTypeDef ArduinoSerial_Rx(void* serial, uint16_t* buf, uint8_t* len, uint8_t* timeout) { - HardwareSerial* _serial = static_cast<HardwareSerial*>(serial); + // HardwareSerial* _serial = static_cast<HardwareSerial*>(serial); + // SoftwareSerial *_serial = static_cast<SoftwareSerial*>(serial); + Stream *_serial = static_cast<Stream*>(serial); buf = _serial->read(); /* HardwareSerial class funcs do not return any UART errors, sadly */ return MB_OK; } + diff --git a/examples/stm32/main.c b/examples/stm32/main.c index 958e4d7..57ba4e8 100644 --- a/examples/stm32/main.c +++ b/examples/stm32/main.c @@ -1,6 +1,6 @@ #include <modbus.h> -/* +/** * Note that this example is not self-contained. User will need to handle UART * device initialization and relevant HAL/LL library includes. These steps are * skipped in this example to ensure simplicity - illustrated here is the @@ -9,7 +9,7 @@ * */ -/* +/** * If using CubeMX, the HAL library files will be automatically placed in the * correct location. The examples were tested with the Makefile option in * CubeMX. @@ -23,9 +23,11 @@ */ -/* Function initialization for the STM32 HAL Library */ -/* - * Since this library was orignally written to work with the STM32 HAL/LL API, +/** + * + * Function initialization for the STM32 HAL Library + * + * \brief Since this library was orignally written to work with the STM32 HAL/LL API, * the UART functions match perfectly and can be directly mapped to the Modbus * library functions. */ @@ -36,18 +38,20 @@ MB_StatusTypeDef (*MB_UART_Rx)(void*, uint16_t*, uint8_t*, uint8_t*) = int main() { - /* Single modbus device node */ - modbus_slave_device modbus_dev_single; + + modbus_slave_device modbus_dev_single; /*< Single modbus device node */ + /* Multiple modbus device nodes.. after all, what is the point of using * Modbus in a single point-to-point connection? */ /* Not illustrated beyond the definition, but should be clear enough */ modbus_slave_device modbus_dev_multiple[10]; - - /* Set the slave ID to 1 */ - modbus_dev_single.slave_id = 1; + + modbus_dev_single.slave_id = 1; /*< Set the slave ID to 1 */ + /* Attach the UART device on the STM32 to this node */ /* Note that initialization of UART1 is not included in this example */ modbus_dev_single.modbus_uart = &huart1; + /* Continuously read the 30001 input register */ uint16_t register = 30001; |
