ESP32 + RS232 TTL serial communication

Hello!
I’m currently trying to communicate my ESP32 DevKitV1 through serial. I bought a TTL-RS232 like this
So I wired it to the ESP32 like the image attached, crossing the TX/RX pins.
This is my code:

#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);
  //Serial1.begin(9600, SERIAL_8N1, RXD2, TXD2);
  Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
  Serial2.println("serial2test");
  Serial.println("Serial Txd is on pin: " + String(TX));
  Serial.println("Serial Rxd is on pin: " + String(RX));

}

void loop() {
  Serial2.print("hello");
  Serial2.write("hellowrite");
  Serial.print(char(Serial2.read()));
  delay(1000);
}

I’m using HardwareSerial since I’ve seen everyone saying it’s better that SoftwareSerial, and what I want to do is to read from the Serial2 (connected to the RS232) and print it on Serial (ESP32). With this code I can print on both serials just fine (sort of) but I can’t seem to be able to read from Serial2: look and the output on 2nd attachment.

The final goal is to be able to type something on Serial2 so it’s sent to the ESP32, and read it correctly.
Am I missing something? Should I not be crossing the TX/RX pins?

To use the serial ports on the ESP32, I use do the following:

The ESP32 has 3 user serial ports (0), (1), (2). For now, I will write about (1) and (2), (0) has a bit more of a trick to it.

For the ESP32 I include #include <HardwareSerial.h>

Next I define the serial ports

#define SerialDataBits 115200
HardwareSerial SerialTFMini( 1 );
HardwareSerial SerialController( 2 );
// serial(1) = pin27=RX green, pin26=TX white
// serial(2) = pin16=RXgreen , pin17=TX white

The default pinouts for (1) Should not be used and can be redefined in setup. The default pin out for (2) works well.

In setup I have:

SerialController.begin( SerialDataBits );
 SerialTFMini.begin(  SerialDataBits, SERIAL_8N1, 27, 26 );

Works quite well.

For the ESP32, reserve Serial1 for the monitor. Serial1 and (1), for the ESP32 are not the same serial ports.

You can assign (ESP32), the GPIO pins 0 - 31 to input and outputs, GPIO pins 32 and up are input only.

Hi! After changing some things, I realised that I didn't need to cross the TX/RX pins (against my own logic but w/e). So Tx->Tx, Rx->Rx it is. I didn't need to necessarily change the name of the serial, so if I included HardwareSerial.h or called HardwareSerial Serial2(2) i.e., resulted in an error saying there were multiple calls to Serial2, since it is called in some library of the ESP. If I use Serial2, there's no need to #include anything or call a constructor as it is already defined on the ESP libraries or so I understood.

#define RXD2 16
#define TXD2 17
char c;
String readString;
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(9600, SERIAL_8N1, RXD2, TXD2);
  Serial2.println("serial2test");
  Serial.println("Serial Txd is on pin: " + String(TX));
  Serial.println("Serial Rxd is on pin: " + String(RX));

}

void loop() {
  while (Serial2.available()) {
    c = Serial2.read();
    readString += c;
  }
  if (readString.length() > 0) {
    Serial.print(readString);
    Serial2.print(readString);
    //server.print(readString);
    readString = "";
  }
}

What you type on Putty opened on that COM port, will be printed on the Serial monitor of the ESP and putty too, just to check. I've had something weird going on, since I'm connecting VCC of RS232 on ESP32 3v3, and some chars are weird, then I took my Arduino Uno and connected 5v to VCC on RS232 and there wasn't any problem, but hey both approaches worked for now.

I ran your code and can verify that no includes are needed, Serial2 just works. I was actually able to remove the pin definitions and use the simpler begin() that takes only the bit rate and it still worked.

I also noticed that the RX and TX seem reversed to what I would expect.

I tested this against a Victron Energy MPPT solar charge controller over its VE.direct port and received messages without issue. I did not attempt to send any data back. Worked flawlessly at 19200 bps.

Since the Victron uses 5V signals, I used a couple of transistors to match the voltage to the 3.3V that the ESP can deal with. I had no issues with strange characters or anything else.

Thanks for the post, it kept me moving!