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="phone">手机号码</param>
        /// <param name="msg">短信内容</param>
        public void SendMsg(string phone, string msg)
        {
            try
            {
                Console.WriteLine("!!!!!!!!!!!!!!!!!SendMsg锁死");
                isSendMessageEvent.WaitOne();
                Console.WriteLine("初始化PDUEncoding解码类");
                PDUEncoding pe = new PDUEncoding();
                pe.ServiceCenterAddress = msgCenter;                    //短信中心号码 服务中心地址

                string tmp = string.Empty;
                Console.WriteLine("获取PDUEncoding列表");
                var cmlist = pe.PDUEncoder(phone, msg);
                foreach (CodedMessage cm in cmlist)
                {
                    try
                    {
                        Console.WriteLine("注销事件关联,为发送做准备");
                        //注销事件关联,为发送做准备
                        lock (lockObject)
                        {
                            _com.DataReceived -= sp_DataReceived;
                        }
                        Console.WriteLine("准备发送");
                        _com.Write("AT+CMGS=" + cm.Length.ToString() + "\r");
                        _com.ReadTo(">");
                        _com.DiscardInBuffer();
                        Console.WriteLine("事件重新绑定 正常监视串口数据");
                        //事件重新绑定 正常监视串口数据
                        lock (lockObject)
                        {
                            _com.DataReceived += sp_DataReceived;
                        }
                        Console.WriteLine("准备发送短信");
                        tmp = SendAT(cm.PduCode + (char)(26));  //26 Ctrl+Z ascii码
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("短信发送失败:" + ex.Message);
                    }
                    if (tmp.Contains("OK"))
                    {
                        continue;
                    }

                    throw new Exception("短信发送失败");
                }
            }
            catch (Exception exmsg)
            {
                throw exmsg;
            }
            finally
            {
                isSendMessageEvent.Set();
                Console.WriteLine("!!!!!!!!!!!!!!!!!SendMsg释放");
            }
        }
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
            {
                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.º 4
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.º 5
0
        /// <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 tmp = string.Empty;

            foreach (CodedMessage cm in pe.PDUEncoder(phone, msg))
            {
                try
                {
                    //注销事件关联,为发送做准备,本命令直接发送不写Sim卡,CMSS从Sim卡发送,CMGW写Sim卡
                    _com.DataReceived -= sp_DataReceived;

                    _com.Write("AT+CMGS=" + cm.Length.ToString() + "\r");
                    _com.ReadTo(">");
                    _com.DiscardInBuffer();

                    //事件重新绑定 正常监视串口数据
                    _com.DataReceived += sp_DataReceived;

                    tmp = SendAT(cm.PduCode + (char)(26));  //26 Ctrl+Z ascii码
                }
                catch (Exception ex)
                {
                    //打印日志
                    string errTxt = string.Format("  短信发送失败:{0}\r\n{1}\r\n{2}", "SendMsg", ex.Message, ex.StackTrace);
                    LogHelpers.Error(errTxt);
                    throw new Exception("短信发送失败:" + ex.ToString());
                }
                //打印日志
                LogHelpers.Write(string.Format("  短信已发送:{0}\r\n{1}:{2}", tmp, phone, msg));
                if (tmp.Contains("OK"))
                {
                    continue;
                }
                //打印日志
                LogHelpers.Error(string.Format("  短信发送失败:{0}", tmp));
                throw new Exception("短信发送失败:" + tmp);
            }
        }
Exemplo n.º 6
0
        /// <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 tmp = string.Empty;

            foreach (CodedMessage cm in pe.PDUEncoder(phone, msg))
            {
                try
                {
                    //注销事件关联,为发送做准备,本命令直接发送不写Sim卡,CMSS从Sim卡发送,CMGW写Sim卡
                    _com.DataReceived -= sp_DataReceived;

                    _com.Write("AT+CMGS=" + cm.Length.ToString() + "\r");
                    _com.ReadTo(">");
                    _com.DiscardInBuffer();

                    //事件重新绑定 正常监视串口数据
                    _com.DataReceived += sp_DataReceived;

                    tmp = SendAT(cm.PduCode + (char)(26));  //26 Ctrl+Z ascii码
                }
                catch (Exception ee)
                {
                    throw new Exception("短信发送失败:" + ee.ToString());
                }
                if (tmp.Contains("OK"))
                {
                    continue;
                }

                throw new Exception("短信发送失败:" + tmp);
            }
        }
Exemplo n.º 7
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.º 8
0
        /// <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 tmp = string.Empty;
            foreach (CodedMessage cm in pe.PDUEncoder(phone, msg))
            {
                try
                {
                    //注销事件关联,为发送做准备,本命令直接发送不写Sim卡,CMSS从Sim卡发送,CMGW写Sim卡
                    _com.DataReceived -= sp_DataReceived;

                    _com.Write("AT+CMGS=" + cm.Length.ToString() + "\r");
                    _com.ReadTo(">");
                    _com.DiscardInBuffer();

                    //事件重新绑定 正常监视串口数据
                    _com.DataReceived += sp_DataReceived;

                    tmp = SendAT(cm.PduCode + (char)(26));  //26 Ctrl+Z ascii码
                }
                catch (Exception ee)
                {
                    throw new Exception("短信发送失败:" + ee.ToString());
                }
                if (tmp.Contains("OK"))
                {
                    continue;
                }

                throw new Exception("短信发送失败:" + tmp);
            }
        }
Exemplo n.º 9
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.º 10
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;
        }