Skip to content

Commit 485bac9

Browse files
committed
Added Serial as RS485 interface demo
1 parent e97a519 commit 485bac9

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
This Sketch demonstrates how to use the Hardware Serial peripheral to communicate over an RS485 bus.
3+
4+
Data received on the primary serial port is relayed to the bus acting as an RS485 interface and vice versa.
5+
6+
UART to RS485 translation hardware (e.g., MAX485, MAX33046E, ADM483) is assumed to be configured in half-duplex
7+
mode with collision detection as described in
8+
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/uart.html#circuit-a-collision-detection-circuit
9+
10+
*/
11+
12+
// Open the Serial Monitor with testing baud start typing and sending caracters
13+
14+
#include <HardwareSerial.h>
15+
16+
#define RS485_RX_PIN 16
17+
#define RS485_TX_PIN 5
18+
#define RS485_RTS_PIN 37
19+
20+
HardwareSerial RS485(2);
21+
22+
void setup() {
23+
Serial.begin(9600);
24+
delay(2000);
25+
26+
RS485.begin(9600, SERIAL_8N1, RS485_RX_PIN, RS485_TX_PIN);
27+
if(!RS485.setPins(-1, -1, -1, RS485_RTS_PIN)){
28+
Serial.print("Failed to set RS485 pins");
29+
}
30+
31+
if(!RS485.setMode(MODE_RS485_HALF_DUPLEX)) {
32+
Serial.print("Failed to set RS485 mode");
33+
}
34+
}
35+
36+
void loop() {
37+
if (RS485.available()) {
38+
Serial.write(RS485.read());
39+
}
40+
if (Serial.available()) {
41+
RS485.write(Serial.read());
42+
}
43+
}

0 commit comments

Comments
 (0)