473,480 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

pyserial

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,dateHis to_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()

Feb 23 '06 #1
5 9930
Hi,

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

A+

Philippe

Mimi wrote:
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,dateHis to_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()


Feb 23 '06 #2
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

Feb 24 '06 #3
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
?

Feb 24 '06 #4
thanks philippe,
Hi will take care for the that.

Feb 24 '06 #5
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

Feb 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
10962
by: Bob Greschke | last post by:
We have some equipment that communicates at 57600 baud RS232. The path from the PC is USB to a Phillips USB hub, then off of that a TUSB3410 USB/Serial converter. The driver for the 3410 chip...
4
5053
by: Zarathustra | last post by:
Hi, where I can find the pyserial handbook?? THanks
8
9126
by: Alejandro | last post by:
Hi: I'm using pySerial to talk to a RS232 to RS485 converter. In order to control the converter, I need to control the DTR line to enable/disable de RS485 driver. In particular, I need to : ...
2
3728
by: Jon | last post by:
Hi, I wrote some code to read in info from a port using pyserial. the code reads info sent by a box that is connected to my computer by an rs232-to usb adapter. When I was writing the code and...
15
16786
by: Lone Wolf | last post by:
I'm trying to get data through my serial port from a CMUcam. This gizmo tracks a color and returns a packet of data. The packet has nine data points (well, really eight since the first point is...
3
8136
by: Ron Jackson | last post by:
I am using Python 2.5 on Windows XP. I have installed Pyserial and win32all extensions. When I try to run the example program scan.py (included below), or any other program using pyserial, as...
0
596
by: pauland80 | last post by:
<snip> <snip> Late thanks for your both answers! (Please excuse me for that) The problem was a bug in the device firmware. But before finding this, I dugg lightly in the pyserial source...
0
2045
by: [david] | last post by:
http://pyserial.sourceforge.net/ "port numbering starts at zero, no need to know the port name in the user program" But the implementation in SerialWin32 is just (Portnum +1)
3
11509
by: naveen.sabapathy | last post by:
Hi, I am trying to use virtual serial ports to develop/test my serial communication program. Running in to trouble... I am using com0com to create the virtual ports. The virtual ports seem to...
2
4143
by: bryanleo | last post by:
We are trying to read data from a microcontroller and interface it through serial port. The output is then displayed in Python using Pyserial or the hyperterminal, the former is more important ...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7084
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6739
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4779
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
181
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.