前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 串口编程 AT指令控制短信

Python 串口编程 AT指令控制短信

作者头像
py3study
发布2020-01-06 14:30:59
2.7K0
发布2020-01-06 14:30:59
举报
文章被收录于专栏:python3

#-*- encoding: gbk -*- import time import sys import os import locale import string import traceback import datetime import serial reload(sys); sys.setdefaultencoding('gbk'); def mysterious(ust):     s="";     for i in range(len(ust)/4):         us=ust[i*4:i*4+4];         s=s+unichr(int(us,16));     #end for     return s #end  def decodePDU(pdu):     phonelen=int(pdu[20:22],16);     b=pdu[24:25+phonelen];#38     phone="";     for i in range(1,phonelen+1,2):         if(i!=phonelen):             phone=phone+b[i]+b[i-1];         else:             phone=phone+b[i];         #end if     #end for     afterphone=25+phonelen+4;     c=pdu[afterphone:afterphone+12];     mytime= c[1]+c[0]+'-'+c[3]+c[2]+'-'+c[5]+c[4]+' '+c[7]+c[6]+':'+c[9]+c[8]+':'+c[11]+c[10];     #15-04-12 14:32:35     datalength=int(pdu[afterphone+12+2:afterphone+12+4],16)*2;     mydata=mysterious(pdu[afterphone+12+4:afterphone+12+4+datalength]);     return (phone,mydata,mytime); #end def def getMailIndex():     file = open('messageindex.txt',"r");     lines = file.readlines();     file.close();     return time.strptime(lines[0], "%y-%m-%d %H:%M:%S"); #     def setMailIndex(timestr):     f = open('messageindex.txt', 'w');     f.write(timestr);     f.close(); #     def _queryQuick(cu,sql,tuple):     try:         cu.execute(sql,tuple);         rows = []         for row in cu:             rows.append(row)         #         return rows     except:         print(traceback.format_exc())     #end  # #获取信息 def _queryRows(cu,sql):     try:         cu.execute(sql)         rows = []         for row in cu:             rows.append(row)         #         return rows     except:         print(traceback.format_exc())     #end  # def executeMessage(messagelist):     for messagetuple in messagelist:         article=messagetuple[1];         origin=messagetuple[0];         conn.commit();         #判断用户是否是信任列表里的用户         users=_queryQuick(cur,"select username from trusted_users where username=%s",(origin,));         if(len(users)<1):             print('the user %s is not trusted user,ignore the message: %s,continue search the message...'%(origin,article));             time.sleep(5)             continue;         #end if         #start send message'已收到短信');         print 'start send message'         ser.write("AT+CMGS=47\r");         pdus3="";         while(pdus3.find('> ')<0):             mydata3=ser.read(12);             pdus3=pdus3+mydata3;             time.sleep(0.1);         #end while         PDUmsg='0891683108200005F011000D91683166607056F70008AA205DF26536523077ED4FE1FF0C4E2D697757FA91D1611F8C224E0E60A854084F5C';         myphone=origin[1]+origin[0]+origin[3]+origin[2]+origin[5]+origin[4]+origin[7]+origin[6]+origin[9]+origin[8]+origin[11]+origin[10]+'F'+origin[12];         PDUmsgAll=PDUmsg[0:26]+myphone+PDUmsg[40:];         ser.write(PDUmsgAll+"\x1a");         time.sleep(1);         pdus4="";         while(pdus4.find('OK\r\n')<0):             mydata4=ser.read(65536);             pdus4=pdus4+mydata4;             time.sleep(0.1);         #end while         print 'end send message' #end def #全局已读的邮件数量 globalMailReaded=getMailIndex(); hasNewMail=True; ser = serial.Serial('COM3', 9600); print 'get new Message...' while True:     if(hasNewMail):         globalMailReaded=getMailIndex();     #end if     ser.write("AT+CMGL=4\r");     pdus="";     hasMessage=True;     while(pdus.find('\r\nOK\r\n')<=0):         mydata2=ser.read(65536);         pdus=pdus+mydata2;         if(len(pdus)==6 and pdus=='\r\nOK\r\n'):             hasMessage=False;             break;         #end if           time.sleep(1);     #end while     if(hasMessage==False):         hasNewMail=False;         time.sleep(5);         continue;     #end if     while(pdus.find('\r\n\r\nOK\r\n')<0):         mydata=ser.read(65536);         pdus=pdus+mydata;         time.sleep(1);     #end while     strlist=pdus.split('+CMGL:');     messagelist=[];     pduCount=0;     for str in strlist:         index = str.find('0891');         pduStr=str[index-1:].replace("\n", "");         if(len(pduStr)>30):             print pduStr;             pduCount=pduCount+1;             (phone2,mydata2,mytime2)=decodePDU(pduStr);             Sms_Time=time.strptime(mytime2, "%y-%m-%d %H:%M:%S");             if(Sms_Time>globalMailReaded):                 messagelist.append((phone2,mydata2));                 setMailIndex(mytime2);                 hasNewMail=True;             #end if         #end if     #end for     #删除旧短信     print messagelist;     print 'delete old message'     time.sleep(1);     if(pduCount>0):         for j in range(25,0,-1):             ATStr="AT+CMGD=%d\r"%(j);             ser.write(ATStr);             ATReturn="";             while(ATReturn.find('OK')<0):                 mydata=ser.read(65536);                 ATReturn=ATReturn+mydata;                 time.sleep(0.05);             #end while         #end for         print('has deleted all messages');     #end if         if(len(messagelist)>0):         executeMessage(messagelist);     else:         hasNewMail=False;     #end if     time.sleep(1); #end while

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
短信
腾讯云短信(Short Message Service,SMS)可为广大企业级用户提供稳定可靠,安全合规的短信触达服务。用户可快速接入,调用 API / SDK 或者通过控制台即可发送,支持发送验证码、通知类短信和营销短信。国内验证短信秒级触达,99%到达率;国际/港澳台短信覆盖全球200+国家/地区,全球多服务站点,稳定可靠。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档