mercredi 22 avril 2015

Python error in If-Then statement

I am writing a program in python, which works like this:

  1. Take input string through Serial port, when press enter (Carriage Return)
  2. Check $ sign on the first character of input string then continue if present
  3. Remove all the unwanted character and alphabetics except numerics and , and then print it.

Problem

Program stuck, if $ sign is not present in input string because next time if It get input string with $, it doesn't print the values

kindly review my codes below and guide me on how do I resolve it?

CODE

import serial,re

x = [0,0,0]
ser = serial.Serial('/dev/ttyAMA0', 9600)
buffer = ''

while True:
    buffer += ser.read(ser.inWaiting())
    if '\n' in buffer:
        if buffer[0] == '$':
            x= re.sub("[^0-9\,]","", buffer)
            x1 = x.rstrip()
            x2= x1.split(",")
            print((x2[0]),(x2[1]),(x2[2]))            
            buffer = ""

Aucun commentaire:

Enregistrer un commentaire