コード例 #1
0
        private void SendMsg_Thead(object obj)
        {
            var gsmStruct = obj as GsmMsgStruct;

            if (null == gsmStruct)
            {
                return;
            }
            string phone = gsmStruct.Phone;
            string msg   = gsmStruct.Msg;

            ListenPort.ReadTimeout = 60000;

            string returnMsg = string.Empty;

            try
            {
                //注销事件关联,为发送做准备
                ListenPort.DataReceived -= Port_DataReceived;

                ListenPort.Write("AT+CMGS=" + phone + "\r");
                ListenPort.ReadTo(">");
                ListenPort.Write(msg + (char)(26));
                ListenPort.DiscardInBuffer();

                string wirteMessage = "AT+CMGS=" + phone + "\r" + ">" + msg + (char)(26);
                InvokeMessage(wirteMessage, "发送");

                string temp = string.Empty;

                while (!temp.Trim().Contains("OK") && !temp.Trim().Contains("ERROR"))
                {
                    Debug.WriteLine("temp = " + temp);
                    temp       = ListenPort.ReadLine();
                    returnMsg += temp;
                }
                Debug.WriteLine("result = " + returnMsg);

                if (returnMsg.Contains(msg + (char)(26)))
                {
                    returnMsg = returnMsg.Replace((msg + (char)(26)), string.Empty);
                }
                InvokeMessage(returnMsg, "接收");
                IsCommonWorkNormal = true;
            }
            catch (TimeoutException exp)
            {
                IsCommonWorkNormal = false;
                if (null != GSMTimeOut)
                {
                    GSMTimeOut(this, new ReceivedTimeOutEventArgs()
                    {
                        Second = ListenPort.ReadTimeout / 1000
                    });
                }
                InvokeMessage("短信接收超时", "接收");
                Debug.WriteLine("短信接收失败");
            }
            catch (Exception exp)
            {
                IsCommonWorkNormal = false;
                InvokeMessage("短信接收失败", "接收");
                if (null != GSMTimeOut)
                {
                    GSMTimeOut(this, new ReceivedTimeOutEventArgs()
                    {
                        Second = ListenPort.ReadTimeout / 1000
                    });
                }
                Debug.WriteLine("短信接收失败");
            }
            finally
            {
                //事件重新绑定 正常监视串口数据
                ListenPort.DataReceived += Port_DataReceived;
            }
            //  GSM通讯口状态监测
            if (SerialPortStateChanged != null)
            {
                SerialPortStateChanged(this, new CEventSingleArgs <CSerialPortState>(new CSerialPortState()
                {
                    PortNumber = Int32.Parse(ListenPort.PortName.Replace("COM", "")),
                    BNormal    = IsCommonWorkNormal,
                    PortType   = m_portType
                }));
            }
        }
コード例 #2
0
        /// <summary>
        /// 发送AT指令 逐条发送AT指令 调用一次发送一条指令
        /// 能返回一个OK或ERROR算一条指令
        /// </summary>
        /// <param name="ATCom">AT指令</param>
        /// <returns>发送指令后返回的字符串</returns>
        public string SendMsg(string atCom, out bool isSendSuccesse)
        {
            isSendSuccesse         = false;
            ListenPort.ReadTimeout = 3000;
            string result = string.Empty;

            //忽略接收缓冲区内容,准备发送
            ListenPort.DiscardInBuffer();

            //注销事件关联,为发送做准备
            ListenPort.DataReceived -= Port_DataReceived;

            try
            {
                /* 写入指令 */
                ListenPort.Write(atCom + "\r");

                /* 接收数据 循环读取数据 直至收到“OK”或“ERROR”*/
                string temp = string.Empty;
                if (!atCom.Contains(GsmHelper.AT_SAVE))
                {
                    while (!temp.Trim().Contains("OK") && !temp.Trim().Contains("ERROR"))
                    {
                        temp    = ListenPort.ReadLine();
                        result += temp;
                    }
                    Debug.WriteLine(result);
                }
                else
                {
                    var str = ListenPort.ReadExisting();
                    //while (str.Length != 9)
                    //{
                    //    str += ListenPort.ReadExisting();
                    //}
                    result = str;
                }
                /* 如果缓冲区中包含已发送的指令,则清除 */
                if (result.Contains(atCom))
                {
                    result = result.Replace(atCom, string.Empty);
                }
                isSendSuccesse = true;
            }
            catch (TimeoutException exp)
            {
                isSendSuccesse = false;
                //InvokeMessage("设置参" + atCom + "超时", "接收");
                if (null != GSMTimeOut)
                {
                    GSMTimeOut(this, new ReceivedTimeOutEventArgs()
                    {
                        Second = ListenPort.ReadTimeout / 1000
                    });
                }
            }
            catch (Exception exp)
            {
                isSendSuccesse = false;
                Debug.WriteLine(String.Format("发送指令:{0} 错误\r{1}", atCom, exp.Message));
            }
            finally
            {
                //事件重新绑定 正常监视串口数据
                ListenPort.DataReceived += Port_DataReceived;
            }
            return(result);
        }