Losing data in received serial string

♀尐吖头ヾ 提交于 2019-12-01 09:14:59

Glad my comment helped!

Set timeout to a low number, e.g. 1 second. Then try something like this. It tries to read a large chunk, but times out quickly and doesn't block for a long time. Whatever has been read is put into a list (rx_buf). Then loop forever, as long as you've got pending bytes to read. The real problem is to 'know' when not to expect any more data.

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:
         break

rx_data = ''.join(rx_buf) # Join the chunks, to get a string of serial data.

The reason I'm putting the chunks in a list is that the join operation is much more efficient than '+=' on strings.

mfitzp

According to this question you need to read the data from the in buffer in chunks (here single byte):

out = ''
# Let's wait one second before reading output (let's give device time to answer).
time.sleep(1)
while ser.inWaiting() > 0:
    out += ser.read(1)

I suspect what is happening in your case is that you're getting an entire 'buffers' full of data, which depending on the state of the buffer may vary.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!