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!
Don’t pass objects, try using global variables
print(dir(ser))
All methods of printing ser, findinWaiting
andin_waiting
, there is no inwaiting, so an error is reported.Are you sure you are calling inwaiting outside?