0.2.1 RS485

This commit is contained in:
rob tillaart 2022-05-25 12:32:10 +02:00
parent 74408c8c4d
commit 0d213da2e7
6 changed files with 46 additions and 19 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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
@ -36,6 +36,11 @@ public:
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
// void send(uint8_t deviceID, uint8_t msg[], uint8_t len);

View File

@ -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": "*",

View File

@ -1,5 +1,5 @@
name=RS485
version=0.2.0
version=0.2.1
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=RS485 library for Arduino.

View File

@ -80,6 +80,23 @@ unittest(test_microsPerByte)
}
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()
// --------