类似于Python 3.5字节的对象是必需的

时间:2015-11-18 16:41:14

标签: python python-3.x pyserial

我有一个使用pyserial的代码部分来获取外部设备的输出。无论我如何解码readline()输出它都会引发错误。我首先测试每个条件,但在while中使用时会出错。

mess = ser.readline().decode().strip("\r\n")
print(mess)
print(mess != "")
print("Off" not in mess)
print(mess is not None)
while mess != "" and "Off" not in mess:
    if "Google" in mess:
        #code

得到我的答复:

On
True
True
True
Traceback (most recent call last):
  File "main.py", line 57, in <module>
    main()
  File "main.py", line 38, in main
    while mess != "" and "Off" not in mess:
TypeError: a bytes-like object is required, not 'str'

不知道如何解决这个问题

1 个答案:

答案 0 :(得分:0)

@tdelaney有一条评论导致了我的解决方案:

  

我的水晶球说你有另一个ser.readline()   循环,但你不解码结果。