Python 当比较非常明显为真时,识别为假的If语句
我目前正在使用串行命令强制执行一些错误,以测试我是否正确读取它们Python 当比较非常明显为真时,识别为假的If语句,python,python-3.x,serialization,arduino,Python,Python 3.x,Serialization,Arduino,我目前正在使用串行命令强制执行一些错误,以测试我是否正确读取它们 def errorCheck(): while(1): if(ser.in_waiting >= 0): serString = ser.readline() decoding = serString.decode('Ascii') serDecode = str(decoding) if(serDecode in errorHandlin
def errorCheck():
while(1):
if(ser.in_waiting >= 0):
serString = ser.readline()
decoding = serString.decode('Ascii')
serDecode = str(decoding)
if(serDecode in errorHandling):
ErrMsg(serDecode)
return
break
errorHandling = ["*F", "*N", "*I", "*U", "*L"]
每次在errorHandling中,我都会收到至少一条错误消息。但是由于某些原因,if语句没有将错误代码识别为在errorHandling列表中。我错过了什么
打印到控制台显示了这一点
*U <---This is serDecode
['*F', '*N', '*I', '*U', '*L'] <---- errorHandling list
False <---printing serDecode in errorHandling
*U如@MByD所述,我需要从收到的readline()中删除('\r')
因此,代码现在如下所示:
def errorCheck():
while(1):
if(ser.in_waiting >= 0):
serString = ser.readline()
serDecode = serString.decode('Ascii').strip('\r')
if(serDecode in errorHandling):
ErrMsg(serDecode)
return
break
errorHandling = ["*F", "*N", "*I", "*U", "*L"]
我的控制台上看不到回车,所以我不知道它在那里。请提供一个完整的示例。应该有足够的代码让其他人可以按原样运行并遇到问题。readline()通常返回带换行符的字符串,请确保您没有读到“*U\n”@MByD您是对的,谢谢。我的控制台上没有显示回车,所以我错过了。再次感谢!