Espressif and Arduino use the same UART numbering, and there are no hidden properties of hardwareSerial that are bound to it UART instance So it's possible to perform a reach-around and replace the arduino harwareSerial UART driver instance with an espressif RS485 uart instance and then use Arduinos hardwareSerial methods to communicate with the UART.
#include <Arduino.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <driver/uart.h>
#define ECHO_UART_PORT UART_NUM_1
#define ECHO_TEST_TXD (25)
#define ECHO_TEST_RXD (26)
#define ECHO_TEST_RTS (27)
#define ECHO_TEST_CTS UART_PIN_NO_CHANGE
#define BUF_SIZE (127)
#define BAUD_RATE (9600)
#if ECHO_UART_PORT == UART_NUM_0
#define SERIAL_485 Serial
#error Serial is already committed elsewhere in this code
#elif ECHO_UART_PORT == UART_NUM_1
#define SERIAL_485 Serial1
#elif ECHO_UART_PORT == UART_NUM_2
#define SERIAL_485 Serial2
#endif
static const char *TAG = "RS485_ECHO_APP";
void setup() {
Serial.begin(115200); //uses UART_NUM_0
//configure UART_NUM_2 for RS485
const uart_port_t uart_num = ECHO_UART_PORT;
uart_config_t uart_config = {
.baud_rate = BAUD_RATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.rx_flow_ctrl_thresh = 122,
};
Serial.println("Start RS485 application test and configure UART.");
// start arduino serial port
SERIAL_485.begin(BAUD_RATE);
SERIAL_485.setTimeout(1+(10000/BAUD_RATE) ); // 10000 is 10 symbols per byte * 1000 ms per second
// drop rhe Arduino installed UART driver!
// we will replace this with a 485 capable driver.
uart_driver_delete(uart_num);
// Configure UART parameters
uart_param_config(uart_num, &uart_config);
Serial.println("UART set pins, mode and install driver.");
// Set UART1 pins(TX: IO23, RX: I022, RTS: IO18, CTS: IO19)
uart_set_pin(uart_num, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
// Install UART driver (we don't need an event queue here)
// In this example we don't even use a buffer for sending data.
uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0);
// Set RS485 half duplex mode
uart_set_mode(uart_num, UART_MODE_RS485_HALF_DUPLEX);
ESP_LOGI(TAG, "UART start recieve loop.\r\n");
SERIAL_485.printf("Start RS485 UART test.\r\n");
}
driver/uart.hand copy-paste code from the official RS485 example (github.com/espressif/esp-idf/blob/master/examples/peripherals/…). See my example at pastebin.com/2PtWJvd6. What is your exact error message?flush()after sending any data.