Part of the code:
#打开串口
ser=serial.Serial('COM3', 9600)
#开启一个线程
th=threading.Thread(target=thread_read, args=(ser, msg_parsed))
th.start()
def thread_read(ser, callback=None):
buf=b' '
while running.is_set():
buf=read_data(ser, buf,callback=None)
def read_data(ser, buf, callback=None):
if callback is None:
callback=print
buf += ser.read(ser.inwaiting())
Why do I call inwaiting() in the read_data() method, and an error is reported, telling me that it has no attribute 'inwaiting()'. It is normal to write ser.inwaiting() outside the method. I have not passed the reference of the serial object. Is it in the method? The book master answers!
你别传对象,用全局变量试试
print(dir(ser))
打印ser的所有方法,找到inWaiting
和in_waiting
,没有inwaiting,所以报错。你确定在外面调用的是inwaiting吗?