Ok I don't get this. I've looked everywhere now, but I don't see why this is not working:
def main():
time = sys.argv[1]
ser = serial.Serial('/dev/ttyACM0',9600, timeout=1)
paramstr= "A 5 " + time + " 0 0 0"
ser.write(paramstr)
print 'sent'
print 'now listening...'
while True:
dbstr = ser.readline()
fo.write(str(dbstr));
fo.close()
ser.close()
print 'exiting.'
This is my def main
in python
. What I'm doing, is sending a string over serial
from my Raspberry Pi to my Teensy (Arduino). The Teensy successfully starts a program and sends 1200 lines back over serial
to the raspberry. This is working so far.
What does not work is the while
loop. The data is written to the file, but the loop goes on forever, although the transmission (Teensy->RPi) has already stopped. For this case I implemented a timeout=1
, but seems to be ignored. The program does not come out of the while loop.
Can somebody pleas help? Thanks in advance!
The timeout will not affect the while loop. It will only affect the maximum time that each call to read()
or readline()
will wait. If you want to stop looping when you are no longer receiving data, then stop looping when you are no longer receiving data. E.g. something like this:
while True:
dbstr = ser.readline()
fo.write(str(dbstr));
if not dbstr:
break