pyserial: failed to readlines() after many hours running.

zxo102 zxo102 at gmail.com
Sat Oct 11 10:30:31 EDT 2008


Hello All,

I have a system. An instrument attched to 'com1' is wireless connected
to many sensors at different locations.  The instrument can forward
the  "commands" (from pyserial's write()) to those sensors.  Based on
the "commands",  the sensors keep sending corresponding data back to
the instrument which wraps up those data and put into "com1" . The
readlines() of pyserial pick up those data for processing.
The data ’string' does not have "\n".

With the following pythong script, if timeout = 0.1,  ser.readlines()
in thread1 failed to receive any data after approximate 20 hours. if
timeout=0.5,  ser.readlines() in thread1 failed to receive any data
after approximate 60 hours.  I am not sure the thread1 was dead or
not. But the whole script did not throw out any error information and
ser.write() in thread2 was ok and kept sending "commands" to com1.

I am testing "timeout = 1" right now, it will probably take more days
to fail to receive data.

Anybody knows how long I should set for "timeout"?  Since the data are
from different sensors, I have no idea when they arrive at com1 via
that instrument. If the timeout is set too long,  com1 (com1 has
buffer? Sorry, I don't know very much about hardwares) can not have
enough buffer to hold those coming data before ser.readlines(). Or how
does ser.readlines() work?

Should I use readline() instead of readlines()?

Thanks for your any help in advance.

The below is the script:


In thread 1:

import serial, time

ser=serial.Serial('com1', baudrate=9600, bytesize=8, parity='N',
stopbits=1, timeout=0.1,xonxoff=0, rtscts=0)

while 1:
         reading = ser.readlines()
         for i in range(len(reading)):
               if len(reading[i]) > 0:
                    aa = map(ord, reading[i])
                    bb = ["%02X"%aa[k] for k in range(len(aa))]
                    # do something here
	  else:
                    pass
         time.sleep(ReadComSleepTime)
ser.close()

In thread 2:
...
while 1:
     ...
     ser.write("some commands here")
     ...
     time.sleep(30)



Best Regards

ouyang



More information about the Python-list mailing list