ESP32 serial2 giving error upon disconnecting

hey,
I have 2 esps32 connected to each other using Serial2 but it seems that when i disconnect the sender board, the receiver gets jumped with errors, idk why or how can i handle this or how to fix it

im using 2 esp32 nodemcu-32s

@randoms123 ,

Welcome to the forum..
Good job on the post!!

The error is from the receiver??

~q

thanks, yes the error is on the receiver

or atleast i think its only on the receiver, i dont have the sender connected to serial monitor since im sending the message using an outside program to write to uart serial connected to usb

maybe you get stuck in here??

~q

You're reading without first checking available in there..
if you're disconnected..

~q

you are right, will fix that and try again

seems to dont fix it, weirdly if i disconnect the sender even without sending anything, the receiver also gets the error, it starts receiving random bytes even tho the sender is disconnected and didnt send anything

it shouldnt even cross this first if:
if (SerialConnect.available() >= 12) { // Wait for header to be available

since there shouldnt be bytes available to read

depends on how you disconnect..
wires tend to be antennas..
and if you do get a bunch of unexpected bytes you're going to overflow that buffer as you don't currenty check to see if you are receiving too many bytes..

~q

maybe because UART start bit is a 0 when i disconnect the sender it reads 0's and the receiver starts reading those 0's? but then is there a way to invert UART start bits?

I'm thinking a state machine, lose the while and use the loop..
Got you in a sim now, give me a bit and i'll show you what i mean..
~q

it was the thing I said about the 0's, i used the invert option on sender and reader so start bit would be a 1 and now its working perfectly

thank you for helping me figuring it out, the discussion helped me a lot thinking

1 Like