diff --git a/README.adoc b/README.adoc index b1be50e..ee19f70 100644 --- a/README.adoc +++ b/README.adoc @@ -8,6 +8,7 @@ image:https://github.com/{repository-owner}/{repository-name}/actions/workflows/ image:https://github.com/{repository-owner}/{repository-name}/actions/workflows/spell-check.yml/badge.svg["Spell Check status", link="https://github.com/{repository-owner}/{repository-name}/actions/workflows/spell-check.yml"] Enables sending and receiving data using the RS-485 standard with RS-485 shields, like the MKR 485 Shield. +I've Made this compatible with ESP8266 and ESP32. This library supports the Maxim Integrated MAX3157 and equivalent chipsets. diff --git a/library.properties b/library.properties index ffd6dfe..673aec3 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ArduinoRS485 -version=1.0.2 +version=1.0.61 author=Arduino maintainer=Arduino sentence=Enables sending and receiving data using the RS-485 standard with RS-485 shields, like the MKR 485 Shield. diff --git a/src/RS485.cpp b/src/RS485.cpp index ba6e80f..eb2ec29 100644 --- a/src/RS485.cpp +++ b/src/RS485.cpp @@ -38,12 +38,12 @@ void RS485Class::begin(unsigned long baudrate, int predelay, int postdelay) begin(baudrate, SERIAL_8N1, predelay, postdelay); } -void RS485Class::begin(unsigned long baudrate, uint16_t config) +void RS485Class::begin(unsigned long baudrate, RS485_SER_CONF_TYPE config) { begin(baudrate, config, RS485_DEFAULT_PRE_DELAY, RS485_DEFAULT_POST_DELAY); } -void RS485Class::begin(unsigned long baudrate, uint16_t config, int predelay, int postdelay) +void RS485Class::begin(unsigned long baudrate, RS485_SER_CONF_TYPE config, int predelay, int postdelay) { _baudrate = baudrate; _config = config; @@ -137,6 +137,11 @@ void RS485Class::endTransmission() } _transmisionBegun = false; +#ifdef ESP32 + // there is a bug in ESP32 for Serial2 + while(_serial->available()) + _serial->read(); +#endif } void RS485Class::receive() diff --git a/src/RS485.h b/src/RS485.h index 050b253..aa760d7 100644 --- a/src/RS485.h +++ b/src/RS485.h @@ -21,33 +21,17 @@ #define _RS485_H_INCLUDED #include +#include "RS485_defs.h" -#ifdef PIN_SERIAL1_TX -#define RS485_DEFAULT_TX_PIN PIN_SERIAL1_TX -#else -#define RS485_DEFAULT_TX_PIN 1 -#endif - -#ifdef __AVR__ -#define RS485_DEFAULT_DE_PIN 2 -#define RS485_DEFAULT_RE_PIN -1 -#else -#define RS485_DEFAULT_DE_PIN A6 -#define RS485_DEFAULT_RE_PIN A5 -#endif - - -#define RS485_DEFAULT_PRE_DELAY 50 -#define RS485_DEFAULT_POST_DELAY 50 class RS485Class : public Stream { public: RS485Class(HardwareSerial& hwSerial, int txPin, int dePin, int rePin); virtual void begin(unsigned long baudrate); - virtual void begin(unsigned long baudrate, uint16_t config); + virtual void begin(unsigned long baudrate, RS485_SER_CONF_TYPE config); virtual void begin(unsigned long baudrate, int predelay, int postdelay); - virtual void begin(unsigned long baudrate, uint16_t config, int predelay, int postdelay); + virtual void begin(unsigned long baudrate, RS485_SER_CONF_TYPE config, int predelay, int postdelay); virtual void end(); virtual int available(); virtual int peek(); @@ -79,7 +63,7 @@ class RS485Class : public Stream { bool _transmisionBegun; unsigned long _baudrate; - uint16_t _config; + RS485_SER_CONF_TYPE _config; }; extern RS485Class RS485; diff --git a/src/RS485_defs.h b/src/RS485_defs.h new file mode 100644 index 0000000..6d585b6 --- /dev/null +++ b/src/RS485_defs.h @@ -0,0 +1,38 @@ +#ifndef _ARDUINO_RS485_DEFS_H_INCLUDED +#define _ARDUINO_RS485_DEFS_H_INCLUDED + +#ifndef RS485_DEFAULT_TX_PIN +#ifdef PIN_SERIAL1_TX +#define RS485_DEFAULT_TX_PIN PIN_SERIAL1_TX +#else +#define RS485_DEFAULT_TX_PIN 1 +#endif +#endif + +#ifdef __AVR__ +#define RS485_DEFAULT_DE_PIN 2 +#define RS485_DEFAULT_RE_PIN -1 +#elif defined(ESP32) || defined(ESP8266) +#ifndef RS485_DEFAULT_DE_PIN +#define RS485_DEFAULT_DE_PIN 0 +#endif +#ifndef RS485_DEFAULT_RE_PIN +#define RS485_DEFAULT_RE_PIN 0 +#endif +#else +#define RS485_DEFAULT_DE_PIN A6 +#define RS485_DEFAULT_RE_PIN A5 +#endif + +#if defined(ESP8266) +#define RS485_SER_CONF_TYPE SerialConfig +#elif defined(ESP32) +#define RS485_SER_CONF_TYPE uint32_t +#else +#define RS485_SER_CONF_TYPE uint16_t +#endif + +#define RS485_DEFAULT_PRE_DELAY 50 +#define RS485_DEFAULT_POST_DELAY 50 + +#endif