Unable to receive the data from RS232 to TTL Converter

I am trying to read the data form RS232 to TTL converter using PCAN. I have connected my ESP32's SERIAL1 pins to RS232 to TTL Converter's Rx & Tx pins. The module is recognized on the PCAN View but I am unable to receive the data. I am using the example code of CAN library available on Arduino IDE.
The Example Code:

#include <CAN.h>

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println("CAN Sender");

  // start the CAN bus at 250 kbps
  if (!CAN.begin(250E3)) {
    Serial.println("Starting CAN failed!");
    while (1);
  }
}

void loop() {
  // send packet: id is 11 bits, packet can contain up to 8 bytes of data
  Serial.print("Sending packet ... ");

  CAN.beginPacket(0x12);
  CAN.write('h');
  CAN.write('e');
  CAN.write('l');
  CAN.write('l');
  CAN.write('o');
  CAN.endPacket();

  Serial.println("done");

  delay(1000);

  // send extended packet: id is 29 bits, packet can contain up to 8 bytes of data
  Serial.print("Sending extended packet ... ");

  CAN.beginExtendedPacket(0xabcdef);
  CAN.write('w');
  CAN.write('o');
  CAN.write('r');
  CAN.write('l');
  CAN.write('d');
  CAN.endPacket();

  Serial.println("done");

  delay(1000);
}

The RS232 to TTL Converter I am using : https://robu.in/product/rs232-to-ttl-serial-port-converter-adapter-communication-module/

RS232 and CAN have nothing in common. There's some kind of confusion here.

1 Like

Lets sort this out, post an annotated schematic showing exactly how this is wired. Be sure to show all connections, power, ground, power sources. Also important include links to technical information on each of the hardware devices. What you are telling us is working as expected, it won't do much of anything.

But you don't use Serial1 anywhere in your code.
Also, what are those ESP32 Serial pins you use?

Yes, you are right. I got confused with both these protocols as they both use the DB9 connector.

My bad, I am using the Serial0 pins.
The serial pins I am using are:
Tx0 - GPIO1
Rx0 - GPIO3

You can't use those if you use serial monitor. Use pins GPIO16/17 for your converter
Serial2.begin(9600, SERIAL_8N1, 16, 17);
But first answer to posts#2 and 3.

Check that you have connected UART2 (Serial2) Port of ESP32 with TTL <-----> RS232 Converter as per Fig-1 and you have included this code in your sketch: Serial2.begin(9600).

Figure-1:

Thanks for the solution. I'll try that and let you know.
I have answered to those posts.

Oh thanks I'll try this and let you know.

Note that Rx2/Tx2 an Gpio16/17 is same thing, different boards have them labeled one way or another...

No problem. You've got several replies so I step back.

1 Like

Yes!

initially try a loopback test on the TTL-RS232 module, e.g. connect 9-pin D_type connector pins 2 Tx to 3 Rx (pin 5 is GND)

simple program to test loopback

// ESP32  Serial1 test - for loopback test connect pins RXD1 and TXD1

#define RXD1 16 // can map Serial1 and Serial2 to many ESP32 GPIO pins
#define TXD1 17 // check pin usage https://randomnerdtutorials.com/esp32-pinout-reference-gpios/

// for RS232 shield connect
// ESP32 RXD1 to TTL/RS232 Rx
// ESP32 TXD1 to TTL/RS232 Tx
// connect GND pins together and VCC to 3.3V on ESP32 5V on UNO ect
// for loopback test connect 9-pin D_type connector pins 2 Tx to 3 Rx (pin 5 is GND)

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
  Serial.printf("\n\nESP32 serial1  test RXD1 pin %d TXD1 pin %d\n", RXD1, TXD1);
  Serial.printf(" loopback test connect pin %d to pin %d\n", RXD1, TXD1);
  Serial.printf("RS232: ESP32 pin %d RXD1 to TTL/RS232 Rx and pin %d TXD1 to TTL/RS232 Tx\n", RXD1, TXD1);
  Serial.printf("RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx\n");
}

void loop() {
  // read from Serial1, send to Serial
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }
  // read from Serial, send to Serial1
  if (Serial.available()) {
    int inByte = Serial.read();
    //Serial.write(inByte);     // local echo if required
    Serial1.write(inByte);
  }
}

The module which I am using is the wrong one. I have ordered a new one and will test this on that.
Thanks!!

When DPin-16/17 are for Serial2/UART2 by default, then is there any advantage of routing Serial1/UART1 into thes pins?

you can map ESP32 Serial1 and Serial2 to the majority of GPIO pins whatever is convenient, e.g. Serial2 to pins 18 and 19

#define RXD2 18
#define TXD2 19

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);

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