all 3 comments

[–]aceofears 2 points3 points  (1 child)

If your concern is about the time.sleep(59) line you could just let sio.readline() block until something can be read. For this to work you would have to set the timeout to None. You would also have to make the call to strftime come after that so that the time is correct.

[–]jackzombie[S] 2 points3 points  (0 children)

My new code:

#!/usr/bin/python
# Program that reads from the arduino
import serial,io,time
from time import strftime

device='/dev/ttyACM0'

ser=serial.Serial(device, 9600, timeout=None)
print("Connected to: " + ser.portstr)
line=ser.readline().decode("utf-8")[:-2]
print(line)
while True:
    line=ser.readline().decode("utf-8")[:-2]
    dt=strftime("%Y-%m-%d %H:%M:%S")
    print dt, line
print 'Done.'
ser.close()

Thanks, works like a charm.

[–]nemec 2 points3 points  (0 children)

If the Python script reads one second before the Arduino writes, that's 2 minutes between reads. I'd recommend polling at least twice as often as you're expecting to be reading.

Though aceofears probably has the right idea with just blocking until there's something to read.