很高兴我的评论有所帮助!
将超时设置为较低的数字,例如1秒。然后尝试这样的事情。它尝试读取较大的块,但超时很快,并且长时间不阻塞。任何已读取的内容都将放入列表(rx_buf)。然后,只要您有待读取的未读字节,就永远循环。真正的问题是“知道”何时不希望有更多数据。
rx_buf = [ser.read(16384)] # Try reading a large chunk of data, blocking for timeout secs.while True: # Loop to read remaining data, to end of receive buffer. pending = ser.inWaiting() if pending: rx_buf.append(ser.read(pending)) # Append read chunks to the list. else: breakrx_data = ''.join(rx_buf) # Join the chunks, to get a string of serial data.
我将大块放在列表中的原因是,对字符串的联接操作比’+ =’更有效。



