pyserial

Discussion in 'Python' started by Mimi, Feb 23, 2006.

  1. Mimi

    Mimi Guest

    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()
    Mimi, Feb 23, 2006
    #1
    1. Advertising

  2. 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,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()
    Philippe Martin, Feb 23, 2006
    #2
    1. Advertising

  3. Mimi

    Petr Jakes Guest

    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
    Petr Jakes, Feb 24, 2006
    #3
  4. Mimi

    Mimi Guest

    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
    ?
    Mimi, Feb 24, 2006
    #4
  5. Mimi

    Mimi Guest

    thanks philippe,
    Hi will take care for the that.
    Mimi, Feb 24, 2006
    #5
  6. Mimi

    Petr Jakes Guest

    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
    Petr Jakes, Feb 24, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Diez B. Roggisch

    Pyserial question

    Diez B. Roggisch, Jan 5, 2004, in forum: Python
    Replies:
    1
    Views:
    396
    Kelia Nichols
    Jan 5, 2004
  2. David Goodger

    Re: Pyserial question

    David Goodger, Jan 5, 2004, in forum: Python
    Replies:
    0
    Views:
    441
    David Goodger
    Jan 5, 2004
  3. jorjun
    Replies:
    1
    Views:
    637
    John P. Speno
    Jan 8, 2004
  4. Grant Edwards
    Replies:
    6
    Views:
    1,344
    Grant Edwards
    Jun 30, 2004
  5. SoftwareTester

    pyserial -- setting DTR_CONTROL_HANDSHAKE

    SoftwareTester, Nov 17, 2004, in forum: Python
    Replies:
    0
    Views:
    789
    SoftwareTester
    Nov 17, 2004
Loading...

Share This Page