I am writing a program in python, which works like this:
- Take input string through Serial port, when press enter (Carriage Return)
- Check
$sign on the first character of input string then continue if present - Remove all the unwanted character and alphabetics except
numericsand,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 = ""
$the first time, it never will.$. Maybe you should break out of the loop?