Find the answer to your Linux question:
Results 1 to 1 of 1
Hi forum, I've looked at information coming from some equipment, and this is its output: "&DELUX,58.0,10" "&DELUX,58.5,10" "&DELUX,59.0,10" "&DELUX,59.5,10" .... I would like to use Python v2.5.4 to receive this ...
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    3

    Printing bytes from a serial port using python

    Hi forum,

    I've looked at information coming from some equipment, and this is its output:
    "&DELUX,58.0,10"
    "&DELUX,58.5,10"
    "&DELUX,59.0,10"
    "&DELUX,59.5,10"
    ....

    I would like to use Python v2.5.4 to receive this data, and pull out the 'E' to do further processing. This is my code:

    Code:
    import time
    import serial
    
    ser = serial.Serial(
                                port=0,
                                baudrate=9600,
                                bytesize=8,
                                stopbits=1,
                                timeout=0
                               )
    while 1:
                data = ser.read(5)
                 if data[2] == 'E':
                             print data[2]
                 else:
                          print 'Error'
    Unfortunately (for me), all I get printed is 'Error', which means Im trying to extract that byte in the wrong way.

    Could anyone kindly point me in the right direction. This is getting a bit frustrating


    Update:
    So I figured out my problem. I was searching for an 'e' instead of 'E'. problem solved.

    I've since modified my code and it all works fine. my new code:


    Code:
    import time
    import serial
    
    ser = serial.Serial(
                                port=0,
                                baudrate=9600,
                                bytesize=8,
                                stopbits=1,
                                timeout=0
                               )
    while 1:
                data = ser.read(14)
                 if data[0] == '&':
                             print data[7:10]
                 else:
                          print 'Error'
    My new question:
    Is there a faster (more efficient) way of executing my new code ?

    much thanks in advance.
    -stopgo
    Last edited by stopgo; 12-14-2010 at 09:26 PM. Reason: Found my error

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...