查看: 12730|回复: 0
打印 上一主题 下一主题

短信收发设备通过C#来实现API函数接口

[复制链接]
跳转到指定楼层
楼主
发表于 2014-3-10 11:33:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
通过上几篇短信收发设备的串口类学习,PDU编码分析和PDU码解码后今日这篇文章是实现一个API接口的;只是简单的实现了,还没有完善的一个程序,只供参考使用,
设备类目前可以完成发送、接收、读取和删除短信,需要其他功能可以自行添加。
本程序使用用前两篇实现PDU编解码类,在此基础上完成短信的发送和读取
PDU编解码参考:《C#实现PUD编码 》《C#实现PDU格式解码程序
下面正式开始解说实现API:

       类名:GSMModem,提供短信收发设备API,完成短信的发送与接收等。直接使用串口类SerialPort,通过实例化了的串口对象sp完成向设备发送AT指令及读取设备返回字符串,从而完成对“猫”的控制及数据交换。

       属性:ComPort、BaudRate、IsOpen。ComPort,串口号,此属性用来对串口sp的PortName访问,为设备所连接的串口号;BaudRate,波特率,此属性用来访问串口实例sp的BaudRate属性,是PC与短信收发设备通信的波特率;IsOpen,是否打开,此属性用来访问串口实例sp的IsOpen属性,指示设备是否打开。

       方法:Open、Close、SendAT、SendMsg、ReadMsgByIndex、DeleteMsgByIndex。Open:设备打开;Close:设备关闭;SendAT:发送AT指令;SendMsg:发送短信;ReadMsgByIndex:按序号读短信;DeleteMsgByIndex:按序号删除短信。
事件:OnRecieved,收到新消息触发此事件,应在事件调用读信息函数。

构造函数:
  1.         public GSMModem()
  2.         {
  3.             sp = new SerialPort();

  4.             sp.ReadTimeout = 2500;      //读超时时间 发送短信时间的需要
  5.             sp.RtsEnable = true;        //必须为true 这样串口才能接收到数据

  6.             sp.ReceivedBytesThreshold = 6;      //引发事件DataReceived前的字符数
  7.             sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
  8.         }
复制代码
  1.         public GSMModem(string comPort, int baudRate)
  2.         {
  3.             sp = new SerialPort();

  4.             sp.PortName = comPort;      //
  5.             sp.BaudRate = baudRate;
  6.             sp.ReadTimeout = 2500;      //读超时时间 发送短信时间的需要
  7.             sp.RtsEnable = true;        //必须为true 这样串口才能接收到数据

  8.             sp.ReceivedBytesThreshold = 6;      //引发事件DataReceived前的字符数
  9.             sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
  10.         }
复制代码
     完成sp的实例化与初始化,值得一提的sp.RtsEnable = true; 我写串口调试器的时候,刚开始没设此属性,总是收不到数据,后来好不容易找到这个问题,具体原因不知道,加上就是了。
       sp.ReadTimeout属性设为2000ms时,发短信时读取超时(发送短信用时比较长),2500可以正常了。最好是3秒3000

      属性:属性代码比较简单,在此只列出一个,其他与这个类似,详见附件。
  1.         public string ComPort
  2.         {
  3.             get
  4.             {
  5.                 return sp.PortName;
  6.             }
  7.             set
  8.             {
  9.                 try
  10.                 {
  11.                     sp.PortName = value;
  12.                 }
  13.                 catch (Exception ex)
  14.                 {
  15.                     throw ex;
  16.                 }
  17.             }
  18.         }
复制代码
     属性代码只提供对类中串口实例的属性访问,比较简单,不多讲解了。

方法:        
      
  • Open,Close 比较简单,仅仅调用的sp的打开与关闭函数
      更新:添加设备初始部分,设置关回显,PDU模式,收到短信提醒
      更新2:添加设备连接识别,解决上个更新后未连接设备或串口号或波特率设置不正确时连接时间太长(30s),而且错误的连接成功问题

  1.         /// <summary>
  2.         /// 设备打开函数,无法时打开将引发异常
  3.         /// </summary>
  4.         public void Open()
  5.         {
  6.             try
  7.             {
  8.                 sp.Open();
  9.             }
  10.             catch (Exception ex)
  11.             {
  12.                 throw ex;
  13.             }

  14.             //初始化设备
  15.             if (sp.IsOpen)
  16.             {
  17.                 sp.DataReceived -= sp_DataReceived;

  18.                 //更新添加连接识别
  19.                 sp.Write("AT\r");
  20.                 Thread.Sleep(50);
  21.                 string s = sp.ReadExisting().Trim();
  22.                 s = s.Substring(s.Length - 2, 2);           //有回显时 去后两位有效字符
  23.                 if (s != "OK")
  24.                 {
  25.                     throw new Exception("硬件连接错误");    //硬件未连接、串口或是波特率设置错误
  26.                 }

  27.                 try
  28.                 {
  29.                     SendAT("ATE0");
  30.                     SendAT("AT+CMGF=0");
  31.                     SendAT("AT+CNMI=2,1");
  32.                 }
  33.                 catch { }
  34.             }
  35.         }
复制代码

  • Close函数和此类似,但仅有关闭函数,不再贴出来了。

  • SendAT,完成AT指令的发送,并返回设备返回的字符串。

  1.         /// <summary>
  2.         /// 发送AT指令 逐条发送AT指令 调用一次发送一条指令
  3.         /// 能返回一个OK或ERROR算一条指令
  4.         /// </summary>
  5.         /// <param name="ATCom">AT指令</param>
  6.         /// <returns>发送指令后返回的字符串</returns>
  7.         public string SendAT(string ATCom)
  8.         {
  9.             string result = string.Empty;
  10.             //忽略接收缓冲区内容,准备发送
  11.             sp.DiscardInBuffer();

  12.             //注销事件关联,为发送做准备
  13.             sp.DataReceived -= sp_DataReceived;

  14.             //发送AT指令
  15.             try
  16.             {
  17.                 sp.Write(ATCom + "\r");
  18.             }
  19.             catch (Exception ex)
  20.             {
  21.                 sp.DataReceived += sp_DataReceived;
  22.                 throw ex;
  23.             }

  24.             //接收数据 循环读取数据 直至收到“OK”或“ERROR”
  25.             try
  26.             {
  27.                 string temp = string.Empty;
  28.                 while (temp.Trim() != "OK" && temp.Trim() != "ERROR")
  29.                 {
  30.                     temp = sp.ReadLine();
  31.                     result += temp;
  32.                 }
  33.                 return result;
  34.             }
  35.             catch (Exception ex)
  36.             {
  37.                 throw ex;
  38.             }
  39.             finally
  40.             {
  41.                 //事件重新绑定 正常监视串口数据
  42.                 sp.DataReceived += sp_DataReceived;
  43.             }
  44.         }
复制代码
      发送AT指令的过程:先清空缓冲区,注销事件关联(收到字符不再自动调用sp_DataReceived函数,这样发送AT指令后方能正常接收数据);发送AT指令,接收返回数据;最后重新绑定事件,以监视串口数据,以正常引发OnRecieved事件。

  • SendMsg 发送短信,参数:phone 手机号,msg 短信内容。


        此方法调用SendAT方法,发送指令 实例化PDUEncoding类完成编码,从而完成短信的发送
更新:发送短信的SendAT同时发送AT指令和短信PDU串,有的短信收发设备处理会出现一定问题,改成分开发送,收到”>”后再发PDU串
  1.         /// <summary>
  2.         /// 发送短信
  3.         /// 发送失败将引发异常
  4.         /// </summary>
  5.         /// <param name="phone">手机号码</param>
  6.         /// <param name="msg">短信内容</param>
  7.         public void SendMsg(string phone, string msg)
  8.         {
  9.             PDUEncoding pe = new PDUEncoding();
  10.             string temp = pe.PDUUSC2Encoder(phone, msg);

  11.             int len = (temp.Length - Convert.ToInt32(temp.Substring(0, 2), 16) * 2 - 2) / 2;  //计算长度
  12.             try
  13.             {
  14.                 //注销事件关联,为发送做准备
  15.                 sp.DataReceived -= sp_DataReceived;

  16.                 sp.Write("AT+CMGS=" + len.ToString() + "\r");
  17.                 sp.ReadTo(">");
  18.                 sp.DiscardInBuffer();

  19.                 //事件重新绑定 正常监视串口数据
  20.                 sp.DataReceived += sp_DataReceived;

  21.                 temp = SendAT(temp + (char)(26));  //26 Ctrl+Z ascii码
  22.             }
  23.             catch (Exception)
  24.             {
  25.                 throw new Exception("短信发送失败");
  26.             }
  27.             finally
  28.             {
  29.             }

  30.             if (temp.Substring(temp.Length - 4, 3).Trim() == "OK")
  31.             {
  32.                 return;
  33.             }

  34.             throw new Exception("短信发送失败");
  35.         }
复制代码
    发送过程:实例化PDUEncoding类,完成PDU的编码工作;计算长度(AT+CMGS=<LEN><cr>中的LEN)发送指令;查看是否发送成功,不成功则引发异常。

  • ReadMsgByIndex 按序号读取短信

  1.         /// <summary>
  2.         /// 按序号读取短信
  3.         /// </summary>
  4.         /// <param name="index">序号</param>
  5.         /// <param name="msgCenter">短信中心</param>
  6.         /// <param name="phone">发送方手机号码</param>
  7.         /// <param name="msg">短信内容</param>
  8.         /// <param name="time">时间字符串</param>
  9.         public void ReadMsgByIndex(int index, out string msgCenter, out string phone, out string msg, out string time)
  10.         {
  11.             string temp = string.Empty;
  12.             PDUEncoding pe = new PDUEncoding();
  13.             try
  14.             {
  15.                 temp = SendAT("AT+CMGR=" + index.ToString() + "\r");
  16.             }
  17.             catch (Exception ex)
  18.             {
  19.                 throw ex;
  20.             }

  21.             if (temp.Trim() == "ERROR")
  22.             {
  23.                 throw new Exception("没有此短信");
  24.             }
  25.             temp = temp.Split((char)(13))[2];       //取出PDU串(char)(13)为0x0a即\r 按\r分为多个字符串 第3个是PDU串

  26.             pe.PDUDecoder(temp, out msgCenter, out phone, out msg, out time);
  27.         }
复制代码
     按序号读短信,实例化PDUEncoding;发送AT指令;提取PDU串;PDU解码。
  • DeleteMsgByIndex 参数 index 序号,删除指定序号的短信(短信在SIM卡中是按序号存储的)。

  1.         public void DeleteMsgByIndex(int index)
  2.         {
  3.             if (SendAT("AT+CMGD=" + index.ToString() + "\r").Trim() == "OK")
  4.             {
  5.                 return;
  6.             }

  7.             throw new Exception("删除失败");
  8.         }
  9.     }
复制代码
   删除过程:发送AT指令;查看是否成功;成功则返回,否则引发异常。

事件:OnRecieved  收到短信后引发事件。
      事件声明:  
  1.   /// <summary>
  2.   /// 创建事件收到信息的委托
  3.    /// </summary>
  4.   /// <param name="sender"></param>
  5.   /// <param name="e"></param>
  6.    public delegate void OnRecievedHandler(object sender, EventArgs e);
  7.    
  8.   /// <summary>
  9.   /// 收到短信息事件 OnRecieved
  10.   /// 收到短信将引发此事件
  11.    /// </summary>
  12.    public event OnRecievedHandler OnRecieved;
复制代码

      事件由串口事件引发,检测出有短信,则引发事件
  1.   void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
  2.   {
  3.       string temp = sp.ReadLine();
  4.       if (temp.Length > 8)
  5.       {
  6.           if (temp.Substring(0, 6) == "+CMTI:")  //收到短信标记
  7.           {
  8.               NewMsgIndex = Convert.ToInt32(temp.Split(',')[1]);
  9.               OnRecieved(this, e);
  10.           }
  11.       }
  12.   }
复制代码

      事件使用方式:
  1.   gm.OnRecieved += new GSMModem.OnRecievedHandler(gm_OnRecieved); //添加事件绑定
复制代码

      程序引发事件,事件函数
  1.   void gm_OnRecieved(object sender, EventArgs e)
  2.   {
  3.       string s1, s2, s3, s4;
  4.       gm.ReadMsgByIndex(gm.NewMsgIndex, out s1, out s2, out s3, out s4);
  5.       textBox1.Text = s1 + s2 + s3 + s4;  //此处引发异常(此处不属于主进程)  须回调函数才能改变此属性
  6.    }
复制代码
     一般还需要一个回调函数,这样才能改变Form中的控件属性,详细参考MSDN
委托与事件可参考:C#事件(event)解析

类库简化版完成,功能尚不完善,加入自己程序时要做一定更改,让代码更安全
GSMMODEM.rar (134.42 KB, 下载次数: 4598)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-4-29 10:14 , Processed in 0.083198 second(s), 32 queries .

Powered by WLSD X3.1

© 2013-2014 WLSD Inc.

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