"AT+CGPSINFO" 命令适用于 Socat 但不适用于 PySerial

"AT+CGPSINFO" command works with Socat but not with PySerial

以下Socat命令returnsGPS数据,根据需要:

echo 'AT+CGPSINFO' | socat - /dev/ttyUSB3,crnl

+CGPSINFO: 5100.505778,N,11404.437214,W,031120,175538.0,1076.7,0.0,348.0

OK

现在我想在 Python 中使用 PySerial 做同样的事情,但是我遇到了错误。

from serial import Serial
import time

def sleep_for_x_seconds(sleep_secs=0.25):
    print(f"Sleeping for {sleep_secs} seconds...")
    time.sleep(sleep_secs)

def write_to_modem(modem, to_write, sleep=True, sleep_secs=0.25, readlines=True):
    print(f"Writing {to_write}...")
    modem.write(to_write)
    if sleep:
        sleep_for_x_seconds(sleep_secs)
    if readlines:
        print(modem.readlines())

sleep_secs = 0.25
port = "/dev/ttyUSB3"
print(f"Initializing modem on port '{port}'...")
modem = Serial()
modem.port = port
modem.timeout = 1  # How long to wait when reading lines
modem.writeTimeout = 1
sleep_for_x_seconds(sleep_secs)

print("Opening modem and flushing input and output...")
modem.open()
modem.flushInput()
modem.flushOutput()
sleep_for_x_seconds(sleep_secs)

# This first command returns [b'\r\n', b'OK\r\n']
write_to_modem(modem, b"AT\r\n", sleep=True, sleep_secs=sleep_secs, readlines=True)

# This second command returns [b'\r\n', b'ERROR\r\n'] for some reason... why?
write_to_modem(modem, b"AT^CGPSINFO=1\r\n", sleep=True, sleep_secs=sleep_secs, readlines=True)

modem.flushInput()
modem.close()

这是运行上述程序的完整输出日志(为便于阅读而添加了换行符):

Initializing modem on port '/dev/ttyUSB3'...
Sleeping for 0.25 seconds...

Opening modem and flushing input and output...
Sleeping for 0.25 seconds...

Writing b'AT\r\n'...
Sleeping for 0.25 seconds...
[b'\r\n', b'OK\r\n']

Writing b'AT^CGPSINFO=1\r\n'...
Sleeping for 0.25 seconds...
[b'\r\n', b'ERROR\r\n']

为什么我在 运行 PySerial 时收到错误“[b'\r\n', b'ERROR\r\n']”,当 运行 以下命令时在 Socat 中工作正常吗?

echo 'AT+CGPSINFO' | socat - /dev/ttyUSB3,crnl

+CGPSINFO: 5100.505778,N,11404.437214,W,031120,175538.0,1076.7,0.0,348.0

OK

正如@meuh 在评论中显示的那样,我使用的是 ^ 而不是 +,因此将其更改为 b"AT+CGPSINFO=1\r\n" 解决了问题...