473,769 Members | 6,034 Online
Bytes | Software Development & Data Engineering Community
+ 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.baudra te = 9600
self.ser.port = 3
self.ser.timeou t= 10
self.ser.bytesi ze = serial.EIGHTBIT S
self.ser.stopbi ts = serial.STOPBITS _ONE
self.ser.xonxof f = 0

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

#print cmd

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

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

try:
dateHisto_cur = listeFenetreNon Nul["%s"
%(histoAdresse)]
print dateHisto_cur
self.ecrireHist oDansFic(histo, histoAdresse,da teHisto_cur,his toAdresse,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 9960
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.baudra te = 9600
self.ser.port = 3
self.ser.timeou t= 10
self.ser.bytesi ze = serial.EIGHTBIT S
self.ser.stopbi ts = serial.STOPBITS _ONE
self.ser.xonxof f = 0

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

#print cmd

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

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

try:
dateHisto_cur = listeFenetreNon Nul["%s"
%(histoAdresse)]
print dateHisto_cur
self.ecrireHist oDansFic(histo, histoAdresse,da teHisto_cur,his toAdresse,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(p ort=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.flushI nput()) 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
11038
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 creates a "normal" comm port (like COM3). There is a C++ program that has no problem talking to the equipment, but two Python programs have trouble. One is a big, scary, controller program, and the other, since that is the one I'm having trouble...
4
5075
by: Zarathustra | last post by:
Hi, where I can find the pyserial handbook?? THanks
8
9183
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 : write a character to the serial port set the DTR line to level 1 _after_ the last bit of the character is send
2
3741
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 testing it on my computer it worked fine. I ran py2exe on the program (which uses wxpython for its gui) and sent the output from the py2exe to another computer. now when I try to run it on this other computer it fails to open the port. it gives...
15
16872
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 just a packet header) separated by spaces as follows: M xxx xxx xxx xxx xxx xxx xxx xxx Here is the code I am using (python v24): import serial
3
8159
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 soon as it hits the statement: s = serial.Serial(i) I get the error:
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 and found (to take with care!) :
0
2078
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
11632
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 be working fine when I test it with Hyperterminal . I am using the example program that comes with pyserial, as below. --------------- import serial
2
4160
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 When you touch the input pins of the microcontroller the value changes real time in hyper terminal. But In the case of the pyserial module, even though you touch the input pins, the value does not change. We have actually noticed that python records and...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6675
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.