How to use multiple Serial ports in one programme

Can we change the pin no while using the program for serial communication in ESP32 board.

EX:
void loop(){
Serial2.begin(9600,SERIAL_8N1,RXD_1,TXD_1);
// after using this data
Serial2.flush();
Serial2.end();
Serial2.begin(9600,SERIAL_8N1,RXD_2,TXD_2);
//Again after using this data
Serial2.flush();
Serial2.end();
}

//but this is giving error, can anyone tell me why?
// and how to use more than 3 serial ports in ESP32

  1. the Serial ports get defined in the Setup() not the Loop()
  2. There are hardware ports that have predefined I/O pins. These cannot be changed.
    2a) The 2nd hardware serial port has limitations, read the specification.
  3. For 3 serial ports you will have to use a "softserial" library. This will allow you to create serial ports that are not hardware based. They also take more processor time as they are doing in software what the hardware ports do in hardware.

ESP32 IDF Serial Communication

APIs Used

  • uart_param_config()
  • uart_set_pin()
  • uart_driver_install()
  • uart_read_bytes()
  • uart_write_bytes()

This API is used to set the UART configuration like baud rate, stop bits etc. This API contains Two arguments. Have a look at this API below.

esp_err_t uart_param_config (uart_port_t uart_num, const uart_config_t *uart_config )

Return

  • ESP_OK Success
  • ESP_FAIL Parameter error

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.