Python 使用 pySerial 读取响应 AT 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23532038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:05:48  来源:igfitidea点击:

Read response AT command with pySerial

pythonpyserial

提问by Renato

I have the following sample code:

我有以下示例代码:

import serial

ser = serial.Serial('/dev/ttyUSB1', 115200, timeout=5)
ser.write("AT\r")
response =  ser.readline()
ser.write(chr(26)) 
ser.close()

print response

My goal is to send the ATcommand and get your answer OK.

我的目标是发送AT命令并得到您的回答OK

The documentation of PySerial readline()says reads the data received until it finds a line break, the problem is that my print is returning nothing.

PySerial readline()的文档说读取接收到的数据,直到找到换行符,问题是我的打印没有返回任何内容。

I'm sure that after the ATcommand, the response that the 3G modem sends me is OK. Anyone know the reason why you can not retrieve the answer?

我确定在AT命令之后,3G 调制解调器发送给我的响应是OK。任何人都知道您无法检索答案的原因?

PS: using programs like CuteCom, I got confirmation that the device works and that it responds to AT commands.

PS:使用像CuteCom这样的程序,我确认设备可以工作并且它响应AT命令。

采纳答案by pah

In order to complement the question comments, please try this and see if anything changes:

为了补充问题评论,请尝试此操作,看看是否有任何变化:

import serial

ser = serial.Serial('/dev/ttyUSB1', 115200, timeout=5)
ser.write("AT\r")
response =  ser.read(2)
print response
ser.close()

If everything works, then add the "\r" to your write() and replace the ser.read(2)with ser.readline()and set the timeout value to zero again.

如果一切正常,然后添加“\ r”把你写()和更换ser.read(2)ser.readline(),并重新设置超时值为零。

回答by Qwerty

Received problem with code:

收到代码问题:

import serial
ser = serial.Serial(port='COM1', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=1, xonoff=False, rtscts=False, dsrdtr=False)
cmd="AT\r"
ser.write(cmd.encode())
msg=ser.read(64)
print(msg)

output is OK :)

输出正常:)

回答by Oleg Kokorin

it's a multiple lines output what the modem returns to your application. will need multiple readline calls to collect all the output (the OK is not the first response line and neither it is second one, and if readline calls are too fast will be lost the tail of the message). consider following code instead (the answer var would be complete modem reply placeholder):

它是多行输出调制解调器返回到您的应用程序的内容。将需要多次 readline 调用来收集所有输出(OK 不是第一个响应行,也不是第二个响应行,如果 readline 调用太快将丢失消息的尾部)。请考虑以下代码(答案变量将是完整的调制解调器回复占位符):

`

`

import serial, time

modem = serial.Serial(port='/dev/ttyHS0', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=1, xonoff=False, rtscts=False, dsrdtr=False)

cmd = "AT\r"

modem.write(cmd.encode())

answer = ""

read_timeout = 0.1

quantity = modem.in_waiting 

while True:

    if quantity > 0:

           answer += modem.read(quantity)
    else:
                # read_timeout is depends on port speed

                # with following formula it works:

                # 0.1 sec + 1.0 sec / baud rate (bits per second) * 10.0 bits (per character) * 10.0 times

                # example for 115200 baud rate:

                # 0.1 + 1.0 / 115200 * 10.0 * 10.0 ~ 0.1 sec

           time.sleep(read_timeout) 

    quantity = modem.in_waiting

    if quantity == 0:

           break

`

`