/// <summary> /// 获取未读信息列表 /// </summary> /// <returns>未读信息列表(中心号码,手机号码,发送时间,短信内容)</returns> public string[] GetUnreadMsg() { string[] result = new string[255]; string[] temp = null; string tmp = string.Empty; tmp = SendAT("AT+CMGL=0"); if (tmp.Substring(tmp.Length - 4, 3).Trim() == "OK") { temp = tmp.Split('\r'); } PDUEncoding pe = new PDUEncoding(); int i = 0; foreach (string str in temp) { if (str != null && str.Length != 0 && str.Substring(0, 2).Trim() != "+C" && str.Substring(0, 2) != "OK") { result[i] = pe.PDUDecoder(str); i++; } } return(result); }
/// <summary> /// 发送短信 (重载) /// </summary> /// <param name="phone">手机号码</param> /// <param name="msg">短信内容</param> /// <param name="msgType">短信类型</param> public void SendMsg(string phone, string msg, MsgType msgType) { if (msgType == MsgType.AUSC2) { SendMsg(phone, msg); } else { PDUEncoding pe = new PDUEncoding(); pe.ServiceCenterAddress = msgCenter; //短信中心号码 服务中心地址 string temp = pe.PDU7BitEncoder(phone, msg); int len = (temp.Length - Convert.ToInt32(temp.Substring(0, 2), 16) * 2 - 2) / 2; //计算长度 try { temp = SendAT("AT+CMGS=" + len.ToString() + "\r" + temp + (char)(26)); //26 Ctrl+Z ascii码 } catch (Exception) { throw new Exception("短信发送失败"); } if (temp.Substring(temp.Length - 4, 3).Trim() == "OK") { return; } throw new Exception("短信发送失败"); } }
/// <summary> /// 发送短信 /// 发送失败将引发异常 /// </summary> /// <param name="phone">手机号码</param> /// <param name="msg">短信内容</param> public void SendMsg(string phone, string msg) { PDUEncoding pe = new PDUEncoding(); pe.ServiceCenterAddress = msgCenter; //短信中心号码 服务中心地址 string temp = pe.PDUUSC2Encoder(phone, msg); //得到编码后的信息 Console.WriteLine(temp); int len = (temp.Length - Convert.ToInt32(temp.Substring(0, 2), 16) * 2 - 2) / 2; //计算长度 Console.WriteLine(len); try { //注销事件关联,为发送做准备 sp.DataReceived -= sp_DataReceived; sp.Write("AT+CMGS=" + len.ToString() + "\r"); sp.ReadTo(">"); sp.DiscardInBuffer(); //事件重新绑定 正常监视串口数据 sp.DataReceived += sp_DataReceived; temp = SendAT(temp + (char)(26)); //26 Ctrl+Z ascii码 } catch (Exception ex) { throw new Exception(ex.Message); } finally { } if (temp.Substring(temp.Length - 4, 3).Trim() == "OK") { return; } throw new Exception("短信发送失败"); }
/// <summary> /// 按序号读取短信 /// </summary> /// <param name="index">序号</param> /// <returns>信息字符串 (中心号码,手机号码,发送时间,短信内容)</returns> public string ReadMsgByIndex(int index) { string temp = string.Empty; string msgCenter, phone, msg, time; PDUEncoding pe = new PDUEncoding(); try { temp = SendAT("AT+CMGR=" + index.ToString()); } catch (Exception ex) { throw ex; } if (temp.Trim() == "ERROR") { throw new Exception("没有此短信"); } temp = temp.Split((char)(13))[2]; //取出PDU串(char)(13)为0x0a即\r 按\r分为多个字符串 第3个是PDU串 pe.PDUDecoder(temp, out msgCenter, out phone, out msg, out time); if (AutoDelMsg) { try { DelMsgByIndex(index); } catch { } } return(msgCenter + "," + phone + "," + time + "," + msg); }
/// <summary> /// 发送短信 /// 发送失败将引发异常 /// </summary> /// <param name="phone">手机号码</param> /// <param name="msg">短信内容</param> public void SendMsg(string phone, string msg) { PDUEncoding pe = new PDUEncoding(); pe.ServiceCenterAddress = msgCenter; //短信中心号码 服务中心地址 string temp = pe.PDUUSC2Encoder(phone, msg);//得到编码后的信息 Console.WriteLine(temp); int len = (temp.Length - Convert.ToInt32(temp.Substring(0, 2), 16) * 2 - 2) / 2; //计算长度 Console.WriteLine(len); try { //注销事件关联,为发送做准备 sp.DataReceived -= sp_DataReceived; sp.Write("AT+CMGS=" + len.ToString() + "\r"); sp.ReadTo(">"); sp.DiscardInBuffer(); //事件重新绑定 正常监视串口数据 sp.DataReceived += sp_DataReceived; temp = SendAT(temp + (char)(26)); //26 Ctrl+Z ascii码 } catch (Exception ex) { throw new Exception(ex.Message); } finally { } if (temp.Substring(temp.Length - 4, 3).Trim() == "OK") { return; } throw new Exception("短信发送失败"); }
/// <summary> /// 按序号读取短信 /// </summary> /// <param name="index">序号</param> /// <returns>信息字符串 (中心号码,手机号码,发送时间,短信内容)</returns> public string ReadMsgByIndex(int index) { string temp = string.Empty; string msgCenter, phone, msg, time; PDUEncoding pe = new PDUEncoding(); try { temp = SendAT("AT+CMGR=" + index.ToString()); } catch (Exception ex) { throw ex; } if (temp.Trim() == "ERROR") { throw new Exception("没有此短信"); } temp = temp.Split((char)(13))[2]; //取出PDU串(char)(13)为0x0a即\r 按\r分为多个字符串 第3个是PDU串 pe.PDUDecoder(temp, out msgCenter, out phone, out msg, out time); if (AutoDelMsg) { try { DelMsgByIndex(index); } catch { } } return msgCenter + "," + phone + "," + time + "," + msg; }
/// <summary> /// 获取未读信息列表 /// </summary> /// <returns>未读信息列表(中心号码,手机号码,发送时间,短信内容)</returns> public string[] GetUnreadMsg() { string[] result = new string[255]; string[] temp = null; string tmp = string.Empty; tmp = SendAT("AT+CMGL=0"); if (tmp.Substring(tmp.Length - 4, 3).Trim() == "OK") { temp = tmp.Split('\r'); } PDUEncoding pe=new PDUEncoding(); int i = 0; foreach (string str in temp) { if (str != null && str.Length != 0 && str.Substring(0, 2).Trim() != "+C" && str.Substring(0, 2) != "OK") { result[i] = pe.PDUDecoder(str); i++; } } return result; }