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 = "";
}
}
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
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
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
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