예제 #1
0
        /// <summary>
        /// 根据类型获取短信
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns>信息集合</returns>
        public List <SMSEnity> GetMsgByType(int type)
        {
            List <SMSEnity> result    = new List <SMSEnity>();
            string          resultStr = SendAT(ATCommand.CMGL, type);

            //"+CMGL: 1,1,,144\r0891683110100305F00005A10110F0000801101021000020805C0A656C76845BA2623760A8597DFF01611F8C2260A8900962E94E2D56FD8054901A7684901A4FE1670D52A1FF0C8054901A516C53F85C067AED8BDA4E3A60A8670D52A130025BA26237670D52A170ED7EBF00310030003000310030FF0C8BDD8D3967E58BE24E137EBF00310030003000310031FF0C6B228FCE4F7F75283002\r+CMGL: 2,1,,146\r0891683110100305F00005A10110F0000801101021000020824E3A4E868BA960A8768456DE94C397F35C3D663E4E2A6027FF0C6211516C53F84E3A60A863D04F9B4E8670AB94C34E1A52A1FF0C6D41884C97F34E50300197F365483001641E602A5E9467095C3D6709FF0C5FEB62E86253003100300031003500360032003000316CE8518C5F00901AFF0C8BA97B495F854E0D518D5BC25BDE3002\r+CMGL: 3,1,,146\r0891683110100305F00005A10110F0000801101021000020824E3A4E867ED960A863D04F9B8D348EAB670D52A1FF0C6211516C53F85F00901A003100300031003900388054901A79D84E66670D52A1FF0C003200345C0F65F64E3A60A863D04F9B6F0F8BDD63D0793A30014EE353D177ED4FE13001673A4E3B75598A007B49670D52A1FF0C62E800310030003100390038537353EF6CE8518C3002\r+CMGL: 4,1,,134\r0891683110100305F00005A10110F0000801101021000020768BDD8D395468546862A54E1A52A16BCF54684E3A60A863D04F9B8BDD8D394FE1606FFF0C8BA960A853CA65F64E8689E38BDD8D3960C551B5FF0C53D10048004652300038003000340031537353EF8BA25236FF0C4E5F53EF62E862538BDD8D3967E58BE270ED7EBF0031003000310039003000313002\r+CMGL: 5,1,,32\r0891683110903305F02405A00110F0000811704051731023100032002D0032FF1A53C85B9E60E00021\r+CMGL: 6,1,,152\r0891683110903305F02405A00110F0000811704051731023880032002D0031FF1A5C0A656C76845BA26237FF1A60A85F53524D751F65484F7F752876848D448D39595799104E3A65B052BF529B7545804A595799100031003851433002767B5F558054901A7F514E0A84254E1A5385007700770077002E00310030003000310030002E0063006F006DFF0C67E58BE230014EA48D3930015145503C300165B94FBF\r+CMGL: 7,1,,157\r0891683110903305F02408A1015695230008117091812170238C8054901A70AB94C3FF0C8BA97B495F85768458F097F366F47F8E5999FF01003100300031003500350035662F70AB94C3670D52A170ED7EBFFF0C73B057285F00901A70AB94C3537353EF83B78D604E009996514D8D3970ED95E894C397F3FF1A628A5E78798F7ED94F60300262E800310030003100350035003563090031952E537353EF63D053D64F7F7528\r+CMGL: 8,1,,30\r0891683110200405F0240D91683103897829F70008117062011375230A653652304E096761FF01\r+CMGL: 9,1,,30\r0891683110903305F0240D91685148101704F00008117072014524230A768458EB6C146E295EA6\r+CMGL: 10,1,,28\r0891683110200405F0040D91683130423215F200081170720165622308548C5E7365F65019\r+CMGL: 11,1,,30\r0891683110200405F0240D91685148101704F00008117072017592230A4E8689E351B35B9A671F\r+CMGL: 12,1,,26\r0891683110900405F0240D91685148101704F0000811707211108023064E8689E351B3\r+CMGL: 13,1,,36\r0891683110903305F0240D91685148101704F0000811707211110523104ED64EEC768458EB6C146E295EA65047\r+CMGL: 14,1,,32\r0891683110200405F0240D91685148101704F00008117072116152230C4E86548C5E7365F650199009\r+CMGL: 15,1,,28\r0891683110903305F0240D91685148101704F0000811707211717223085475768458EB6C14\r+CMGL: 16,1,,32\r0891683110903305F0240D91685148101704F00008117072118193230C548C5E7365F65019900962E9\r+CMGL: 17,1,,34\r0891683110903305F0240D91685148101704F00008117072213393230E628A63E14F4F5740768458EB6C14\r+CMGL: 18,1,,28\r0891683110200405F0040D91683130423215F2000811707221337523084E8689E351B35B9A\r+CMGL: 19,1,,22\r0891683110200405F0040D91683130423215F200081170722163552302548C\r\rOK"
            if (!(String.IsNullOrEmpty(resultStr) || resultStr.Equals(_OK) || resultStr.Equals(_ERROR)))
            {
                //协议头匹配
                const string CMGL = "+CMGL:";
                //"+CMGL: 1,1,,144"
                //"0891683110100305F00005A10110F0000801101021000020824E3A4E868BA960A8768456DE94C397F35C3D663E4E2A6027FF0C6211516C53F84E3A60A863D04F9B4E8670AB94C34E1A52A1FF0C6D41884C97F34E50300197F365483001641E602A5E9467095C3D6709FF0C5FEB62E86253003100300031003500360032003000316CE8518C5F00901AFF0C8BA97B495F854E0D518D5BC25BDE3002"
                string[] resultList = resultStr.Split('\r');
                //由于SendAT函数一定会返回结果 所以不用判断数组越界问题
                if (resultList[resultList.Length - 1].Equals(_OK))
                {
                    for (int i = 0; i < resultList.Length; i += 2)
                    {
                        if (!String.IsNullOrEmpty(resultList[i]))
                        {
                            //Protocol Head
                            string head = resultList[i];
                            if (head.StartsWith(CMGL))
                            {
                                //如果符合协议头标准,移除协议头,以获得参数
                                head = head.Remove(0, CMGL.Length);
                                string[] headArgs = head.Split(',');
                                //RFC 协议规定必须为4个参数
                                //+CMGL : <index>,<stat>, [<alpha>], <length>
                                //<pdu>
                                if (headArgs.Length == 4)
                                {
                                    int index, stat, alpha, length;
                                    Int32.TryParse(headArgs[0], out index);
                                    Int32.TryParse(headArgs[1], out stat);
                                    Int32.TryParse(headArgs[2], out alpha);
                                    Int32.TryParse(headArgs[3], out length);
                                    if (AutoDelMsg)
                                    {
                                        DelMsg(index);
                                    }
                                }
                                //PDU String
                                string   pduStr   = resultList[i + 1];
                                SMSEnity SMSEnity = PduDecoder.Decoding(pduStr); //PDU解码
                                result.Add(SMSEnity);
                            }
                        }
                    }
                }
            }
            return(result);
        }
예제 #2
0
        /// <summary>
        /// 按序号读取短信
        /// 如果没有,返回Null
        /// </summary>
        /// <param name="index">序号</param>
        /// <returns>信息字符串 (中心号码,手机号码,发送时间,短信内容)</returns>
        public SMSEnity ReadMsg(int index)
        {
            SMSEnity SMSEnity = null;
            //
            string resultStr = SendAT(ATCommand.CMGR, index);

            //"+CMGR: 1,,32\r0891683110200405F0240D91683148000148F70008117082419521230C00460061006E006800740031\r\rOK"
            //未找到 返回 ERROR
            if (!resultStr.Equals(_ERROR))
            {
                string[] resultList = resultStr.Split('\r');
                //由于SendAT函数一定会返回结果 所以不用判断数组越界问题
                if (resultList[resultList.Length - 1].Equals(_OK))
                {
                    string       head   = resultList[0];
                    string       pduStr = resultList[1];
                    const string CMGR   = "+CMGR:";
                    if (head.StartsWith(CMGR))
                    {
                        head = head.Remove(0, CMGR.Length);
                        string[] headArgs = head.Split(',');
                        //RFC 协议规定必须为3个参数
                        //+CMGR: <stat>, [<alpha>] ,<length>
                        //<pdu>
                        if (headArgs.Length == 3)
                        {
                            int stat, alpha, length;
                            Int32.TryParse(headArgs[0], out stat);
                            Int32.TryParse(headArgs[1], out alpha);
                            Int32.TryParse(headArgs[2], out length);
                            if (AutoDelMsg)
                            {
                                DelMsg(index);
                            }
                        }
                        //0891683110903305F0240D91685148101704F0000811707211110523104ED64EEC768458EB6C146E295EA65047

                        SMSEnity = PduDecoder.Decoding(pduStr);
                    }
                }
            }
            return(SMSEnity);
        }