pySerial切断文本文件 [英] pySerial cuts off text in file

查看:402
本文介绍了pySerial切断文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过串口上打印出我的Andr​​oid文件的内容,它只是切断它的一半。

I'm trying to print out the content of a file on my Android through the serial port and it just cut off half of it.

我的code是这样的:

My code look like this:

ser = Serial('/dev/ttyUSB0', 115200, timeout=0)
ser.write(' cat /sdcard/dump.xml \r\n')
sleep(5)
while ser.inWaiting():
    print ser.readline()
ser.close()

猫作品,未经我的串口终端内的任何问题,所以必须有一些设置与串行类。是否有一些MAXLIMIT?我试图与它周围的变量有点玩,但似乎无法找到任何工作。

Cat works without any problems inside my serial port terminal so must be some setting with the Serial class. Does it have some maxlimit? I have tried to play around a bit with it's variables but can't seem to find anything that work.

推荐答案

麻烦似乎是 inWaiting()终止返回false,导致读数停止。也就是说,如果你循环速度不够快,不会有在等待的任何数据(因为你刚刚读它和循环)。

The trouble seems to be that inWaiting() is returning false, causing the reading to stop. That is, if you loop fast enough there won't be any data "in waiting" (because you've just read it and looped).

另外请注意,的ReadLine()无超时(= 0),将阻塞,直到它看到一个换行符,这意味着你可以停留在循环。

Also note that readline() with no timeout (=0) will block until it sees a newline, meaning you can get stuck in the loop.

更好地增加超时例如一秒钟,做了相当大的read()追加到一个字符串(或清单)。退出阅读,当你用尽了缓冲区,任何你想要的结果做。也就是说,如果没有办法知道,当你已经收到了一切办法。

Better to increase the timeout to e.g. one second and do a sizable read() appending to a string (or list). Quit reading when you've exhausted the buffer and do whatever you want with the result. That is, if there is no way of knowing when you've received everything.

ser = Serial('/dev/ttyUSB0', 115200, timeout=1) # NB! One second timeout here!
ser.flushInput() # Might be a good idea?
ser.flushOutput()
ser.write(' cat /sdcard/dump.xml \r\n')
sleep(5) # I really don't think you need to sleep here.

rx_buf = [ser.read(1024)] # Try reading a large chunk. It will read up to 1024 bytes, or timeout and continue.
while True: # Loop to read remaining data, to the end of the rx buffer.
    pending = ser.inWaiting()
    if pending:
        rx_buf.append(ser.read(pending)) # Read pending data, appending to the list.
        # You'll read pending number of bytes, or until the timeout.
    else:
        break

rx_data = ''.join(rx_buf) # Make a string of your buffered chunks.

免责声明:我没有一个Python间preTER可用的串行端口,所以这是我的头顶部

Disclaimer: I haven't got a Python interpreter with serial port available, so this is off the top of my head.

这篇关于pySerial切断文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆