I'm using ESP32 for UART comm with a device that may be physically disconnected. It's all good while it's connected, but when I take out the UART wires ESP32 continues to receive some random data.
Is this expected (maybe non-terminated pins have random noise on them?)
How can I detect that UART is properly connected?
Serial is itself an "open ended" protocol when it comes to connection status. Monitoring the state of that is the responsibility of the transport layer software that uses it.
In other words, valid input is defined by what you expect vs. what you receive at the input.
Very definitely. What you experience is noise from an unterminated or floating pin. A simple pull down say 10K to ground will cause it to cease. This can happen at any baud, the band rate determines to some extent what it will decode it as.
You need a bit to lock on to then after a predetermined time it will look for another bit clocking whatever value it sees on the pin as data. Remember the noise although in analog format will be come 1 or zero depending on the signal level at the processor port. What is in between will be interpreted as 1's or zero's. I see it a lot because I am about a long drive with a #1 wood away from a communication tower. I have used UARTs as tone generators, with a bit of care they will work nicely.
Try it with maybe a 2' piece of wire then terminate it and see the difference.
So I called the below, random noisy data disappeared when un-terminated, and it works while connected as well. Is it the right thing to do? Are there any unexpected consequences? I'm quite new to this game
well simply put since the parity is 'None', all it needs is a start-bit, so basically the pin going from HIGH to LOW, after that the pin-state will be read to fill FIFO, and after elapsed time again the pin needs to go from the stop-bit HIGH (or '0') to LOW for the start-bit again.