I found this post which asks a similar question. However, the answers were not what I expected to find so I'm going to try asking it a little differently.
Let's assume a function searching_for_connection
will run indefinitely in a while True
loop. It that function, we'll loop and preform a check to see if a new connection has been made with /dev/ttyAMA0
. If that connection exists, exit the loop, finish searching_for_connection
, and begin some other processes. Is this possible to do and how would I go about doing that?
My current approach is sending a carriage return and checking for a response. My problem is that this method has been pretty spotty and hasn't yielded consistent results for me. Sometimes this method works and sometimes it will just stop working
def serial_device_connected(serial_device: "serial.Serial") -> bool:
try:
serial_device.write(b"\r")
return bool(serial_device.readlines())
except serial.SerialException
return False