【转】python调用短信收发设备使用SMS.DLL二次开发包

2015-6-8 20:09| 发布者: admin| 查看: 3171| 评论: 0|原作者: admin

摘要: 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 ...
python代码

  1. #-*- encoding: gbk -*-
  2. import sys
  3. import locale
  4. import string
  5. import mysql.connector
  6. import traceback
  7. import datetime
  8. from mysql.connector import errorcode
  9. from ctypes import *
  10. import time

  11. reload(sys);
  12. sys.setdefaultencoding('gbk');

  13. def getMailIndex():
  14.     file = open('messageindex.txt',"r");
  15.     lines = file.readlines();
  16.     file.close();
  17.     return time.strptime(lines[0], "%y-%m-%d %H:%M:%S");
  18. #   

  19. def setMailIndex(timestr):
  20.     f = open('messageindex.txt', 'w');
  21.     f.write(timestr);
  22.     f.close();
  23. #   


  24. #是否有新邮件
  25. global hasNewMail;
  26. hasNewMail=True;
  27. #全局已读的邮件数量
  28. global globalMailReaded;
  29. globalMailReaded=getMailIndex();
  30. #全局dll api
  31. global api;
  32. api = WinDLL('sms.dll');
  33. array_type = c_char_p * 1
  34. strType = array_type();
  35. strCopyRight=array_type();
  36. if (0==api.Sms_Connection("//上海迅赛信息技术有限公司,网址www.xunsai.com//",3,9600,strType,strCopyRight)):
  37.     print('connect to modem fail!');
  38.     exit(0);
  39. #end if
  40. print "connect to %s"%strType[0];
  41. print strCopyRight[0];
  42. #获取新短信
  43. def getNewMessage():
  44.     try:
  45.         global hasNewMail;
  46.         global globalMailReaded;
  47.         global api;
  48.         messagelist=[];
  49.         if(hasNewMail):
  50.             print('read messageindex.txt')
  51.             globalMailReaded=getMailIndex();
  52.         #end if
  53.         #如果来了新短信就接收
  54.         array_type = c_char_p * 1;
  55.         ReceiveSmsStr = array_type();
  56.         result=api.Sms_Receive("4", ReceiveSmsStr);
  57.         strList=ReceiveSmsStr[0].split('|');
  58.         #最新的一个信息
  59.         message=strList[len(strList)-1];
  60.         myfields=message.split('#');
  61.         Sms_TelNum=myfields[2];
  62.         Sms_Text=myfields[3];
  63.         Sms_Time=time.strptime(myfields[4], "%y-%m-%d %H:%M:%S");
  64.         if(Sms_Time<=globalMailReaded):
  65.             hasNewMail=False;
  66.             return None;#没新短信,直接返回
  67.         #end if
  68.         print('get new message');
  69.         messagelist.append((Sms_TelNum,Sms_Text));
  70.         setMailIndex(myfields[4]);
  71.         hasNewMail=True;
  72.         return messagelist;
  73.     except:
  74.         print(traceback.format_exc());
  75.         print("search modem fail,try again");
  76.         return None;
  77.     finally:
  78.         pass;
  79.     #end try
  80. #end def

  81. #只要收到短信,就把这个事件记录在事件库中
  82. #现在就是循环查询未看短信,如果有新短信就读取,并查询关键词库
  83. while(True):
  84.     messagelist=getNewMessage();
  85.     if(messagelist==None):
  86.         time.sleep(5);
  87.         continue;
  88.     #end if
  89.     for messagetuple in messagelist:
  90.         article=messagetuple[1];
  91.         origin=messagetuple[0];
  92.   
  93.     #end for
  94. #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

路过

雷人

握手

鲜花

鸡蛋

相关阅读

Archiver|小黑屋|未来时代科技 ( 粤ICP备12044031号-1

GMT+8, 2024-4-25 16:49 , Processed in 0.064271 second(s), 35 queries .

Powered by WLSD X3.1

© 2013-2014 WLSD Inc.

返回顶部
 
【电话】(15118131494)
【QQ】 未来时代科技01 售前咨询
【QQ】 未来时代科技02 售后技术
【旺旺】 请问有什么可以帮到您?不在线可留言.
【邮箱】
inextera@sina.com
【地址】 (深圳市龙岗坂田扬马小区)