Question

In the code below I am getting characters from serial input and when a carriage return is detected it will save the value and overwrite the line variable. The issue is that when an error is triggered 2 lines sometimes are added together as if no carriage return is present.

The serial output seems fine with carriage return present where expected.

line = ""
        while True:
            data = self.ser.read()
            if(data == "\r"):
                print line
                if line == "check probe":
                    print "CHECK PROBE IF TRIGGERED."
                else:
                    # save line value to a different variable here.
                    print "VALID VALUE ELSE TRIGGERED."
                    line = ""
            else:
                line += data

Output snippet when there is an issue with the sensor:

CHECK PROBE IF TRIGGERED.
check probecheck probe
VALID VALUE ELSE TRIGGERED.
check probe
CHECK PROBE IF TRIGGERED.
check probe7.00
VALID VALUE ELSE TRIGGERED.
7.20

As you can see the lines run together. What is causing this in my code?

Was it helpful?

Solution

You are not setting line = "" in the if case.

if line == "check probe":
    print "CHECK PROBE IF TRIGGERED."
    line = ""
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top