我在项目中使用pyserial访问GSM模块,但是需要更多时间将AT
命令发送到GSM模块并通过pyserial获取输出。当我使用PuTTY执行相同的任务时,它会更快。
myport=serial.Serial(port=PORT,baudrate=9600,timeout=0.3,xonxoff=False,rtscts=False,
bytesize=serial.EIGHTBITS,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE)
我也尝试设置timeout
,但仍需要比PuTTY更多的时间。有谁知道如何减少运行AT命令和获取输出的时间?
请帮帮我。
def check(PORT):
myport=serial.Serial(port=PORT,baudrate=9600,timeout=0.3,xonxoff=False,rtscts=False,
bytesize=serial.EIGHTBITS,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE)
time.sleep(0.2)
temp=myport.readall()
myport.write("ate0\r")
time.sleep(0.2)
temp=myport.readall()
myport.write("at\r")
time.sleep(0.2)
data=myport.readall()
myport.close()
return (data.strip()) #if gives "OK" then GSM is working otherwise there will be some error
def send(PORT,num,msg):
try:
myport=serial.Serial(port=PORT,baudrate=9600,timeout=0.3,xonxoff=False,rtscts=False,
bytesize=serial.EIGHTBITS,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE)
time.sleep(0.2)
temp=myport.readall()
myport.write("at+cmgf=1\r")
time.sleep(0.2)
data=myport.readall()
if(data.strip()=="OK"):
temp=myport.readall()
myport.write("at+cmgs=\""+str(num)+"\"\r")
time.sleep(0.2)
data=myport.readall()
if(data.strip()==">"):
temp=myport.readall()
myport.write(str(msg)+"\r")
time.sleep(0.1)
myport.write(chr(26))
time.sleep(0.2)
data=myport.readall()
try:
a=data.split('\r\nOK')
result="OK"
except:
result="ERROR"
myport.close()
return result
except:
myport.close()
return "GSM is not giving proper response... contact your software provider"