Pyserial AT命令问题

时间:2019-05-28 10:40:26

标签: python pyserial python-unicode

我想通过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函数时,它将向COM端口发送AT+CGSOCKCONT=1,"IP","mobile",但是结果如下:


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'

1 个答案:

答案 0 :(得分:0)

您将需要"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'