简体   繁体   中英

How to read the response from serial port

I'm new with the python language

I have a uart bluetooth dongle, I wrote this code below and the write method works fine because I can see the response using the gtkterm software

Code:

import serial

ser = serial.Serial()
ser.baudrate = 115200
ser.port = '/dev/ttyUSB0'

ser.open()
print(ser.is_open)

ser.write(b'info\r\n')  # get info command 
ser.write(b'scan=00\r\n')  # start scan command

The response displayed in gtkterm software:

Device information
firmware: nrf_dongle
firmware_version: 0.2.5-ba519b3
firmware_build: 20180413-104249
device_name: amine
serial_number: a58f2080352ac55bd1850576df54
mac_address: d1850576df54
device_state: 1
adv_state: 0
scan_state: 0
END

@scan:d1850576df54,20fabb03c064,-71,2,30,0201041aff4c00021570996ffaa2c34f00b776a3852c4bbd790cb90006c2
@scan:d1850576df54,20fabb044b2c,-62,2,30,0201041aff4c000215023f3d601143013582ba2e1e1603bcb9ffff02e5c5
@scan:d1850576df54,20fabb044b51,-54,3,30,0201041aff4c000215023f3d601143013582ba2e1e1603bcb9ffff02c8c5
@scan:d1850576df54,20fabb044b2c,-62,2,30,0201041aff4c000215023f3d601143013582ba2e1e1603bcb9ffff02e5c5
.
.
.

So my question is how can I read this data using the pyserial module or any other approach ?

There are many approaches to this problem. First of all the question is - do you want to implement every detail yourself as an exercise? If so then you can implement a function that will read from the serial port one byte at the time, like so:

def readline(port):
    message = ""
    byte = ""
    while True:
        byte = port.read()
        if byte == "\n":
            break
        message += byte
    return message

It will stop reading from the port when the newline character is encountered and return the message so far. But be aware that there are some problems here (is the end-of-line character always "\\n" ? What if there is a timeout on the read function?)

Here is the link to the documentation about how the read function behaves. Note, that if the Serial object has not been set with a timeout the function will block, which means that it will wait for the incoming data from the serial port.

The PySerial documentation is a great source of information on the topic - they also provide an example for using the readline function that takes into account problems connected to the newline differences (end-of-line characters). Here is the example from the docs rewritten for your example:

import serial
import io

ser = serial.Serial()
ser.baudrate = 115200
ser.port = '/dev/ttyUSB0'

ser.open()
print(ser.is_open)

sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))

sio.write(b'info\r\n')
sio.flush() # it is buffering. required to get the data out *now*
response = sio.readline()
print(response)

I strongly suggest to look at the miniterm.py module that is supplied with the PySerial module. Although it might be quite hard at first it is in my opinion a good source of learning material to get accustomed with this library.

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