pyserial 使用 None 接收错误数据

pyserial receiving wrong data with the None

当我将字节写入串行时,我得到了我想要的数据,但是有一个 None 出现我的串行没有这个 one.How 我可以删除 "None"? 谢谢!

#encoding = utf-8
import serial

def getValues():
    ser = serial.Serial('COM3', baudrate=9600, timeout=1)
    str = 'ZREADMACS'
    ser.write(bytes(str, encoding='utf8'))
    data = ser.readline()
    print(data)

while 1:
    # data = ser.readline()
    # if data:
    #     print(str(data)[2:63])
    # else:
    #     print(getValues())
    userInput = input('Are you kidding me?')
    if userInput == "1":
        print(getValues())

这样输出,但我不想要"None"。

Are you kidding me?1
b'client:TESTR~~address:R0003~~radiation:01000~~voltage:21000~~current:21000~~temprature:01000~~Li_power:02000~~time_delay:00010~~acs_712_zero:00000~~'
None
Are you kidding me?1
b'client:TESTR~~address:R0003~~radiation:01000~~voltage:21000~~current:21000~~temprature:01000~~Li_power:02000~~time_delay:00010~~acs_712_zero:00000~~'
None

在您的代码中,您调用了 print 两次。

第一次使用 data 调用它时,data 被分配给 readline 结果。

第二次,直接在getValues()结果上调用。

这里的问题是 getValues() returns 没什么所以 python 将它翻译成 None 然后你打印它。

去掉最后一个print就好了。

您还可以将 return data 添加到 getValues() 函数中。