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

Z

zxo102

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) > 0:
aa = map(ord, reading)
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
 
T

Terry Reedy

Do you have the option of having the instrument insert them between
readings?
If the data you're reading doesn't contain "\n", then you can't
use readline() or readlines().

Use .read() instead.
 
Z

zxo102

If the data you're reading doesn't contain "\n", then you can't
use readline() or readlines().

But readlines() can read data out of 'com1' for many hours. Maybe I
should try read().

Ouyang
 
Z

zxo102

Do you have the option of having the instrument insert them between
readings?




Use .read() instead.

I can not have the instrument insert "\n" between readings. That is
out of my control. I can try read(). Thanks.

Ouyang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top