Exemplo n.º 1
0
        /// <summary>
        /// 获取未读信息列表
        /// </summary>
        /// <returns>未读信息列表(中心号码,手机号码,发送时间,短信内容)</returns>
        public List <DecodedMessage> GetUnreadMsg()
        {
            List <DecodedMessage> result = new List <DecodedMessage>();

            string[] temp = null;
            string   tmp  = string.Empty;

            Console.WriteLine("!!!!!!!!!!!!!!!!!GetUnreadMsg锁死");
            isSendMessageEvent.WaitOne();
            tmp = SendAT("AT+CMGL=0");
            isSendMessageEvent.Set();
            Console.WriteLine("!!!!!!!!!!!!!!!!!GetUnreadMsg释放");
            if (tmp.Contains("OK"))
            {
                temp = tmp.Split('\r');
            }

            PDUEncoding pe = new PDUEncoding();

            foreach (string str in temp)
            {
                if (str != null && str.Length > 18)   //短信PDU长度仅仅短信中心就18个字符
                {
                    result.Add(pe.PDUDecoder(str));
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 按序号读取短信
        /// </summary>
        /// <param name="index">序号</param>
        /// <returns>信息字符串 (中心号码,手机号码,发送时间,短信内容)</returns>
        public DecodedMessage 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)
            {
                //打印日志
                string errTxt = string.Format("  ReadMsgByIndex:{0}\r\n{1}\r\n{2}", temp, ex.Message, ex.StackTrace);
                LogHelpers.Error(errTxt);
                throw ex;
            }

            if (temp.Contains("ERROR"))
            {
                //打印日志
                LogHelpers.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
                {
                    DeleteMsgByIndex(index);
                }
                catch (Exception ex)
                {
                    //打印日志
                    string errTxt = string.Format("  {0}\r\n{1}", ex.Message, ex.StackTrace);
                    LogHelpers.Error(errTxt);
                    throw ex;
                }
            }

            return(pe.PDUDecoder(index, temp.Replace((char)(13), ' ').Trim()));
            //return msgCenter + "," + phone + "," + time + "," + msg;
        }
Exemplo n.º 3
0
        /// <summary>
        /// 按序号读取短信
        /// </summary>
        /// <param name="index">序号</param>
        /// <returns>信息字符串 (中心号码,手机号码,发送时间,短信内容)</returns>
        public DecodedMessage ReadMsgByIndex(int index)
        {
            string temp = string.Empty;
            //string msgCenter, phone, msg, time;
            PDUEncoding pe = new PDUEncoding();

            try
            {
                Console.WriteLine("!!!!!!!!!!!!!!!!!ReadMsgByIndex锁死");
                isSendMessageEvent.WaitOne();
                temp = SendAT("AT+CMGR=" + index.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                isSendMessageEvent.Set();
                Console.WriteLine("!!!!!!!!!!!!!!!!!ReadMsgByIndex释放");
            }

            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
                {
                    DeleteMsgByIndex(index);
                }
                catch
                {
                }
            }

            return(pe.PDUDecoder(temp));
            //return msgCenter + "," + phone + "," + time + "," + msg;
        }
Exemplo n.º 4
0
        /// <summary>
        /// 获取已读或未读信息列表
        /// </summary>
        /// <returns>未读信息列表(中心号码,手机号码,发送时间,短信内容)</returns>
        public List <DecodedMessage> GetReceiveMsg(int iMsgType, out string sInfo)
        {
            List <DecodedMessage> result = new List <DecodedMessage>();

            string[] temp      = null;
            string   tmp       = string.Empty;
            string   sRead     = string.Empty;
            int      iCurIndex = 0;

            sInfo = "";

            if (iMsgType != 1)
            {
                iMsgType = 0;
            }

            tmp = SendAT("AT+CMGL=" + iMsgType);

            if (tmp.Contains("OK"))
            {
                temp = tmp.Split('\r');
                PDUEncoding pe = new PDUEncoding();
                foreach (string str in temp)
                {
                    if (str != null && str.Length > 6)   //短信PDU长度仅仅短信中心就18个字符
                    {
                        sRead = str.Replace((char)(13), ' ').Trim();
                        if (sRead.Substring(0, 6) == "+CMGL:")
                        {
                            iCurIndex = Convert.ToInt32(sRead.Split(',')[0].Substring(6));  //存储新信息序号
                        }
                        if (sRead.Length > 30)
                        {
                            try
                            {
                                //sInfo +=  " ReadPDUindex: " + iCurIndex + " sReadPDU:" + sRead;
                                result.Add(pe.PDUDecoder(iCurIndex, sRead));
                            }
                            catch (Exception ex)
                            {
                                sInfo += " DECODER:" + ex.ToString() + " ReadPDUindex: " + iCurIndex + " sReadPDU:" + sRead;
                                //打印日志
                                string errTxt = string.Format("  sInfo:{0}\r\n{1}\r\n{2}", sInfo, ex.Message, ex.StackTrace);
                                LogHelpers.Error(errTxt);
                                //return result;
                                //throw ex;
                            }
                            if (AutoDelMsg)
                            {
                                try
                                {
                                    DeleteMsgByIndex(iCurIndex);
                                }
                                catch (Exception ex)
                                {
                                    sInfo += " DEL:" + ex.ToString();
                                    //打印日志
                                    string errTxt = string.Format("  sInfo:{0}\r\n{1}\r\n{2}", sInfo, ex.Message, ex.StackTrace);
                                    LogHelpers.Error(errTxt);
                                    //throw ex;
                                }
                            }
                        }
                    }
                }
            }
            return(result);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 按序号读取短信
        /// </summary>
        /// <param name="index">序号</param>
        /// <returns>信息字符串 (中心号码,手机号码,发送时间,短信内容)</returns>
        public DecodedMessage 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.Contains("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
                {
                    DeleteMsgByIndex(index);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return pe.PDUDecoder(index,temp.Replace((char)(13),' ').Trim());
            //return msgCenter + "," + phone + "," + time + "," + msg;
        }
Exemplo n.º 6
0
        /// <summary>
        /// 获取已读或未读信息列表
        /// </summary>
        /// <returns>未读信息列表(中心号码,手机号码,发送时间,短信内容)</returns>
        public List<DecodedMessage> GetReceiveMsg(int iMsgType,out string sInfo)
        {
            List<DecodedMessage> result = new List<DecodedMessage>();
            string[] temp = null;
            string tmp = string.Empty;
            string sRead = string.Empty;
            int iCurIndex = 0;
            sInfo = "";

            if (iMsgType != 1) iMsgType = 0;

            tmp = SendAT("AT+CMGL=" + iMsgType);

            if (tmp.Contains("OK"))
            {
                temp = tmp.Split('\r');
                PDUEncoding pe = new PDUEncoding();
                foreach (string str in temp)
                {
                    if (str != null && str.Length > 6)   //短信PDU长度仅仅短信中心就18个字符
                    {
                        sRead = str.Replace((char)(13), ' ').Trim();
                        if (sRead.Substring(0, 6) == "+CMGL:")
                        {
                            iCurIndex = Convert.ToInt32(sRead.Split(',')[0].Substring(6));  //存储新信息序号
                        }
                        if (sRead.Length > 30)
                        {
                            try
                            {
                                //sInfo +=  " ReadPDUindex: " + iCurIndex + " sReadPDU:" + sRead;
                                result.Add(pe.PDUDecoder(iCurIndex, sRead));
                            }
                            catch (Exception ex)
                            {
                                sInfo += " DECODER:" + ex.ToString() + " ReadPDUindex: " + iCurIndex + " sReadPDU:" + sRead;
                                //return result;
                                //throw ex;
                            }
                            if (AutoDelMsg)
                            {
                                try
                                {
                                    DeleteMsgByIndex(iCurIndex);
                                }
                                catch (Exception ex)
                                {
                                    sInfo += " DEL:" + ex.ToString();
                                    //throw ex;
                                }
                            }
                        }
                    }
                }
            }
            return result;
        }