2

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!

Markus
  • 147
  • 2
  • 10
  • 1
    Why don't you care about breath(teensy). Your speed is `9600` so need wait `0.3` second (minimal and every loop). Which side sending first (for clear buffer)? – dsgdfg Sep 18 '15 at 09:21
  • Timeout work on when finished your `read` command. You used `readline` but haven't any idea 'how many line you got ?'. Try: `ser.read(ser.inWaiting())` clear all data in buffer for serial timeout exception. – dsgdfg Sep 18 '15 at 09:57
  • It doesn't matter whether I use `ser.read(ser.inWaiting())` or just `ser.read()` or `ser.readline()`. The Raspberry is sending first (one line), the Teensy/Arduino answers with around 1200lines/minute. Do you think I should increase the `timeout`? – Markus Sep 18 '15 at 11:52
  • so that big. rotate teensy data with serial command. don't send unused data to pc(on teensy). Starting point read modbus documentation. But you need check, single package sending time(on teensy). Large packages that can not respond to fast. don't increase create small package... – dsgdfg Sep 18 '15 at 12:07

1 Answers1

3

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
Patrick Maupin
  • 8,024
  • 2
  • 23
  • 42
  • That is exactly what I was looking for. Works like a charm. But I'am sure that I tried it before without success. Thank you! – Markus Sep 21 '15 at 11:17