Exemplo n.º 1
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.º 2
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.º 3
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);
            }
        }