mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.2.1 RS485
This commit is contained in:
parent
74408c8c4d
commit
0d213da2e7
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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": "*",
|
||||
|
@ -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.
|
||||
|
@ -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()
|
||||
|
||||
// --------
|
||||
|
Loading…
Reference in New Issue
Block a user