Exemplo n.º 1
0
        /// <summary>
        /// 发送短信
        /// 发送失败将引发异常
        /// </summary>
        /// <param name="phone">手机号码</param>
        /// <param name="msg">短信内容</param>
        public void SendMsg(string phone, string msg)
        {
            PDUEncoding pe = new PDUEncoding();
            string temp = pe.PDUEncoder(phone, msg);

            int len = (temp.Length - Convert.ToInt32(temp.Substring(0, 2), 16) * 2 - 2) / 2;  //计算长度
            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)
            {
                throw new Exception("短信发送失败");
            }
            finally
            {
            }

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

            throw new Exception("短信发送失败");
        }
Exemplo n.º 2
0
        //void ReadNewMsg(out string msgCenter, out string phone, out string msg, out string time)
        //{
        //    Char[] temp = null;
        //    sp.Read(temp, 0, 2);
        //}
        /// <summary>
        /// 按序号读取短信
        /// </summary>
        /// <param name="index">序号</param>
        /// <param name="msgCenter">短信中心</param>
        /// <param name="phone">发送方手机号码</param>
        /// <param name="msg">短信内容</param>
        /// <param name="time">时间字符串</param>
        public void ReadMsgByIndex(int index, out string msgCenter, out string phone, out string msg, out string time)
        {
            string temp = string.Empty;
            PDUEncoding pe = new PDUEncoding();
            try
            {
                temp = SendAT("AT+CMGR=" + index.ToString() + "\r");
            }
            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);
        }