意在解决自动化机器人,接受短信验证码场景。
1.规避手机app读取短信方案,安全性,手机占用,系统兼容问题;
2.长时间,高稳定,低成本选择方案。
某宝上买4G LTE硬件模块(声称不支持中文短信接收,实际是没有解码),PC通过串口和模块连接,模拟at控制进行短信收发。
import serial
import serial.tools.list_ports
import binascii
class DTUSMS:
def __init__(self, serialName=None, port=9600, **args):
if serialName == None:
plist = list(serial.tools.list_ports.comports())
if len(plist) > 0:
plist_0 =list(plist[0])
serialName = plist_0[0]
if "timeout" not in args:
args["timeout"] = 2
if "parity" not in args:
args["parity"] = serial.PARITY_NONE
if "stopbits" not in args:
args["stopbits"] = 1
if serialName == None:
self.ser = None
else:
# uart_fd = serial.Serial(serialName, int(9600), timeout=2, parity=serial.PARITY_NONE, stopbits=1)
self.ser = serial.Serial(serialName, port, **args)
def open(self):
if not self.ser.isOpen():
self.ser.open()
def write(self, msg):
# 短信接收模式
self.ser.write(msg.encode())
def readLines(self):
msgs = self.ser.readlines()
msgs = [i.decode("utf-8") for i in msgs]
return msgs
def decodeMsg(self, msg):
return binascii.unhexlify(msg).decode("utf-16be")
def close(self):
if self.ser.isOpen():
self.ser.close()
def open(serialName=None,port=9600,args={}):
return DTUSMS(serialName,port,**args)
def write(sms,msg):
sms.write(msg)
def read(sms):
return sms.readLines()
def decodeMsg(msg):
return binascii.unhexlify(msg).decode("utf-16be")
def close(sms):
sms.close()
if __name__ == "__main__":
sms = open()
print(1)
write(sms,'AT*SMSOUT=2#\r')
print(2)
msg=read(sms)
print(msg)
while True:
msg=read(sms)
print(msg)
if len(msg)>0 and msg[0][0:5] == "+SMS:":
msg=msg[0].split(",")[3]
msg=sms.decodeMsg(msg)
print(msg)
break
close(sms)
效果:

网友评论