Pyserial AT命令问题

2024-09-30 20:24:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过pyserial发送一个简单的命令,但是我发现当我在pyserial.write中发送""符号时,它会变成另一种格式。你知道吗

我可以知道如何使用pyserial发送""符号吗?你知道吗

import serial
import time
import sys


class SIMComModem(object):

def __init__(self):
    self.open()

def open(self):
    self.ser = serial.Serial('COM9', 115200, timeout=5)
    print("Send AT OK command \n ")
    self.SendCommand('AT\r')
    time.sleep(0.5)


def SendCommand(self, command, getline=True):
    self.ser.flushInput()
    self.ser.flushOutput()
    print(command.encode())
    self.ser.write(command.encode())
    data = ''
    if getline:
        data = self.ReadLine()
    return data

def ReadLine(self):
    data = self.ser.readline()
    data = self.ser.readline()
    print(data)
    return data

def SetGPS(self):
    pass

def GetGpsOne(self):
    print("Set APN")
    self.SendCommand('AT+CGSOCKCONT=1,”IP”,”mobile”\r')
    time.sleep(0.5)
    print("Donwnload GpsOneData")
    self.SendCommand('AT+CGPSXD=0\r')
    time.sleep(0.5)
    self.SendCommand('AT+CHTPSERV=\\”ADD\\”,\\”www.google.com\\”,80,1\r')
    time.sleep(0.5)
    print("Update Time zone")
    self.SendCommand('AT+CTZU=1\r')
    time.sleep(0.5)
    self.SendCommand('AT+CCLK?\r')
    time.sleep(0.5)

我希望当我调用GetGpsOne函数时,它会将AT+CGSOCKCONT=1,"IP","mobile"发送到COM端口,但结果如下:


b'AT\r'
b'OK\r\n'
Set APN
b'AT+CGSOCKCONT=1,\xe2\x80\x9dIP\xe2\x80\x9d,\xe2\x80\x9dmobile\xe2\x80\x9d\r'

Tags: importselfdatatimedefsleepcommandat
1条回答
网友
1楼 · 发布于 2024-09-30 20:24:28

您需要的是"straight" quotes,而不是当前使用的”typographical” quotes。你知道吗

试试看

print("Set APN")
self.SendCommand('AT+CGSOCKCONT=1,"IP","mobile"\r')
time.sleep(0.5)
print("Donwnload GpsOneData")
self.SendCommand('AT+CGPSXD=0\r')
time.sleep(0.5)
self.SendCommand('AT+CHTPSERV="ADD","www.google.com",80,1\r')
time.sleep(0.5)
print("Update Time zone")
self.SendCommand('AT+CTZU=1\r')
time.sleep(0.5)
self.SendCommand('AT+CCLK?\r')
time.sleep(0.5)

相反。你知道吗

为了澄清,以下是您的排版引用的错误之处:

>>> ord('”')  # <  the quote used by the original code
8221
>>> '”'.encode('utf-8')
b'\xe2\x80\x9d'  # <  the UTF-8 encoding you saw



>>> ord('"')  # <  the correct ASCII quote
34
>>> '"'.encode('utf-8')  # <  its UTF-8 encoding
b'\x22'

相关问题 更多 >