I'm reading data from USB serial with:
s = serial.Serial(port="COM1", baudrate=115200, write_timeout=0.010, timeout=0.010)
while polling:
r = s.readline().decode("ascii")
if r != '':
v = float(r)
print('new value', v)
time.sleep(0.005)
I'm expecting data at a rate of 50 values per second, but in order to not miss anything, I'm only waiting 5 ms (200 Hz). This works but it is not optimal.
Is there a way to make readline()
blocking? i.e. it should't return
anything until new data comes. This way I could totally avoid the time.sleep(...)
with the arbitrary 0.005
duration.
Problem: it seems that the hardware returns an empty string (or an empty string with newline) each time I do readline()
when there is no new data.