Connection to RS232 via ESP32 - HardwareSerial.available always 0

I am using a ESP32S3 Dev Board an try to connect a RS232 interface via MAX3232-component. I have connected 3V3 and GND and MAX3232-RX to Pin GPIO47 and MAX3232 to Pin GPIO48. The communication is at first a request. The device gives me then a response.

SerialRS232.available() returns always false. Can somebody tell me why?

The output of serial monitor will be “Try to read bytes from RS232 ... : false”

On Core0 the ArduinoOS is running and this core sends a udp package to LAN. This part works fine and I see this package every second, e.g. in Wireshark.

Reading sensor data should run on Core1. This part makes problems.

Here is my code:

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <ArduinoJson.h>
#include <HardwareSerial.h>

// W5500 Pin Definitions
#define W5500_CS     14                               // Chip Select pin
#define W5500_RST     9                               // Reset pin (optional, not used here)
#define W5500_INT    10                               // Interrupt pin (optional, not used here)
#define W5500_MISO   12                               // MISO pin
#define W5500_MOSI   11                               // MOSI pin
#define W5500_SCK    13                               // Clock pin

// RS232 Pin Definition
#define RS232_RX_PIN 48
#define RS232_TX_PIN 47

// Local configuration of project settings
byte mac[] = { 0x34, 0xF5, 0x28, 0xA0, 0xED, 0x40 };  // MAC from ESP32 device
IPAddress ip(192, 168, 1, 10);                        // Static IP address
IPAddress dns(192, 168, 1, 1);                        // DNS server
IPAddress gateway(192, 168, 1, 1);                    // Gateway address
IPAddress subnet(255, 255, 255, 0);                   // Subnet mask
String sourceIP = "192.168.1.10";
String sensor = "RS232";

// Target configuration

// Important IPs within the project (one of these must be set):
IPAddress targetIP_Broadcast(192, 168, 1, 255);

unsigned int targetPort = 8889;
unsigned int localPort = 8888;
StaticJsonDocument<200> jsonMap;
String targetMessage;

EthernetClient client;
EthernetUDP Udp;

HardwareSerial SerialRS232(2);
const int NR_OF_READ_BYTES = 7;

byte rs232TxData[] = {0x01, 0x03, 0x00, 0x35, 0x00, 0x01, 0x94, 0x04};

// Helper vars for sending udp package
unsigned long lastSendTime;
unsigned long currentTime;
const int UDP_SEND_DELAY_IN_MS = 1000;

TaskHandle_t Task_ReadSensor;


void setup() {
  // Start serial communication
  Serial.begin(115200);                               
  unsigned long time = millis();
  while (!Serial && (millis() - time < 5000)) {
    ; 												  // Abort after 5s, if Serial is not available
  } 

  // Initialize SPI with custom pin configuration
  SPI.begin(W5500_SCK, W5500_MISO, W5500_MOSI, W5500_CS);

  // Initialize Ethernet with static IP settings
  Ethernet.init(W5500_CS);
  Ethernet.begin(mac, ip, dns, gateway, subnet);
  Udp.begin(localPort);

  // Verify if IP address is properly assigned
  if (Ethernet.localIP() == IPAddress(0, 0, 0, 0)) {
    Serial.println("Failed to configure Ethernet with static IP");
    while (true);                                     // Halt on failure
  }

  SerialRS232.begin(115200, SERIAL_8N1, RS232_RX_PIN, RS232_TX_PIN);


  // Assign methods to cores and use multi processing
  xTaskCreatePinnedToCore(
    readSensor,              /* Task function. */
    "ReadingSensor",         /* name of task. */
    10000,                   /* Stack size of task */
    NULL,                    /* parameter of the task */
    1,                       /* priority of the task */
    &Task_ReadSensor,        /* Task handle to keep track of created task */
    1);                      /* pin task to core 1 */
}


// Reads information of rs232 interface
void readSensor(void * pvParameters) {
  Serial.print("readSensor running on core ");
  Serial.println(xPortGetCoreID());
  for (;;) {
    SerialRS232.write(rs232TxData, sizeof(rs232TxData));
    delay(100);
    Serial.print("Try to read bytes from RS232 ... : ");
    Serial.println(TryGetSerialData());
  }
}

String TryGetSerialData() {
  delay(10);
  if (SerialRS232.available()) { // This is always false !
    delay(10);
    // Hint: For our case, we expect always NR_OF_READ_BYTES Bytes as a response length
    byte data[NR_OF_READ_BYTES];
    SerialRS232.readBytes(data, NR_OF_READ_BYTES);

    // Do someting with the bytes

    return "true";
  }
  return "false";
}


void loop() {
  createAndSendUDPPackage(); //This works fine.
}

Can somebody give any hint, whats going wrong here?

have a look at simple serial example code and get that working on its own .

and ... TX >RX and RX>TX

We need a wiring diagram and how the MAX232 is connected ( have you looked at its data sheet?)

I did just switching these two pins. But nothing has changed.

MAX3232 is this product here: https://de.aliexpress.com/item/1005006585171220.html

MAX3232.VCC is connected to 3V3 on Board

MAX3232.GND is connected to GND on Board

MAX3232.RX is connected to GPIO47

MAX3232.TX is connected to GPIO48

So, actually rx and tx are crossed.

@abuech2s

That is wrong
For that module RX-RX and TX-TX

What are you connecting to?

As I wrote, I have tried both ways. Both with the same result.

The device is this one here: https://www.controlin.com/wp-content/uploads/2021/05/intelidrive-ipu-comap-2.pdf

But only the connection I gave is correct.

You may also need a null modem cable to connect to that device.

So, a direct connection MAX3232 to this device is not possible?

Don't know.
There is no information in that data sheet about the RS232 connection but if it was intended to connect to a PC then you need a null modem cable.

initially try a simple loopback test, e.g.

// ESP32-S3 DevkitC-1  Serial1 test - for loopback test connect pin TXD to pin  RXD"
// ss https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/_images/ESP32-S3_DevKitC-1_pinlayout_v1.1.jpg

#define RXD1 47
#define TXD1 48

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  Serial1.begin(9600, SERIAL_8N1, RXD1, TXD1);
  Serial.println();
  Serial.printf("\n\nESP32-S3 DevkitC-1 serial1  test pin GPIO%d TXD pin GPIO%d RXD\n", TXD1, RXD1);
  Serial.printf("   for loopback test connect pin GPIO%d TXD to pin GPIO%d RXD\n", TXD1, RXD1);
    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 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);
  }
}

serial monitor output

ESP32-S3 DevkitC-1 serial1  test pin GPIO48 TXD pin GPIO47 RXD
   for loopback test connect pin GPIO48 TXD to pin GPIO47 RXD
RS232: ESP32 pin 47 RXD1 to TTL/RS232 Rx and pin 48 TXD1 to TTL/RS232 Tx
RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx
loopback test 1
test2 1234567890
test3 abcdefghijklmnopqrstuvwxyz
1 Like

The intention is to have the chain: Device → MAX3232 → ESP32 → LAN → Switch.

@abuech2s
OK but you need to have the hardware connections correct or the software will not work.

So you may need a null modem cable between the RS232 board and that device.

if the device has an Ethernet interface why not connect it directly to the LAN?

It seems, that I have an older version of this device. I dont see any Ethernet/USB sockets.

can you show us some photos of the device?

see this

Need to have the correct connections!

Do you understand about the null-modem cable?

Yes, I think so. I looking, if I find such a cable. Or a “null modem mini adapter” (or anything like this). I guess this mini adapter has the same function.

As long as it says null-modem or crossover and the gender of the connectors on the two connectors is correct.

Also see post #15 that person had a similar issue.

Yes, I think so. I looking, if I find such a cable. Or a “null modem mini adapter” (or anything like this). I guess this mini adapter has the same function.

I mean some product like this: DELOCK Adapter Sub-D 9Pin Stecker/Buchse Nullmodem | Adapter, Gender-Changer günstig kaufen | reichelt elektronik

Would it be correct, that this adapter has just 8 pins?

No that adaptor just changes male to female

It must say null-modem or crossover

You can also try what I suggested in post #15