在某些情况下,python中未检测到回车

在某些情况下,python中未检测到回车,python,python-2.7,serial-port,carriage-return,Python,Python 2.7,Serial Port,Carriage Return,在下面的代码中,我从串行输入中获取字符,当检测到回车时,它将保存值并覆盖行变量。问题在于,当触发错误时,有时会将两行相加,就好像不存在回车符一样 串行输出似乎良好,回车符出现在预期位置 line = "" while True: data = self.ser.read() if(data == "\r"): print line if line == "check p

在下面的代码中,我从串行输入中获取字符,当检测到回车时,它将保存值并覆盖行变量。问题在于,当触发错误时,有时会将两行相加,就好像不存在回车符一样

串行输出似乎良好,回车符出现在预期位置

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
传感器出现问题时输出代码段:

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

正如你所看到的,这些线连在一起。我的代码中出现这种情况的原因是什么?

您没有在
if
案例中设置
line=”“

if line == "check probe":
    print "CHECK PROBE IF TRIGGERED."
    line = ""