From 485bac9aa7d444abbe3bd80c24438bd21657918e Mon Sep 17 00:00:00 2001 From: Neale Petrillo Date: Wed, 29 Nov 2023 12:19:13 -0500 Subject: [PATCH 1/8] Added Serial as RS485 interface demo --- .../RS485_Echo_Demo/RS485_Echo_Demo.ino | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino diff --git a/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino new file mode 100644 index 00000000000..07347474b08 --- /dev/null +++ b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino @@ -0,0 +1,43 @@ +/* + This Sketch demonstrates how to use the Hardware Serial peripheral to communicate over an RS485 bus. + + Data received on the primary serial port is relayed to the bus acting as an RS485 interface and vice versa. + + UART to RS485 translation hardware (e.g., MAX485, MAX33046E, ADM483) is assumed to be configured in half-duplex + mode with collision detection as described in + https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/uart.html#circuit-a-collision-detection-circuit + +*/ + +// Open the Serial Monitor with testing baud start typing and sending caracters + +#include + +#define RS485_RX_PIN 16 +#define RS485_TX_PIN 5 +#define RS485_RTS_PIN 37 + +HardwareSerial RS485(2); + +void setup() { + Serial.begin(9600); + delay(2000); + + RS485.begin(9600, SERIAL_8N1, RS485_RX_PIN, RS485_TX_PIN); + if(!RS485.setPins(-1, -1, -1, RS485_RTS_PIN)){ + Serial.print("Failed to set RS485 pins"); + } + + if(!RS485.setMode(MODE_RS485_HALF_DUPLEX)) { + Serial.print("Failed to set RS485 mode"); + } +} + +void loop() { + if (RS485.available()) { + Serial.write(RS485.read()); + } + if (Serial.available()) { + RS485.write(Serial.read()); + } +} From 6022ef62bc34c30bb37688e6a0fe4d0c40df569c Mon Sep 17 00:00:00 2001 From: Neale Petrillo Date: Wed, 29 Nov 2023 12:32:11 -0500 Subject: [PATCH 2/8] Added more detail to initial comment --- .../examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino index 07347474b08..d4d625a4478 100644 --- a/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino +++ b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino @@ -7,10 +7,11 @@ mode with collision detection as described in https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/uart.html#circuit-a-collision-detection-circuit + To use the script open the Arduino serial monitor (or alternative serial monitor on the Arduino port). Then, + using an RS485 tranciver, connect another serial monitor to the RS485 port. Entering data on one terminal + should be displayed on the other terminal. */ -// Open the Serial Monitor with testing baud start typing and sending caracters - #include #define RS485_RX_PIN 16 From ad8589ff421b2b0d8220a6ec03eda40d5bd6df5e Mon Sep 17 00:00:00 2001 From: Neale Petrillo Date: Wed, 29 Nov 2023 14:06:17 -0500 Subject: [PATCH 3/8] Switched to UART_MODE definitions from uart_types.h in order to accomodate some versions of Arduino core --- .../examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino index d4d625a4478..95d4fb20229 100644 --- a/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino +++ b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino @@ -13,6 +13,7 @@ */ #include +#include "hal/uart_types.h" #define RS485_RX_PIN 16 #define RS485_TX_PIN 5 @@ -29,7 +30,11 @@ void setup() { Serial.print("Failed to set RS485 pins"); } - if(!RS485.setMode(MODE_RS485_HALF_DUPLEX)) { + // Certain versions of Arduino core don't define MODE_RS485_HALF_DUPLEX and so fail to compile. + // By using UART_MODE_RS485_HALF_DUPLEX defined in hal/uart_types.h we work around this problem. + // If using a newer IDF and Arduino core you can ommit including hal/uart_types.h and use MODE_RS485_HALF_DUPLEX + // defined in esp32-hal-uart.h (included during other build steps) instead. + if(!RS485.setMode(UART_MODE_RS485_HALF_DUPLEX)) { Serial.print("Failed to set RS485 mode"); } } From 037108085700322ec1ea97e8c1adef4ef16fd80e Mon Sep 17 00:00:00 2001 From: Neale Petrillo <82894027+econeale@users.noreply.github.com> Date: Fri, 1 Dec 2023 09:05:43 -0500 Subject: [PATCH 4/8] Update libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino Co-authored-by: Lucas Saavedra Vaz --- .../ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino index 95d4fb20229..bfb5170e76a 100644 --- a/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino +++ b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino @@ -19,7 +19,7 @@ #define RS485_TX_PIN 5 #define RS485_RTS_PIN 37 -HardwareSerial RS485(2); +#define RS485 Serial1 void setup() { Serial.begin(9600); From 57061e659b4bdc0331933ce89eb51413261fe43a Mon Sep 17 00:00:00 2001 From: Neale Petrillo <82894027+econeale@users.noreply.github.com> Date: Fri, 1 Dec 2023 09:06:08 -0500 Subject: [PATCH 5/8] Update libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino Co-authored-by: Lucas Saavedra Vaz --- .../ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino index bfb5170e76a..80c9e9480ab 100644 --- a/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino +++ b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino @@ -23,7 +23,7 @@ void setup() { Serial.begin(9600); - delay(2000); + while(!Serial) { delay(10); } RS485.begin(9600, SERIAL_8N1, RS485_RX_PIN, RS485_TX_PIN); if(!RS485.setPins(-1, -1, -1, RS485_RTS_PIN)){ From 28a1ef618cb83de7a7cebec7a0d0e3517279999e Mon Sep 17 00:00:00 2001 From: Neale Petrillo <82894027+econeale@users.noreply.github.com> Date: Fri, 1 Dec 2023 09:06:13 -0500 Subject: [PATCH 6/8] Update libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino Co-authored-by: Lucas Saavedra Vaz --- .../ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino index 80c9e9480ab..a1c98800b91 100644 --- a/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino +++ b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino @@ -26,6 +26,7 @@ void setup() { while(!Serial) { delay(10); } RS485.begin(9600, SERIAL_8N1, RS485_RX_PIN, RS485_TX_PIN); + while(!RS485) { delay(10); } if(!RS485.setPins(-1, -1, -1, RS485_RTS_PIN)){ Serial.print("Failed to set RS485 pins"); } From fe1c3664564b233c640c35ae5c349a2823254204 Mon Sep 17 00:00:00 2001 From: Neale Petrillo Date: Tue, 12 Dec 2023 08:40:16 -0500 Subject: [PATCH 7/8] Removed HardwareSerial.h include. Switched RTS pin to GPIO 4 for broader compatability. --- .../ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino index a1c98800b91..67e1416a2e4 100644 --- a/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino +++ b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino @@ -11,13 +11,11 @@ using an RS485 tranciver, connect another serial monitor to the RS485 port. Entering data on one terminal should be displayed on the other terminal. */ - -#include #include "hal/uart_types.h" #define RS485_RX_PIN 16 #define RS485_TX_PIN 5 -#define RS485_RTS_PIN 37 +#define RS485_RTS_PIN 4 #define RS485 Serial1 From 327fbbc04d03bb7803d514b6a7ff089c3972351c Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 20 Dec 2023 11:05:12 -0300 Subject: [PATCH 8/8] using 115200 for UART0 - console --- .../ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino index 67e1416a2e4..a19be7ffe25 100644 --- a/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino +++ b/libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino @@ -20,7 +20,7 @@ #define RS485 Serial1 void setup() { - Serial.begin(9600); + Serial.begin(115200); while(!Serial) { delay(10); } RS485.begin(9600, SERIAL_8N1, RS485_RX_PIN, RS485_TX_PIN);