ESP32 serial-to-serial bridge

I have an external device that uses UART for programming and configuration and it only uses the TX and RX pins, I'd like to attach an ESP32 to the other device's UART port so that I can see what the device is sending out.
Can an ESP32 be used as a serial-to-serial bridge, and if so, would I still be able to flash the other device's firmware through the ESP32 serial bridge?
I have no way of testing this without risking bricking the other device.

HardwareSerial HWSerial0(0);
HardwareSerial HWSerial2(2);

void setup()
{
    HWSerial0.begin(38400);    // CH340C
    HWSerial2.begin(38400);    // external microcontroller
}

void loop()
{
    if (HWSerial2.available() > 0)
    {
        HWSerial0.write(HWSerial2.read());
    }
    if (HWSerial0.available() > 0)
    {
        HWSerial2.write(HWSerial0.read());
    }
}

This explanation may make sense to you, but I couldn't understand your need.

As a drawing speaks more than a thousand words, I suggest you draw a picture of what you want to do.

It can even be freehand and the devices can be simple black boxes.

The important thing is to show the relationship between the ESP, the device that programs it and the device that receives the program. (if I understand this part).

Dont't miss te GND line connection between external MCU and ESP32.

You can try by reading the signal on the RX-line of ESP32 and display it on the Serial Monitor of Host.

Search for "usb to ttl serial cable". Maybe you do not need the ESP32.

are you sure your UART is outputting TTL serial levels (0 to 5V) not RS232 or RS485?
you can check the TX line with a multimeter should read 5Volts when idle

try the following code to read the serial data (change pin definitions to suit your configuration)

// ESP32 serial2 hardware loop back test - jumper GPIO16 (Rx) and GPIO17 (Tx)

// see https://circuits4you.com/2018/12/31/esp32-hardware-serial2-example/
/* There are three serial ports on the ESP known as U0UXD, U1UXD and U2UXD.
 * 
 * U0UXD is used to communicate with the ESP32 for programming and during reset/boot.
 * U1UXD is unused and can be used for your projects. Some boards use this port for SPI Flash access though
 * U2UXD is unused and can be used for your projects.
*/


#define RXD2 16
#define TXD2 17

void setup() {
  // Note the format for setting a serial port is as follows: Serial2.begin(baud-rate, protocol, RX pin, TX pin);
  Serial.begin(115200);
  Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
  Serial.println("ESP32 hardware serial test on Serial2");
  Serial.println("Serial2 Txd is on pin: "+String(TXD2));
  Serial.println("Serial2 Rxd is on pin: "+String(RXD2));
}

void loop() { //Choose Serial1 or Serial2 as required
  while (Serial2.available()) {
    Serial.print(char(Serial2.read()));
  }
  while (Serial.available()) {
    Serial2.print(char(Serial.read()));
  }
}

Both RX and TX pins on the other device measure about 3.3V, it uses a DP32G030 microcontroller.

I soldered the ESP32's UART2 pins to the other device's UART pins, RX to TX and TX to RX, and no I did not forget about the ground.

I loaded the sketch from my original post on the ESP32, but I was not able to read the configuration from the other device through my ESP32 serial bridge / serial passthrough.
I was hoping for the ESP32 to be "transparent" to the host and to the other device.

I tried replacing if with while, I tried the regular Serial and Serial2 objects instead of HardwareSerial, I tried swapping the order of the if statements inside the loop, nothing worked.

which ESP32 pins are you using for Tx and Rx?
have you tested your ESP32 serial code by carrying out a loopback test?
connect the Tx and Rx pins and characters entered on the serial monitor keyboard are echoed to the serial monitor display

for example, if you load the Serial2 program of post 6 and link pins 16 and 17 the loopback works

ESP32 hardware serial test on Serial2
Serial2 Txd is on pin: 17
Serial2 Rxd is on pin: 16
text entered on keyboard
test 2 1234567890
test3 abcdefghijklmnopqrstuvwxyz

The serial bridge does work, the problem was in setup()

HWSerial0.begin(38400);
HWSerial2.begin(38400);

UART2 port requires specifying the pins, even when using the default pins

HWSerial0.begin(38400);
HWSerial2.begin(38400, SERIAL_8N1, 16, 17);

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