From 78088e78539bdd5b4948f432f4747e7e1b7b75c8 Mon Sep 17 00:00:00 2001 From: Aditya Naik Date: Thu, 13 Feb 2020 15:06:29 -0500 Subject: Added Arduino examples, untested --- examples/arduino/arduino.ino | 64 ++++++++++++++++++++++++++++++++++++++++++++ examples/stm32/main.c | 4 ++- 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 examples/arduino/arduino.ino (limited to 'examples') diff --git a/examples/arduino/arduino.ino b/examples/arduino/arduino.ino new file mode 100644 index 0000000..d6d2e1a --- /dev/null +++ b/examples/arduino/arduino.ino @@ -0,0 +1,64 @@ + +#include + +/* Baud rate for UART */ +#define BAUD 9600 + +/* To match the parameters of HardwareSerial Rx/Tx functions with the Modbus functions, wrappers around the ::read() and ::write() functions are required */ +MB_StatusTypeDef ArduinoSerial_Tx(void* serial, uint16_t* buf, uint8_t* len, uint8_t* timeout); +MB_StatusTypeDef ArduinoSerial_Rx(void* serial, uint16_t* buf, uint8_t* len, uint8_t* timeout); + +MB_StatusTypeDef (*MB_UART_Tx)(void*, uint16_t*, uint8_t*, uint8_t*) = &ArduinoSerial_Tx; +MB_StatusTypeDef (*MB_UART_Rx)(void*, uint16_t*, uint8_t*, uint8_t*) = &ArduinoSerial_Rx; + +/* Single modbus device node */ +modbus_slave_device modbus_dev_single; +/* 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]; + +uint16_t reg; + +void setup() +{ + Serial.begin(BAUD); + + /* Set the slave ID to 1 */ + modbus_dev_single.slave_id = 1; + /* Attach the default HardwareSerial class instance to this node */ + modbus_dev_single.modbus_uart = &Serial; + + /* Continuously read the this input register in superloop */ + reg = 30001; +} + +void loop() +{ + MB_StatusTypeDef status = ReadInputRegisters(&modbus_dev_single, reg, 1); + if (status == MB_OK) { + /* Do something with data in the 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(serial); + if (_serial->availableForWrite()) { + _serial->write((uint8_t)*buf); + } + _serial->flush(); + /* Arduino HardwareSerial class does not return any errors, sadly */ + return MB_OK; +} + +MB_StatusTypeDef ArduinoSerial_Rx(void* serial, uint16_t* buf, uint8_t* len, uint8_t* timeout) +{ + HardwareSerial* _serial = static_cast(serial); + buf = _serial->read(); + + /* Arduino HardwareSerial class does not return any errors, sadly */ + return MB_OK; +} + diff --git a/examples/stm32/main.c b/examples/stm32/main.c index 7f63529..9e71ed9 100644 --- a/examples/stm32/main.c +++ b/examples/stm32/main.c @@ -1,4 +1,6 @@ -#include "modbus.h" +// TODO Makefile + +#include /* 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. */ /* If you are not using CubeMX to initialize the project, you are on your own */ -- cgit v1.2.3