From 0d213da2e77961fa771aa2e266b48cc824bffa0f Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Wed, 25 May 2022 12:32:10 +0200 Subject: [PATCH] 0.2.1 RS485 --- libraries/RS485/README.md | 6 +++++- libraries/RS485/RS485.cpp | 13 +++++++------ libraries/RS485/RS485.h | 23 ++++++++++++++--------- libraries/RS485/library.json | 2 +- libraries/RS485/library.properties | 2 +- libraries/RS485/test/unit_test_001.cpp | 19 ++++++++++++++++++- 6 files changed, 46 insertions(+), 19 deletions(-) diff --git a/libraries/RS485/README.md b/libraries/RS485/README.md index f2d6bb9b..7d13d32b 100644 --- a/libraries/RS485/README.md +++ b/libraries/RS485/README.md @@ -37,6 +37,11 @@ The library sets the pinMode and defaults it to LOW (receiving mode). - **void setMicrosPerByte(uint32_t baudRate)** set the delay per character needed. This gives the hardware enough time to flush the buffer. - **uint32_t getMicrosPerByte()** returns the current delay in micros used. +- **void setTXmode()** explicitly set mode to transmitting / sending. +This implies that the device will stop listening on the RS485 bus. +- **void setRXmode()** explicitly set mode to receiving / listening. +This is the default behaviour of every RS485 device. +- **uint8_t getMode()** returns the current mode, 1 == TX, 0 == RX. #### Stream interface @@ -70,7 +75,6 @@ resumes with listening. - dynamic buffer size? - should this be a sort of message class / struct. fixed size? - add **yield()** for large messages that are blocking. -- **stopListening()** and **startListening()** (with sendPin ?) - prevent blocking if possible. - add examples - add unit tests diff --git a/libraries/RS485/RS485.cpp b/libraries/RS485/RS485.cpp index 4969c362..78320b2e 100644 --- a/libraries/RS485/RS485.cpp +++ b/libraries/RS485/RS485.cpp @@ -2,13 +2,14 @@ // FILE: RS485.cpp // AUTHOR: Rob Tillaart // DATE: 30-okt-2017 -// VERSION: 0.2.0 +// VERSION: 0.2.1 // PURPOSE: Arduino library for RS485 modules (MAX485) // URL: https://github.com/RobTillaart/RS485 // // HISTORY: // 0.1.x 2017-10-30 experimental versions. // 0.2.0 2022-05-24 first published version +// 0.2.1 2022-05-24 add setTXmode(), setRXmode(), getMode(). #include "RS485.h" @@ -24,7 +25,7 @@ RS485::RS485(Stream * stream, uint8_t sendPin, uint8_t deviceID) _deviceID = deviceID; pinMode(_sendPin, OUTPUT); - digitalWrite(_sendPin, LOW); // receiver mode + setRXmode(); // receiver mode } @@ -50,20 +51,20 @@ void RS485::flush() size_t RS485::write(uint8_t c) { - digitalWrite(_sendPin, HIGH); // transmit mode + setTXmode(); // transmit mode size_t n = _stream->write(c); delayMicroseconds(_microsPerByte); - digitalWrite(_sendPin, LOW); // receiver mode + setRXmode(); // receiver mode return n; } // TODO: fix blocking - yield() - merge above size_t RS485::write(uint8_t * array, uint8_t length) { - digitalWrite(_sendPin, HIGH); // transmit mode + setTXmode(); // transmit mode size_t n = _stream->write(array, length); delayMicroseconds(_microsPerByte); - digitalWrite(_sendPin, LOW); // receiver mode + setRXmode(); // receiver mode return n; } diff --git a/libraries/RS485/RS485.h b/libraries/RS485/RS485.h index 2b39667b..edbd1f2f 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.2.0 +// VERSION: 0.2.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 "0.2.0" +#define RS485_LIB_VERSION (F("0.2.1")) class RS485 : public Stream @@ -28,13 +28,18 @@ public: uint8_t getDeviceID() { return _deviceID; }; - // Stream interface - int available(); - int read(); - int peek(); - void flush(); - size_t write(uint8_t c); - size_t write(uint8_t * array, uint8_t length); + // Stream interface + int available(); + int read(); + int peek(); + void flush(); + size_t write(uint8_t c); + size_t write(uint8_t * array, uint8_t length); + + // set the communication direction + inline void setTXmode() { digitalWrite(_sendPin, HIGH); }; + inline void setRXmode() { digitalWrite(_sendPin, LOW); }; + uint8_t getMode() { return digitalRead(_sendPin) == HIGH; }; // TODO TEST 0.3.0 diff --git a/libraries/RS485/library.json b/libraries/RS485/library.json index b267807b..6f1da81c 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.2.0", + "version": "0.2.1", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/libraries/RS485/library.properties b/libraries/RS485/library.properties index ae7e4b86..5cefda10 100644 --- a/libraries/RS485/library.properties +++ b/libraries/RS485/library.properties @@ -1,5 +1,5 @@ name=RS485 -version=0.2.0 +version=0.2.1 author=Rob Tillaart maintainer=Rob Tillaart sentence=RS485 library for Arduino. diff --git a/libraries/RS485/test/unit_test_001.cpp b/libraries/RS485/test/unit_test_001.cpp index 893461e1..f5245b5f 100644 --- a/libraries/RS485/test/unit_test_001.cpp +++ b/libraries/RS485/test/unit_test_001.cpp @@ -74,12 +74,29 @@ unittest(test_microsPerByte) assertEqual(1000, master.getMicrosPerByte()); master.setMicrosPerByte(115200); - // count 11 bits / byte + // count 11 bits / byte uint32_t us = (11 * 1000000UL) / 115200; assertEqual(us, master.getMicrosPerByte()); } +unittest(test_communication_mode) +{ + Serial.begin(115200); + + const uint8_t sendPin = 4; + + RS485 master(&Serial, sendPin); + + // default value + assertEqual(0, master.getMode()); + master.setTXmode(); + assertEqual(1, master.getMode()); + master.setRXmode(); + assertEqual(0, master.getMode()); +} + + unittest_main() // --------