pyserial

M

Mimi

Hi,
I use the pyserial to read data from a serial port.
My code is in window Xp and python 2.4. when I use Hyperteminal I can
read data without try and try again that it is not the case with
pyserial library.
anyone can help me ?
this is a part of my code:

self.ser = serial.Serial()
self.ser.baudrate = 9600
self.ser.port = 3
self.ser.timeout= 10
self.ser.bytesize = serial.EIGHTBITS
self.ser.stopbits = serial.STOPBITS_ONE
self.ser.xonxoff = 0

nbHisto = 144
for i in range(0,nbHisto):
while 1:
print self.ser.flushInput()
print self.ser.flushOutput()
cmd = "%xs\r" %(i+1)
self.ser.write("%s" %cmd)

#print cmd

histo = self.ser.readlines()
#print histo
if histo:
if histo=="\r\n":
pass
else:

histoAdresse = int(histo[0].strip('\r\n'))
print histoAdresse

try:
dateHisto_cur = listeFenetreNonNul["%s"
%(histoAdresse)]
print dateHisto_cur


self.ecrireHistoDansFic(histo,histoAdresse,dateHisto_cur,histoAdresse,i)
break
except:

print "Adresse pas trouvee dans le
systeme %s" %(histoAdresse)
break
# End if

sleep(10)
# End while
#End for


self.ser.close()
 
P

Philippe Martin

Hi,

I never found the need to flush anything and I always use inWaiting prior to
reader.

A+

Philippe
 
P

Petr Jakes

Few notes from the Python beginner :) HTH.
I do not understand what do you mean by the expression: ".... I can
read data without try and try again....." Can you be more specific
please. I do not understand what do you exactly mean. AFIK the pyserial
is waiting while the data occur on the serial port (so called blocking
read) so it is not necessary to do some "polling" (checking the serial
port periodically) .

Following snippets of code is running in infinitive loop, but it is not
necessary too be worried about processor utilization because the
readline waits for the data on the serial port and the code continues
to run when data occurs or when timeout passes.

import serial
s = serial.Serial(port=0,baudrate=4800, timeout=20)
while 1:
line = s.readline()
# byte = s.read(1) # or you can read No. of bytes according your
needs

Alternatively you can monitor buffer of the serial port and while data
in it, you can read it.

while fd.inWaiting() != 0:
s.read(1)

you can find plenty of examples about pyserial here
http://tinyurl.com/p8tt5

What I am not able to figure out is why are you trying to print out
input and output buffers (print self.ser.flushInput()) Does it print
out something?

Petr Jakes
 
M

Mimi

Thanks Peter,
because you have understood my need: a little understanding of
readlines() function.

# Following snippets of code is running in infinitive loop, but it is
not
necessary too be worried about processor utilization because the
readline waits for the data on the serial port and the code
continues
to run when data occurs or when timeout passes.
#

my python script tries to read a lot of data (the histograms) and that
can take many times (3 min) to download one file. and data do not
arrived in the same time you can wait few seconds between data ( data
response for the same command).
do you think that readlines() function will wait until all data are
arrived or when the timeout expired ?
if the data take more time, the readlines() will wait or will break on
the timeout ?
with hyperterminal the data are echoed when they arrived and you can
see that they do not arrived all the same time.
the timeout is reset when the first data arrived or it is only ignored
?
 
P

Petr Jakes

I can recommend you to read pyserial documentation and look to the
examples. Browsing through this discussion group can help you a lot as
well.
Generally if the timeout is set to 0 (zero) the code will wait and wait
and wait till the data will arrive to the serial port (if there is not
data on the serial port, you can wait "forever" and you can think your
code is "frozen" :) )
Because of that, you can set the timeout, so after some time of waiting
for the data (when the data do not arrive) the code continues on the
next line.

Try to experiment a little bit with some simple and short code first,
so you will be able to understand how to communicate with the serial
port.
HTH
Petr Jakes
 

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

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top