简体   繁体   English

如何从串口读取响应

[英]How to read the response from serial port

I'm new with the python language 我是python语言的新手

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 我有一个uart蓝牙加密狗,我在下面编写了此代码,并且write方法可以正常工作,因为我可以使用gtkterm软件查看响应

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: gtkterm软件中显示的响应:

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 ? 所以我的问题是如何使用pyserial模块或任何其他方法读取此数据?

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?) 但是请注意,这里存在一些问题( end-of-line字符是否始终为"\\n" ?如果read函数超时,该怎么办?)

Here is the link to the documentation about how the read function behaves. 这是有关read函数行为的文档链接 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. 请注意,如果尚未设置超时时间的Serial对象,则该函数将阻塞,这意味着它将等待来自串行端口的传入数据。

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). PySerial文档是有关该主题的重要信息来源-它们还提供了使用readline函数的示例,该示例考虑了与换行符不同(行尾字符)有关的问题。 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. 我强烈建议您查看miniterm.py模块随附的miniterm.py模块。 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. 尽管起初可能很难,但在我看来,习惯该库是学习资料的良好来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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