From f350a8cf309b81c5aa2a1eb36465f1184cfe869f Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Tue, 16 Apr 2024 21:03:41 +0200 Subject: [PATCH] 0.5.1 RS485 --- libraries/RS485/CHANGELOG.md | 8 ++++++++ libraries/RS485/README.md | 16 ++++++++++++++++ libraries/RS485/RS485.cpp | 7 ++++++- libraries/RS485/RS485.h | 18 +++++++++++------- .../RS485_SWS_master_send_receive.ino | 2 +- .../RS485_performance/RS485_performance.ino | 1 + .../examples/RS485_sniffer/RS485_sniffer.ino | 2 +- libraries/RS485/library.json | 2 +- libraries/RS485/library.properties | 2 +- 9 files changed, 46 insertions(+), 12 deletions(-) diff --git a/libraries/RS485/CHANGELOG.md b/libraries/RS485/CHANGELOG.md index e02f454d..f93e4dff 100644 --- a/libraries/RS485/CHANGELOG.md +++ b/libraries/RS485/CHANGELOG.md @@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.5.1] - 2024-04-04 +- improve **flush()** to work for STM32 +- add **void setMicrosPerByte(mpb)** to set delay after flush() for last byte. +- add **uint16_t getMicrosPerByte()** +- update GitHub actions +- minor edits + + ## [0.5.0] - 2024-02-01 - merge PR #28, Fix for FS conflict on ESP32 (kudos to DoomHammer) - all ASCII control characters get a **ASCII_** prefix. diff --git a/libraries/RS485/README.md b/libraries/RS485/README.md index 0663af53..3b9f94af 100644 --- a/libraries/RS485/README.md +++ b/libraries/RS485/README.md @@ -107,6 +107,20 @@ An important command from the stream interface is the **setTimeOut()** as this allows reads on the RS485 bus that are limited. +#### setMicrosPerByte + +**Experimental** + +To add a small delay after **flush()** to be sure that the last byte in the +TX register has time to be sent. +It prevents a premature switching from transmit to receive mode. +The default value is 1100 (9600 baud time) and can be tuned, e.g. when high +baud rates are used it can be smaller. + +- **void setMicrosPerByte(uint16_t mpb)** +- **uint16_t getMicrosPerByte()** returns set value, default 1100 (9600 baud time). + + #### Experimental Work in progress. The library has an **experimental** protocol implemented to @@ -223,6 +237,7 @@ Would allow 127 different 1 byte commands. - dynamic receive buffer size? - investigate error handling? - test other platforms + - STM32 (see issue 30) - ESP32. #### Could @@ -232,6 +247,7 @@ Would allow 127 different 1 byte commands. - multi-master? - add unit tests - investigate yield() on ESP32/RTOS behaviour +- investigate non-blocking version. #### Wont diff --git a/libraries/RS485/RS485.cpp b/libraries/RS485/RS485.cpp index 7e10fc88..426fc1cb 100644 --- a/libraries/RS485/RS485.cpp +++ b/libraries/RS485/RS485.cpp @@ -2,7 +2,7 @@ // FILE: RS485.cpp // AUTHOR: Rob Tillaart // DATE: 30-okt-2017 -// VERSION: 0.5.0 +// VERSION: 0.5.1 // PURPOSE: Arduino library for RS485 modules (MAX485) // URL: https://github.com/RobTillaart/RS485 @@ -55,7 +55,12 @@ int RS485::peek() void RS485::flush() { +#ifdef ARDUINO_ARCH_STM32 + while (Serial.availableForWrite() < ( SERIAL_TX_BUFFER_SIZE - 1)); +#else _stream->flush(); +#endif + delayMicroseconds(_microsPerByte); // for the last byte. } diff --git a/libraries/RS485/RS485.h b/libraries/RS485/RS485.h index 1cb60c94..3589f9a9 100644 --- a/libraries/RS485/RS485.h +++ b/libraries/RS485/RS485.h @@ -3,7 +3,7 @@ // FILE: RS485.h // AUTHOR: Rob Tillaart // DATE: 30-okt-2017 -// VERSION: 0.5.0 +// VERSION: 0.5.1 // PURPOSE: Arduino library for RS485 modules // URL: https://github.com/RobTillaart/RS485 @@ -11,7 +11,7 @@ #include "Arduino.h" #include "ASCII_CONTROL.h" -#define RS485_LIB_VERSION (F("0.5.0")) +#define RS485_LIB_VERSION (F("0.5.1")) class RS485 : public Stream @@ -47,19 +47,23 @@ public: // send ASCII encoded messages from one master to multiple clients. // msg[] = 32..127 // len = 1.. 48 (internal receive buffer is 50) - size_t send(uint8_t receiverID, uint8_t msg[], uint8_t len); - bool receive(uint8_t &senderID, uint8_t msg[], uint8_t &len); + size_t send(uint8_t receiverID, uint8_t msg[], uint8_t len); + bool receive(uint8_t &senderID, uint8_t msg[], uint8_t &len); // EXPERIMENTAL - size_t send(uint8_t receiverID, char msg[], uint8_t len); - bool receive(uint8_t &senderID, char msg[], uint8_t &len); + size_t send(uint8_t receiverID, char msg[], uint8_t len); + bool receive(uint8_t &senderID, char msg[], uint8_t &len); + + // EXPERIMENTAL + void setMicrosPerByte(uint16_t mpb) { _microsPerByte = mpb; }; + uint16_t getMicrosPerByte() { return _microsPerByte; }; private: Stream * _stream; uint8_t _sendPin = 0; uint8_t _deviceID = 0; - uint16_t _microsPerByte = 1000; + uint16_t _microsPerByte = 1100; // EXPERIMENTAL uint8_t _bidx = 0; diff --git a/libraries/RS485/examples/RS485_SWS_master_send_receive/RS485_SWS_master_send_receive.ino b/libraries/RS485/examples/RS485_SWS_master_send_receive/RS485_SWS_master_send_receive.ino index 2cb8450a..c4681670 100644 --- a/libraries/RS485/examples/RS485_SWS_master_send_receive/RS485_SWS_master_send_receive.ino +++ b/libraries/RS485/examples/RS485_SWS_master_send_receive/RS485_SWS_master_send_receive.ino @@ -23,7 +23,7 @@ const uint8_t sendPin = 4; const uint8_t deviceID = 0; -SoftwareSerial SWS(6,7); // RX, TX +SoftwareSerial SWS(6, 7); // RX, TX // use Software Serial diff --git a/libraries/RS485/examples/RS485_performance/RS485_performance.ino b/libraries/RS485/examples/RS485_performance/RS485_performance.ino index 4ab62a9a..cec451e8 100644 --- a/libraries/RS485/examples/RS485_performance/RS485_performance.ino +++ b/libraries/RS485/examples/RS485_performance/RS485_performance.ino @@ -6,6 +6,7 @@ // minimal test to see how fast data can be send over the // RS485 bus at a given baud rate of 4800. (adjust if needed). + #include "Arduino.h" #include "RS485.h" diff --git a/libraries/RS485/examples/RS485_sniffer/RS485_sniffer.ino b/libraries/RS485/examples/RS485_sniffer/RS485_sniffer.ino index 32dd6d2f..8370eca0 100644 --- a/libraries/RS485/examples/RS485_sniffer/RS485_sniffer.ino +++ b/libraries/RS485/examples/RS485_sniffer/RS485_sniffer.ino @@ -73,7 +73,7 @@ void loop() int x = rs485.read(); - // HEX DUMP PART + // HEX DUMP PART if (x < 0x10) Serial.print('0'); Serial.print(x, HEX); Serial.print(" "); diff --git a/libraries/RS485/library.json b/libraries/RS485/library.json index b97ac9d9..dcac1e2d 100644 --- a/libraries/RS485/library.json +++ b/libraries/RS485/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/RS485" }, - "version": "0.5.0", + "version": "0.5.1", "license": "MIT", "frameworks": "*", "platforms": "*", diff --git a/libraries/RS485/library.properties b/libraries/RS485/library.properties index 19b4814f..27fb3736 100644 --- a/libraries/RS485/library.properties +++ b/libraries/RS485/library.properties @@ -1,5 +1,5 @@ name=RS485 -version=0.5.0 +version=0.5.1 author=Rob Tillaart maintainer=Rob Tillaart sentence=RS485 library for Arduino.