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.
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).
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()));
}
}
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