简体   繁体   中英

Read output from serial with pyserial.

I have a problem with pyserial

First, establish a connection:

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

If I press a key a menu appear (I can't see this menu but I know if I press '1' obtain a dump of data):

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

In my code press '1':

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

So, when I press '1' the serial result is a very long list of number like this:

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

I try to read the output with this command after 60 seconds: (60 seconds is about a time to scroll all row)

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

But in dumpfile I have only few rows. How I can read total output of serial?

After a long night I found the solution!!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM