2018-04-11 02:56:00 -04:00
# UART RS485 Echo Example
2018-11-01 06:57:24 -04:00
(See the README.md file in the upper level 'examples' directory for more information about examples.)
This is an example which echoes any data it receives on UART2 back to the sender in the RS485 network.
2018-07-02 09:21:02 -04:00
It uses ESP-IDF UART software driver in RS485 half duplex transmission mode and requires external connection of bus drivers.
The approach demonstrated in this example can be used in user application to transmit/receive data in RS485 networks.
2018-04-11 02:56:00 -04:00
2018-11-01 06:57:24 -04:00
## How to use example
### Hardware Required
2021-06-07 22:47:49 -04:00
PC + USB Serial adapter connected to USB port + RS485 line drivers + ESP32, ESP32-S or ESP32-C series based board.
2018-07-02 09:21:02 -04:00
The MAX485 line driver is used for example below but other similar chips can be used as well.
2018-04-11 02:56:00 -04:00
2018-11-01 06:57:24 -04:00
#### RS485 example connection circuit schematic:
2018-07-02 09:21:02 -04:00
```
2018-04-11 02:56:00 -04:00
VCC ---------------+ +--------------- VCC
| |
+-------x-------+ +-------x-------+
RXD < ------ | RO | | RO | ----- > RXD
| B|---------------|B |
TXD ------>| DI MAX485 | \ / | MAX485 DI|< ----- TXD
2021-06-07 22:47:49 -04:00
ESP dev kit | | RS-485 side | | SERIAL ADAPTER SIDE
2018-04-11 02:56:00 -04:00
RTS --+--->| DE | / \ | DE|---+
| | A|---------------|A | |
+----| /RE | | /RE|---+-- RTS
+-------x-------+ +-------x-------+
| |
2018-11-01 06:57:24 -04:00
--- ---
```
2018-04-11 02:56:00 -04:00
2021-06-07 22:47:49 -04:00
#### Connect an external RS485 serial interface to an ESP board
Connect USB to RS485 adapter to computer and connect its D+, D- output lines with the D+, D- lines of RS485 line driver connected to the ESP board (See picture above). To view or adjust default pins please see the `Echo RS485 Example Configuration` submenu in `idf.py menuconfig` .
2018-07-02 09:21:02 -04:00
```
2021-06-07 22:47:49 -04:00
--------------------------------------------------------------------------------------------------
| ESP Interface | #define | Default ESP Pin | External RS485 Driver Pin |
| ----------------------|--------------------|-----------------------|---------------------------|
| Transmit Data (TxD) | CONFIG_MB_UART_TXD | CONFIG_ECHO_UART_TXD | DI |
| Receive Data (RxD) | CONFIG_MB_UART_RXD | CONFIG_ECHO_UART_RXD | RO |
| Request To Send (RTS) | CONFIG_MB_UART_RTS | CONFIG_ECHO_UART_RTS | ~RE/DE |
| Ground | n/a | GND | GND |
--------------------------------------------------------------------------------------------------
2018-07-02 09:21:02 -04:00
```
2021-06-07 22:47:49 -04:00
Note: Some GPIOs can not be used with some chip because they are used for flash chip connection. Please refer to UART documentation for selected target.
2018-07-02 09:21:02 -04:00
2018-11-01 06:57:24 -04:00
### Configure the project
2018-07-02 09:21:02 -04:00
```
2019-08-01 23:31:20 -04:00
idf.py menuconfig
2018-07-02 09:21:02 -04:00
```
2018-11-01 06:57:24 -04:00
### Build and Flash
2018-07-02 09:21:02 -04:00
Build the project and flash it to the board, then run monitor tool to view serial output:
```
2019-08-01 23:31:20 -04:00
idf.py -p PORT flash monitor
2018-07-02 09:21:02 -04:00
```
(To exit the serial monitor, type ``Ctrl-]``.)
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
2018-11-01 06:57:24 -04:00
#### Setup external terminal software
Refer to the example and set up a serial terminal program to the same settings as of UART in ESP32-WROVER-KIT board.
Open the external serial interface in the terminal. By default if no any symbols are received, the application sends character `.` to check transmission side.
When typing message and push send button in the terminal you should see the message `RS485 Received: [ your message ]` , where "your message" is the message you sent from terminal.
2021-06-07 22:47:49 -04:00
Verify if echo indeed comes from your board by disconnecting either `TxD` or `RxD` pin. Once done there should be no any `.` displayed.
2018-07-02 09:21:02 -04:00
## Example Output
Example output of the application:
```
I (655020) RS485_ECHO_APP: Received 12 bytes:
[ 0x79 0x6F 0x75 0x72 0x20 0x6D 0x65 0x73 0x73 0x61 0x67 0x65 ]
```
2018-11-01 06:57:24 -04:00
The received message is showed in hexadecimal form in the brackets.
2018-07-02 09:21:02 -04:00
## Troubleshooting
2018-11-01 06:57:24 -04:00
When example software does not show the `.` symbol, the issue is most likely related to connection errors of the external RS485 interface.
Check the RS485 interface connection with the environment according to schematic above and restart the application.
Then start terminal software and open the appropriate serial port.
2018-04-11 02:56:00 -04:00