我的pyserial有问题

首先,建立连接:

ser = serial.Serial(
device,
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)


如果按一个键,将显示一个菜单(我看不到此菜单,但是我知道是否按“ 1”来获取数据转储):

0=Cfg
1=Dump
2=Erase
3=Cal
In->


在我的代码中按“ 1”:

cmd = "1"
ser.write(cmd.encode('ascii'))


因此,当我按“ 1”时,串行结果是一个很长的数字列表,如下所示:

816 81e 81e
828 820 820
816 816 81a
82a 826 824
816 80e 81a
81e 824 820
820 81a 80e
816 80e 81e
82a 81c 824
... and many other


我尝试在60秒后使用以下命令读取输出:
(60秒大约是滚动所有行的时间)

time.sleep(60)
dumpfile.write(str(ser.read_all()))


但是在转储文件中,我只有几行。
如何读取串行的总输出?

最佳答案

一整夜后,我找到了解决方案!

while 1:
    serial_line = ser.readline()
    dumpfile.write(str(serial_line))
    print serial_line #for debug
    if len(serial_line) == 0:
      break

关于python - 使用pyserial从串行读取输出。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36462508/

10-10 16:19