Skip to content

Commit

Permalink
Fix #30, flush() for STM32 (#31)
Browse files Browse the repository at this point in the history
- Fix #30, 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
  • Loading branch information
RobTillaart authored Apr 16, 2024
1 parent 0cd2f5e commit 3e5589f
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 12 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
7 changes: 6 additions & 1 deletion RS485.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
}


Expand Down
18 changes: 11 additions & 7 deletions RS485.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// 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


#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
Expand Down Expand Up @@ -47,19 +47,23 @@ class RS485 : public Stream
// 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions examples/RS485_performance/RS485_performance.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion examples/RS485_sniffer/RS485_sniffer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ");
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/RS485"
},
"version": "0.5.0",
"version": "0.5.1",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=RS485
version=0.5.0
version=0.5.1
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=RS485 library for Arduino.
Expand Down

0 comments on commit 3e5589f

Please sign in to comment.