gpt4 book ai didi

Python 通过 AT 读取短信

转载 作者:行者123 更新时间:2023-12-05 07:49:43 27 4
gpt4 key购买 nike

我的脚本已经完成了 99%,但遇到了问题

我正在尝试在 python 中显示从 GSM 调制解调器接收到的文本消息。

到目前为止,我可以看到收到了一条文本,但无法显示该消息。

我知道 self.ser.readlines() 作为列表返回

import serial
import time
class TextMessage:
def connectPhone(self):
self.ser = serial.Serial('COM14', 460800, timeout=5) #for mine this was ttyUSB0 but could be ttyUSB1 etc. good idea to runs ls usb and find out that way
time.sleep(1)

def read(self):
self.ser.write('ATZ\r')
time.sleep(1)
self.ser.write('AT+CMGF=1\r')# put in textmode
time.sleep(1)
self.ser.write('''AT+CMGL="ALL"''' + '''\r''') #fetch all sms's
read = self.ser.readlines()
for msg in read:
if "+CMGL" in msg: #+CMGL looks for all SMS messages
print msg

def disconnectPhone(self):
self.ser.close()

sms = TextMessage()
sms.connectPhone()
sms.read()
sms.disconnectPhone()
raw_input("Press anykey to exit")

这里是输出

AT+CMGL="ALL"
+CMGL: 0,"REC READ","+61xxxxxxxxx",,"16/04/30,19:53:38+38"

+CMGL: 1,"REC READ","+61xxxxxxxxx",,"16/05/02,14:47:53+38"

+CMGL: 2,"REC READ","+61xxxxxxxxx",,"16/05/02,21:27:58+38"

我想要的输出是

+CMGL: 0,"REC READ","+61xxxxxxxxx",,"16/04/30,19:53:38+38"

Test back

+CMGL: 1,"REC READ","+61xxxxxxxxx",,"16/05/02,14:47:53+38"

Im a message

+CMGL: 2,"REC READ","+61xxxxxxxxx",,"16/05/02,21:27:58+38"

TEST TEST TEST

有人可以帮忙吗?

输出if removed

ATZ

OK

AT+CMGF=1

Ok


AT+GCML="ALL"

+CMGL: 0,"REC READ","+61xxxxxxxxx",,"16/04/30,19:53:38+38"
Test back
+CMGL: 1,"REC READ","+61xxxxxxxxx",,"16/05/02,14:47:53+38"
Im a message
+CMGL: 2,"REC READ","+61xxxxxxxxx",,"16/05/02,21:27:58+38"
TEST TEST TEST


OK

最佳答案

希望对你有帮助

#!/usr/bin/env python2.7

import serial
import time
class TextMessage:
def connectPhone(self):
self.ser = serial.Serial('/dev/ttyUSB2', 9600, timeout=5) #for mine this was ttyUSB0 but could be ttyUSB1 etc. good idea to runs ls usb and find out that way
time.sleep(1)

def read(self):
self.ser.write('ATZ\r')
time.sleep(1)
self.ser.write('AT+CMGF=1\r')# put in textmode
time.sleep(1)
self.ser.write('''AT+CMGL="ALL"''' + '''\r''') #fetch all sms's
read = self.ser.readlines()
i=0
for msg in read:
if "+CMGL" in msg: #+CMGL looks for all SMS messages
bvb = 'AT+CMGR={}'.format(str(i)) + '\r'
self.ser.write(bvb) #fetch all sms's
ead = self.ser.readlines()
for x in ead:
print (x)
print (i)
i=i+1

def disconnectPhone(self):
self.ser.close()

sms = TextMessage()
sms.connectPhone()
sms.read()
sms.disconnectPhone()
raw_input("Press anykey to exit")

关于Python 通过 AT 读取短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36984118/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com