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?