Skip to content
Advertisement

If statement recognising comparison as false when it is quite obviously true

I am currently forcing some errors using serial to test I am reading them correctly.

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"]

I am receiving at least one error message each time that is within errorHandling. But for some reason, the if statement isn’t recognising the error code as being in the errorHandling list. What am I missing?

printing to console shows me this

*U    <---This is serDecode
['*F', '*N', '*I', '*U', '*L'] <---- errorHandling list
False  <---printing serDecode in errorHandling

Advertisement

Answer

As stated by @MByD I needed to strip(‘r’) from my received readline().

so the code now looks like this:

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"]

The carriage return was invisible on my console, so I wasn’t aware it was there.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement