I tried running your code on a Pi and ESP32 and it appeared to be working on my setup as you can see from the screenshot below.

I tried changing my config.txt file to match yours but I could not replicate the issue so the only thing I can think of is you have another process that is opening and closing the serial port and it is occasionally conflicting with your python program. The lsof command would only show the process if it is open at exactly the same time as you run the command.
I did notice a couple of potential issues with your code that could be contributing to the problem.
In your python program, you are changing the GPIO state around the serial read instead of write so the RS485 Pi is spending most of its time in transmit mode. You can see this on the capture from my logic analyser.

I changed your python program to put the GPIO state changes around the write command and changed the read command to read_until so it will keep reading until it sees a new line character. In the ESP32 code, I added a new line character to the end of the response string. In the python program, I also added a new line character to the end of the write string so the ESP32 will send a response immediately instead of waiting for the serial read to timeout.
Python Codeimport timeimport serialimport RPi.GPIO as GPIOGPIO.setwarnings(False)GPIO.setmode(GPIO.BOARD)GPIO.setup(11, GPIO.OUT, initial=GPIO.LOW)GPIO.output(11, 1)ser = serial.Serial( port='/dev/serial0', baudrate = 115200, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1.5)ser.flush()while True: try: usr = input(">>> ") b = bytes(str(usr + "\n"), encoding='utf-8') GPIO.output(11, 0) ser.write(b) ser.flush() GPIO.output(11, 1) line = ser.read_until().decode('utf-8', errors="ignore").rstrip() print(line) except ValueError as error: print(error) except IOError as error: print(error)
ESP32 Code#include #include HardwareSerial ser(1);int enablePin = 13; void setup() { Serial.begin(115200); ser.begin(115200, SERIAL_8N1, 16, 17); pinMode(enablePin, OUTPUT); digitalWrite(enablePin, HIGH);}void loop() { while (ser.available()) { String stringData = ser.readStringUntil('\n'); if(stringData != ""){ Serial.println("Received: " + stringData); } if(stringData == "Echo"){ delayMicroseconds(40000); // delay 40ms for python to set the GPIO low on the Pi digitalWrite(enablePin, LOW); ser.write("Hello from esp32\n"); ser.flush(); Serial.println("Transmitting: Hello from esp32"); digitalWrite(enablePin, HIGH); } } }
This reduces the time the program takes to respond from over 1 second to less than 2 milliseconds which then caused a new problem. As you can see in the screenshot below the response was sent back from the ESP32 before the python program set the GPIO pin high so the RS485 Pi on the Raspberry Pi was still in transmit mode when the ESP32 sent its response and it was missed by the python program.

To get around this issue I added a 40ms delay before the ESP32 responds to the Echo command which as you can see from the screenshot below is just enough time for the python program to go back into receive mode.

Writing your Raspberry Pi program in a compiled language like C would probably reduce the time it takes for the GPIO pin to go high after a serial write but for now, the 40ms delay in the ESP32 code will allow you to keep using python.
Can you try these updated programs on your Raspberry Pi and ESP32 modules and see if this helps to solve the problems you are getting?