【转】python调用短信设备使用SMS.DLL二次开发包
分享python代码:
#-*- encoding: gbk -*- import sys import locale import string import mysql.connector import traceback import datetime from mysql.connector import errorcode from ctypes import * import time reload(sys); sys.setdefaultencoding('gbk'); 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(); # #是否有新邮件 global hasNewMail; hasNewMail=True; #全局已读的邮件数量 global globalMailReaded; globalMailReaded=getMailIndex(); #全局dll api global api; api = WinDLL('sms.dll'); array_type = c_char_p * 1 strType = array_type(); strCopyRight=array_type(); if (0==api.Sms_Connection("//上海迅赛信息技术有限公司,网址[url]www.xunsai.com//[/url]",3,9600,strType,strCopyRight)): print('connect to modem fail!'); exit(0); #end if print "connect to %s"%strType[0]; print strCopyRight[0]; #获取新短信 def getNewMessage(): try: global hasNewMail; global globalMailReaded; global api; messagelist=[]; if(hasNewMail): print('read messageindex.txt') globalMailReaded=getMailIndex(); #end if #如果来了新短信就接收 array_type = c_char_p * 1; ReceiveSmsStr = array_type(); result=api.Sms_Receive("4", ReceiveSmsStr); strList=ReceiveSmsStr[0].split('|'); #最新的一个信息 message=strList[len(strList)-1]; myfields=message.split('#'); Sms_TelNum=myfields[2]; Sms_Text=myfields[3]; Sms_Time=time.strptime(myfields[4], "%y-%m-%d %H:%M:%S"); if(Sms_Time<=globalMailReaded): hasNewMail=False; return None;#没新短信,直接返回 #end if print('get new message'); messagelist.append((Sms_TelNum,Sms_Text)); setMailIndex(myfields[4]); hasNewMail=True; return messagelist; except: print(traceback.format_exc()); print("search modem fail,try again"); return None; finally: pass; #end try #end def #只要收到短信,就把这个事件记录在事件库中 #现在就是循环查询未看短信,如果有新短信就读取,并查询关键词库 while(True): messagelist=getNewMessage(); if(messagelist==None): time.sleep(5); continue; #end if for messagetuple in messagelist: article=messagetuple[1]; origin=messagetuple[0]; #end for #end while
下面是我在开发过程中用到的短信设备的AT指令:
AT+CPMS?返回
+CPMS: "ME",1,25,"SM",1,50,"SM",1,50
AT+CNMI?返回
+CNMI: 1,1,0,2,1(我重新设置了,我这里的设置是把收到的短信存储在短息猫设备中,不存储在SIM中,此前短信设备把短信存在SIM卡里了,上面蓝色内容的就是sim卡中存储了1条短信,sim卡读写次数有限,很脆弱,所以我把短信存在ME中了,如果短信满了,就删除。我想工业级的短信设备ME应该经得起反复折腾。)
AT+CMGL=4返回
+CMGL: 1,1,,28
0891683108200005F0040D91683166607056F700085140011205652308671F8D2700330031
再向短信设备发短信:
AT+CPMS?返回
+CPMS: "ME",2,25,"SM",1,50,"SM",1,50
此时说明ME中有两条短信了,原先是1条
AT+CNMI?返回
+CNMI: 1,1,0,2,1
AT+CMGL=4返回
+CMGL: 1,1,,28
0891683108200005F0040D91683166607056F700085140011205652308671F8D2700330031
+CMGL: 2,1,,24
0891683108200005F0040D91683166607056F7000851400112754323045F3A7A3B
这就说明ME中确实存储了两条短信
我们再删除一条短信试试:
AT+CPMS?返回
+CPMS: "ME",1,25,"SM",1,50,"SM",1,50
这里确实少了一条短信
AT+CNMI?返回
+CNMI: 1,1,0,2,1
AT+CMGL=4返回
+CMGL: 1,1,,28
0891683108200005F0040D91683166607056F700085140011205652308671F8D2700330031
这里少了一条短信,说明这条短信确实是被删除了
本文来自csdn“liumengcheng”:http://blog.csdn.net/liumengcheng/article/details/44961051
回帖 ( 0 )