Sometimes I do not get any data in via the serial interface and I want to catch this case with an exception in the following way:
ser = serial.Serial(3)
ser.baudrate = 115200
timeout = 1
while (some condidion)
try:
dump = ser.read(40)
except ser1.SerialTimeoutException:
print('Data could not be read')
ser.close()
However, when I run this program, the exception is never caught when no
data is in the buffer and I am stuck in an endless loop. Anyone an idea
what I am doing wrong here?
I didn’t even know about that exception. After having a look at the API documentation, you’ll see that this exception only applies to
write().If you
read(), you’ll just have a shortened or even empty output.And this applies only if you have opened the connection with a timeout. Something like
serial.Serial(3, timeout=.1).