[英]If statement recognising comparison as false when it is quite obviously true
我目前正在使用串行強制一些錯誤來測試我是否正確閱讀它們。
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
正如@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"]
回車在我的控制台上是不可見的,所以我不知道它在那里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.