短信猫软件的实现(C#)<十>类库(API)完善

前一段时间完成了一个简单的类库,虽然可以完成可以完成短信猫短信的发送与接收,但是类库还不是很完善,现在奉上较为完善的版本。谢谢大家支持了啊。

完善后类库说明如下:

属性

序号

属性名称

数据类型

说明

1

CommPort

bool

串口号(运行时只读)

2

BaudRate

int

设备与计算机的串口波特率

设计时和运行时都可以读写

3

IsOpen

bool

设备是否已经打开(只读)

True 已打开 False 未打开

4

AutoDelMsg

bool

设置是否在阅读短信后自动删除SIM卡内短信存档(建议在经常接收短信时设置为 true)

设计和运行时都可以读写,true 自动删除 false 不自动删除

默认为 false

 

方法

² 打开设备(Open 

功能描述: 打开与设备的通讯端口,设备初始化

参数: 无

返回: 无 (打开失败将抛出异常) 

² 关闭设备(Close 

功能描述: 关闭与设备的通讯端口,设备关闭

参数: 无

返回: 无(关闭失败将抛出异常)

² 取得机器码(GetMachineNo 

功能描述: 取得机器码

参数: 无

返回: string 机器码字符串(设备厂商)

² 发送AT指令(SendAT 

功能描述: 向端口发送 AT 指令

参数: ATCmd string 您要发送的 AT 指令

返回: string AT指令返回结果 (如为错误,引发异常)

² 设置短信中心号码(SetMsgCenterNo 

功能描述: 设置短信中心号码

参数: sNewValue string 短信中心号码

返回: 无 (失败将抛出异常)

² 取得短信中心号码(GetMsgCenterNo 

功能描述: 取得短信中心号码

参数: 无

返回: string 短信中心号码

² 发送短消息(SendMsg

功能描述: 发送一条短信息

参数: phone string 对方的手机号码

msg string 短消息内容

msgCenterNo string 短信中心号码(可选)

msgType 枚举MgType 短信类型{AUSC2:USC2编码,A7Bit:7位编码} 可选(不选时USC2编码)

返回: 无 (发送失败将抛出异常)

² 取得未读信息列表(GetUnreadMsg

功能描述: 取得未读信息

参数: 无

返回: string[] 逗号分隔的未读信息列表字符串(中心号码,手机号码,发送时间,短信内容) (失败将抛出异常)

² 读取设备新收到的短消息(ReadNewMsg

功能描述: 读取设备新收到的短消息

参数: 无

返回: string 信息字符串,同信息列表中格式(失败将抛出异常)

² 读取短消息(ReadMsgByIndex

功能描述: 读取 SIM 卡中指定位置的短消息

参数: index int 短信序号

返回: string 信息字符串

² 删除短消息(DelMsgByIndex

功能描述: 删除 SIM 卡中指定位置的短信

参数: index int 要删除 SIM 卡中短信的位置

返回: 无 (失败将抛出异常)

事件

² OnReceive 当短信到达时触发这个事件,请在此事件中调用ReadNewMsg方法

程序说明:

加入了7bit的编码,发送英文短信每条可以发送160个字符(原来程序中是按USC2编码发送的英文短信,最多只能发送70个字符)
有关编码的详细信息参考:短信猫软件的实现(C#)<三>PDU格式短信解析

7bit的编码详细方案参考:短信猫软件的实现(C#)<九>7bitPDU的编码

7bit编码发送短信函数:

 1:  public void SendMsg(string phone, string msg, MsgType msgType)
 2:  {
 3:      if (msgType == MsgType.AUSC2)
 4:      {
 5:          SendMsg(phone, msg);
 6:      }
 7:      else
 8:      {
 9:   
10:          PDUEncoding pe = new PDUEncoding();
11:          pe.ServiceCenterAddress = msgCenter;                    //短信中心号码 服务中心地址
12:  
13:          string temp = pe.PDU7BitEncoder(phone, msg);
14:   
15:          int len = (temp.Length - Convert.ToInt32(temp.Substring(0, 2), 16) * 2 - 2) / 2;  //计算长度
16:          try
17:          {
18:              temp = SendAT("AT+CMGS=" + len.ToString() + "\r" + temp + (char)(26));  //26 Ctrl+Z ascii码
19:          }
20:          catch (Exception)
21:          {
22:              throw new Exception("短信发送失败");
23:          }
24:   
25:          if (temp.Substring(temp.Length - 4, 3).Trim() == "OK")
26:          {
27:              return;
28:          }
29:   
30:          throw new Exception("短信发送失败");
31:      }
32:  }

和原来发送函数类似:仅仅改动的地方是调用的PDUEncoding的7bit编码。

另外,类还改动了GSMModem的字段,属性,方法;增加了有关API,便于短信猫软件的实现。

字段:添加了newMsgIndex和msgCenter,newMsgIndex用于保存一条刚收到的新信息的序号,msgCenter保存设置的短信中心号,发送短信使用

1:   
2:  private int newMsgIndex;            //新消息序号
3:  
4:  private string msgCenter = string.Empty;           //短信中心号码

 属性:添加AutoDelMsg属性,代表是否需要在读取信息后自动删除SIM卡中存档。

 1:   
 2:  private bool autoDelMsg = false;
 3:   
 4:  /// <summary>
 5:  /// 对autoDelMsg访问
 6:  /// 设置是否在阅读短信后自动删除 SIM 卡内短信存档
 7:  /// 默认为 false 
 8:  /// </summary>
 9:  public bool AutoDelMsg
10:  {
11:      get
12:      {
13:          return autoDelMsg;
14:      }
15:      set
16:      {
17:          autoDelMsg = value;
18:      }
19:  }

AutoDelMsg为true时,将在读取短信的方法中调用DelMsgByIdex删除对应SIM卡中内容。

方法:添加 取得机器码(GetMachineNo)、设置短信中心号码(SetMsgCenterNo)、取得短信中心号码(GetMsgCenterNo)、取得未读信息列表(GetUnreadMsg)、读取设备新收到的短消息(ReadNewMsg)

取得机器码(GetMachineNo):

 1:  public string GetMachineNo()
 2:  {
 3:      string result = SendAT("AT+CGMI");
 4:      if (result.Substring(result.Length - 4, 3).Trim() == "OK")
 5:      {
 6:          result = result.Substring(0, result.Length - 5).Trim();
 7:      }
 8:      else
 9:      {
10:          throw new Exception("获取机器码失败");
11:      }
12:      return result;
13:  }

通过AT指令获取设备厂商名,若需要可以再添加其他内容。

设置短信中心号码(SetMsgCenterNo):

1:  public void SetMsgCenterNo(string msgCenterNo)
2:  {
3:      msgCenter = msgCenterNo;
4:  }

仅设置msgCenter字段,发送短信时:编码部分代码改为

1:   
2:  PDUEncoding pe = new PDUEncoding();
3:  pe.ServiceCenterAddress = msgCenter;                    //短信中心号码 服务中心地址
4:  
5:  string temp = pe.PDUUSC2Encoder(phone, msg);

取得短信中心号码(GetMsgCenterNo):

 1:  public string GetMsgCenterNo()
 2:  {
 3:      string tmp = string.Empty;
 4:      if (msgCenter != null && msgCenter.Length != 0)
 5:      {
 6:          return msgCenter;
 7:      }
 8:      else
 9:      {
10:          tmp = SendAT("AT+CSCA?");
11:          if (tmp.Substring(tmp.Length-4,3).Trim() == "OK")
12:          {
13:              return tmp.Split(‘\"‘)[1].Trim();
14:          }
15:          else
16:          {
17:              throw new Exception("获取短信中心失败");
18:          }
19:      }
20:  }

若字段msgCenter有值则直接返回,无则从SIM卡存档中读取。

取得未读信息列表(GetUnreadMsg):

 1:  public string[] GetUnreadMsg()
 2:  {
 3:      string[] result = new string[255];      //可存储255条 SIM卡不能存这么多
 4:      string[] temp = null;
 5:      string tmp = string.Empty;
 6:   
 7:      tmp = SendAT("AT+CMGL=0");                 //发送AT指令 读取未读信息
 8:      if (tmp.Substring(tmp.Length - 4, 3).Trim() == "OK")     //读取成功
 9:      {
10:          temp = tmp.Split(‘\r‘);
11:      }
12:   
13:      PDUEncoding pe=new PDUEncoding();          //解码类
14:      int i = 0;                                    //计数
15:      foreach (string str in temp)
16:      {         //此条为PDU串
17:          if (str != null && str.Length != 0 && str.Substring(0, 2).Trim() != "+C" && str.Substring(0, 2) != "OK")
18:          {
19:              result[i] = pe.PDUDecoder(str);             //解码
20:              i++;
21:          }
22:      }
23:   
24:      return result;               // 返回 空则无新信息
25:  }

列出所有未读信息,返回string[]类型,无新信息则返回空。

读取设备新收到的短消息(ReadNewMsg):

1:  public string ReadNewMsg()
2:  {
3:      return ReadMsgByIndex(newMsgIndex);
4:  }

新信息序号存在序号为newMsgIndex字段中,调用ReadMsgByIndex即可读取。

其他改动:有些方法的名称有少许改动,ReadMsgByIndex方法返回值类型和参数列表有些改变,算法没有变化,在此不做详细说明;详见附件项目文件

到此,较为完善类库完成!

欢迎大家提出宝贵意见,多谢大家的支持。

附件:工程项目文件

posted @ 2010-11-14 17:17  给我一杯酒  阅读(7923)  评论(19编辑  收藏  举报