-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Added example to demonstrate using HardwareSerial with RS485 interfaces #8941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
485bac9
Added Serial as RS485 interface demo
econeale 6022ef6
Added more detail to initial comment
econeale ad8589f
Switched to UART_MODE definitions from uart_types.h in order to accom…
econeale 0371080
Update libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Dem…
econeale 57061e6
Update libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Dem…
econeale 28a1ef6
Update libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Dem…
econeale fe1c366
Removed HardwareSerial.h include. Switched RTS pin to GPIO 4 for broa…
econeale d1e7c92
Merge branch 'master' into master
econeale 4473267
Merge branch 'master' into master
SuGlider 327fbbc
using 115200 for UART0 - console
SuGlider File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
libraries/ESP32/examples/Serial/RS485_Echo_Demo/RS485_Echo_Demo.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
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 | ||
|
||
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. | ||
*/ | ||
|
||
#include <HardwareSerial.h> | ||
#include "hal/uart_types.h" | ||
|
||
#define RS485_RX_PIN 16 | ||
#define RS485_TX_PIN 5 | ||
#define RS485_RTS_PIN 37 | ||
econeale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
HardwareSerial RS485(2); | ||
econeale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
void setup() { | ||
Serial.begin(9600); | ||
delay(2000); | ||
econeale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
RS485.begin(9600, SERIAL_8N1, RS485_RX_PIN, RS485_TX_PIN); | ||
if(!RS485.setPins(-1, -1, -1, RS485_RTS_PIN)){ | ||
econeale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Serial.print("Failed to set RS485 pins"); | ||
} | ||
|
||
// 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"); | ||
} | ||
} | ||
|
||
void loop() { | ||
if (RS485.available()) { | ||
Serial.write(RS485.read()); | ||
} | ||
if (Serial.available()) { | ||
RS485.write(Serial.read()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.