ESP32 read data from RS232 device

Hi all,

I have an ESP32-EVB and a MOD-RS232 boards. I need to read data from an rfid card device which uses RS232 protocol but I can't achieve it.

Could you help me to know which pins of ESP32 I can use for comunication or if I need to change something at MOD-RS232? I'm using Serial1 as GPIO's 4 and 36.

#include <HardwareSerial.h>
#define RXD 4
#define TXD 36
String readString;
//HardwareSerial Serial1(1); // Configura el puerto serial 1
void setup() {
  Serial.begin(115200); // Inicia el puerto serial para monitorear los datos recibidos
  Serial1.begin(19200, SERIAL_8N1, RXD, TXD); // Inicia el puerto serial 1 con una velocidad de 19200 baudios, 8 bits de datos, sin paridad y 1 bit de stop
  Serial1.println("serial1test");
  Serial.println("Serial Txd is on pin: " + String(TXD));
  Serial.println("Serial Rxd is on pin: " + String(RXD));
}
void loop() {
  //Serial.print("Entrant al loop");
  if (Serial1.available()) { // Si hay datos disponibles en el puerto serial 1
    char c = Serial1.read(); // Lee el siguiente byte de datos
    readString += c;
    Serial.write(c); // Escribe el byte de datos en el puerto serial para su monitoreo
    Serial.print(readString);
  }
  // Verifica si se ha recibido la cadena de texto completa
  if (readString.endsWith("\n")) {
    // Se ha recibido una línea completa, imprime la cadena de texto completa y limpia el buffer
    Serial.println("Received string: " + readString);
    readString = "";
  }
}

pin 36 is input only - see esp32-pinout-reference-gpios

I have used this simple program to test ESP32 Serial1

// ESP32  Serial1 test - for loopback test connect pins 16 and 15

#define RXD1 16
#define TXD1 15

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
  Serial.println();
  Serial.println("\n\nESP32 serial1  test Rx pin 16 Tx pin 15");
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    //Serial.write(inByte);
    Serial1.write(inByte);
  }
}

connect pins 15 and 16 for a simple loopback test - characters entered on serial monitor should echo back

Hi Horace,

Thank you for you response, I have two questions:

  1. I think that my pinout is different (See here ESP32-EVB esp32 schematic: https://github.com/OLIMEX/ESP32-EVB/raw/master/HARDWARE/REV-I/ESP32-EVB_Rev_I.pdf) and I don't have clear which are the Pins or GPIO's I need to use.

  2. Do you know if I need to use a serial adapter (Like MOD-RS232 - Open Source Hardware Board) or I can connect the pins directly to my RS232 device?

Very gratefull,
Roger.

looking at the schematic

I think you have the RXand TX the wrong way around, e.g. try

#define RXD 36
#define TXD 4

if the device you are connecting too is RS232 you require a RS232 to 3V3logic level converter such as you reference

try my test program of post #2 changing the RXD1 and TXD1 to

#define RXD1 36
#define TXD1 4

connect GPIO4 to GPI36 and you can run a loop back test - characters entered on the keyboard should echo back onto the serial minitor display

if that works connect the RS232-TTL level converter and connect pins 2 and 3 of the 9pinD type connector - the loopback test should then work

The schematic which you can download from here

schows that the module will work if you connect 3.3V
See red marked boxes on the picture

best regards Stefan

Hi StefanL38,

I looked it with multimeter and I see that 3.3V voltage supply comes directly from the Olimex-EVB board.

Hi Horace,

I'm trying and trying but without success for now. With and without MOD-RS232.

what have you set RXD and TXD set too
looking at
image
it should be

#define RXD 36
#define TXD 4

did the loop back tests work?

If your ESP32 is soldered to a evaluation-board you have to lookup the datasheet of this evaluationboard to which IO-pins the 10pin-socket is connecetd to. My expectation is that it is not very likely that it will be the IO-pins RX 4 and Tx36

The way to find such information is quogling (quick googling)
or
quuckling (quick duckduckGo-ing
with keywords like schematic, manual, datasheet

and there it is
the link to olimex

and the olimex-site has the link to github

and there is a subdirectory
hardware with different revisions

best regards Stefan

Hi Horace,

I can't get the loop to work. I have tried the following cases with and without RS232 Mod adapter, inverting in each case the RXD and TXD declared in arduino:

GPIO's 36 and 4, GPIO's 16 and 17 & finally GPIO's 1 and 3.

The pins connected at my DB9 male connector are 2 (RXD), 3 (TXD) and 5 (serial GND)

Go small steps
You have to check each and every detail on its own with EXCLUDING other possabilities

directly connect the pins on the ESP-EVB-board-header

If this does not work
you have to cross-check if you are using the right pins on the header

connect the cathode of an LED to a 1 kOhm-resistor best would be soldering the resistor to one pin of the LED
connect Anode to header-pin connect free resistor-pin to gnd
write a small test code that does nothing more than blinking the IO-pin that you assume is connected to this pin

You can running around fast for hours and days without proceeding or you can slow down
checking each and every dammed detail on its own.
This requires time too but after that you know exactly at which point something does not work

best regards Stefan

I carried out some experiments using a ESP32 NodeMCU and found that to use Serial1 TXD1 GPIO4 and RXD1 GPI36 the pins had to be explicitly set as digital output and digital input respectivily, e.g.

// ESP32  Serial1 test - for loopback test connect pins 16 and 15
// version explicitly setting RXD1 pin as digial input and TXD1 pin as digital  output

#define RXD1 36
#define TXD1 4

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  pinMode(RXD1, INPUT);  // sets the digital pin  as input 
  pinMode(TXD1, OUTPUT);  // sets the digital pin  as output
  Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
  Serial.println();
  Serial.println("\n\nESP32 serial1  test Rx pin 36 Tx pin 4");
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    //Serial.write(inByte);
    Serial1.write(inByte);
  }
}

a loopback test with 4 and 36 connected gave

ESP32 serial1  test Rx pin 36 Tx pin 4
test 1 
test 2
test 1234567890

Text entered into serial monitor is echoed back to the display

Hi Horace,

Sorry for the delay in my response, I've been in another project to disconnect from that one. Finally your explanation was the solution.

Very gratefull to you and all who have helped,
Roger.

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