Esempio n. 1
0
        /// <summary>
        /// 从接收到的Pdu串中解析出短信
        /// </summary>
        /// <param name="index">短信在SIM卡中的位置</param>
        /// <param name="pdustr">短信内容</param>
        /// <returns></returns>
        public static Notelet Parse(int index, string pdustr)
        {
            Notelet notelet = new Notelet();

            notelet.location = index;
            try {
                //解析出短信中心号码
                int smsclen = int.Parse(pdustr.Substring(0, 2), NumberStyles.HexNumber) * 2;
                if (smsclen == 0)
                {
                    notelet.smscNum = "";
                }
                else
                {
                    notelet.smscNum = ConvertedNum2PhoneNum(pdustr.Substring(4, smsclen - 2));
                }
                //解析出回复号码
                int phoneNumIndex = smsclen + 4;
                int phoneNumLen   = int.Parse(pdustr.Substring(phoneNumIndex, 2), NumberStyles.HexNumber);
                if (phoneNumLen % 2 != 0)
                {
                    phoneNumLen++;
                }
                string phoneNum = ConvertedNum2PhoneNum(pdustr.Substring(phoneNumIndex + 4, phoneNumLen));
                notelet.mobileNum = phoneNum.Substring(0, 2) == "86" ? phoneNum.Remove(0, 2) : phoneNum;
                //解析出接收时间
                int           sendTimeIndex = 2 + smsclen + 2 + 4 + phoneNumLen + 2 + 2;
                StringBuilder timeString    = new StringBuilder("20");
                timeString.Append(pdustr[sendTimeIndex + 1]);                //年
                timeString.Append(pdustr[sendTimeIndex]);
                timeString.Append("-");
                timeString.Append(pdustr[sendTimeIndex + 3]);                //月
                timeString.Append(pdustr[sendTimeIndex + 2]);
                timeString.Append("-");
                timeString.Append(pdustr[sendTimeIndex + 5]);                //日
                timeString.Append(pdustr[sendTimeIndex + 4]);
                timeString.Append(" ");
                timeString.Append(pdustr[sendTimeIndex + 7]);                //时
                timeString.Append(pdustr[sendTimeIndex + 6]);
                timeString.Append(":");
                timeString.Append(pdustr[sendTimeIndex + 9]);                //分
                timeString.Append(pdustr[sendTimeIndex + 8]);
                timeString.Append(":");
                timeString.Append(pdustr[sendTimeIndex + 11]);                //秒
                timeString.Append(pdustr[sendTimeIndex + 10]);
                notelet.transTime = DateTime.Parse(timeString.ToString());
                // 解析短信内容
                int pduIndex = sendTimeIndex + 14;
                int pduLen   = int.Parse(pdustr.Substring(pduIndex, 2), NumberStyles.HexNumber);
                notelet.pdustr = pdustr.Substring(pduIndex + 2, pduLen * 2);
            }
            catch {
                notelet.smscNum   = "";
                notelet.mobileNum = "1860";
                notelet.transTime = DateTime.Now;
                notelet.pdustr    = "";
            }
            return(notelet);
        }
Esempio n. 2
0
            /// <summary>
            /// 对返回行进行处理
            /// </summary>
            /// <param name="answer">接收到的行</param>
            /// <returns>任务执行的结果</returns>
            public override TaskStatus Receive(string answer)
            {
                switch (state)
                {
                case 1:
                    Regex[] regex = new Regex[] {
                        new Regex(@"\+CMGL:\s*(?<index>\d+),(?<stat>\d+),(?<alpha>\d*),(?<length>\d+)"),
                        new Regex(@"OK"),
                        new Regex(@"\w*ERROR\w*")
                    };
                    Match[] match = new Match[] {
                        regex[0].Match(answer),
                        regex[1].Match(answer),
                        regex[2].Match(answer)
                    };
                    if (match[0].Success)                               //有短信, 得到位置信息, 进入下一步读取短信内容
                    {
                        index = int.Parse(match[0].Result("${index}"));
                        state = 2; return(TaskStatus.Unfinished);
                    }
                    else if (match[1].Success)       //命令执行成功
                    {
                        if (notelets.Count > 0 && modem.NoteletReceived != null)
                        {
                            modem.NoteletReceived(modem, new NoteletReceivedEventArgs(notelets.ToArray()));
                        }
                        return(TaskStatus.Finished);
                    }
                    else if (match[2].Success)       //命令执行错误, 重复执行但不超过最大重复次数
                    {
                        if (repeat++ >= REPEATNUMBER)
                        {
                            return(TaskStatus.Failed);
                        }
                        step0(); return(TaskStatus.Unfinished);
                    }
                    else
                    {
                        return(TaskStatus.Unexpected);
                    }

                case 2:
                    regex = new Regex[] { new Regex(@"[0-9a-fA-F]+") };
                    match = new Match[] { regex[0].Match(answer) };
                    if (match[0].Success)                               //接收到短信内容, 进入第一步接收下一条
                    {
                        Notelet notelet = Notelet.Parse(index, answer);
                        notelets.Add(notelet); state = 1;
                        return(TaskStatus.Unfinished);
                    }
                    else
                    {
                        return(TaskStatus.Unexpected);
                    }

                default: Start(modem); return(TaskStatus.Unfinished);
                }
            }
Esempio n. 3
0
 /// <summary>
 /// 发送短信
 /// </summary>
 /// <param name="notelet">发送的短信</param>
 public void SendNotelet(Notelet notelet)
 {
     tasks.Enqueue(new NoteletSend(notelet));
 }
Esempio n. 4
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="sms">待发送的短信</param>
 public NoteletSend(Notelet notelet)
 {
     this.notelet = notelet;
 }
Esempio n. 5
0
 /// <summary>
 /// 初始化类Vultrue.Communication.NoteletSendedEventArgs的新实例
 /// </summary>
 /// <param name="notelet">已发送的短信</param>
 public NoteletSendedEventArgs(Notelet notelet)
 {
     this.notelet = notelet;
 }