private void btnATCommand_Click(object sender, EventArgs e)//发送AT指令
        {
            if (txtATCommand.Text == "")
            {
                txtMsg.AppendText("\r\nAT指令为空!");
                return;
            }
            if (!sp.IsConnect())
            {
                txtMsg.AppendText("\r\n串口打开失败!");
                return;
            }
            string result = sp.ExeATCommand(delspace(txtATCommand.Text));
            if (result != "")
            {
                txtMsg.AppendText("\r\n"+result);
            }
            sp.Close();
        }
 public bool IsConnect()
        {
            bool opened = IsOpen();
            bool isScc;
            isScc = false;            if (opened)
            {
                readBuffer = ExeATCommand("AT\r");
                if (readBuffer.IndexOf("OK") != -1)
                {
                    isScc = true;
                }                if (IsListen == false)
                {
                    Close();
                }
            }            if (isScc)
            {
                return true;
            }
            else
            {
                return false;
            }        }private bool IsOpen()
        {
            try
            {
                if (!s_port.IsOpen)
                {
                    s_port.Open();
                }
                Opened = s_port.IsOpen;
                SetPDU();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
     
      
 
       /// <summary>
        /// 设置短信PDU方式
        /// </summary>
        private void SetPDU()
        {
            ExeATCommand("AT+CMGF=0\r");
        }
       /// <summary>
        /// 执行AT命令
        /// </summary>
        /// <param name="p_ATCommand">命令</param>
        /// <returns>结果字符串</returns>
        public string ExeATCommand(string p_ATCommand)
        {
           string result = string.Empty; // 返回结果内容
            int SendCommandCount = 0; // 重试次数
            if (p_ATCommand != string.Empty && s_port.IsOpen == true)
            {
                s_port.Write(p_ATCommand); // 输出命令到串口
                System.Threading.Thread.Sleep(retryTimeOut); // 延迟指定时间
                while (SendCommandCount <= retryCount)
                {
                    // 没有达到重试次数时,始终执行
                    System.Threading.Thread.Sleep(retryTimeOut); // 延迟指定时间
                    result += s_port.ReadExisting(); // 读取结果
                    if (result.Length > 0 && result.Length > p_ATCommand.Length)
                    {
                        result = result.Replace(p_ATCommand + "\r\r\n", string.Empty); // 去掉发送过来的命令,关心结果
                        break;
                    }
                    SendCommandCount++;
                }
            }
            return result;
        }
我单步调试时发现,设置PDU格式的时候,不论是0或1,返回的都是error,请大家帮帮忙!

解决方案 »

  1.   


    private void btnATCommand_Click(object sender, EventArgs e)//发送AT指令 
            { 
                if (txtATCommand.Text == "") 
                { 
                    txtMsg.AppendText("\r\nAT指令为空!"); 
                    return; 
                } 
                if (!sp.IsConnect()) 
                { 
                    txtMsg.AppendText("\r\n串口打开失败!"); 
                    return; 
                } 
                string result = sp.ExeATCommand(delspace(txtATCommand.Text)); 
                if (result != "") 
                { 
                    txtMsg.AppendText("\r\n"+result); 
                } 
                sp.Close(); 
            } 
    public bool IsConnect() 
            { 
                bool opened = IsOpen(); 
                bool isScc; 
                isScc = false;             if (opened) 
                { 
                    readBuffer = ExeATCommand("AT\r"); 
                    if (readBuffer.IndexOf("OK") != -1) 
                    { 
                        isScc = true; 
                    }                 if (IsListen == false) 
                    { 
                        Close(); 
                    } 
                }             if (isScc) 
                { 
                    return true; 
                } 
                else 
                { 
                    return false; 
                }         } private bool IsOpen() 
            { 
                try 
                { 
                    if (!s_port.IsOpen) 
                    { 
                        s_port.Open(); 
                    } 
                    Opened = s_port.IsOpen; 
                    SetPDU(); 
                    return true; 
                } 
                catch (Exception) 
                { 
                    return false; 
                } 
            } 
        
                /// <summary> 
            /// 设置短信PDU方式 
            /// </summary> 
            private void SetPDU() 
            { 
                ExeATCommand("AT+CMGF=0\r"); 
            } 
          /// <summary> 
            /// 执行AT命令 
            /// </summary> 
            /// <param name="p_ATCommand">命令 </param> 
            /// <returns>结果字符串 </returns> 
            public string ExeATCommand(string p_ATCommand) 
            { 
              string result = string.Empty; // 返回结果内容 
                int SendCommandCount = 0; // 重试次数 
                if (p_ATCommand != string.Empty && s_port.IsOpen == true) 
                { 
                    s_port.Write(p_ATCommand); // 输出命令到串口 
                    System.Threading.Thread.Sleep(retryTimeOut); // 延迟指定时间 
                    while (SendCommandCount <= retryCount) 
                    { 
                        // 没有达到重试次数时,始终执行 
                        System.Threading.Thread.Sleep(retryTimeOut); // 延迟指定时间 
                        result += s_port.ReadExisting(); // 读取结果 
                        if (result.Length > 0 && result.Length > p_ATCommand.Length) 
                        { 
                            result = result.Replace(p_ATCommand + "\r\r\n", string.Empty); // 去掉发送过来的命令,关心结果 
                            break; 
                        } 
                        SendCommandCount++; 
                    } 
                } 
                return result; 
            } 
    我单步调试时发现,设置PDU格式的时候,不论是0或1,返回的都是error,请大家帮帮忙!这样好读一些!
      

  2.   

    你的SIM卡可能有密码,Modem还没有完全初始化
      

  3.   

    这个问题已经解决了,现在的问题是,输入AT+CSCA?获取短信中心号码的,返回的是error,在超级终端下试,也是error,不知道是不是我的硬件设备的问题呢?请高手指教了!
      

  4.   

    我获取不到短信中心号码,后来就把短信中心号码写死了,我也知道这样不好,换个地方就不行了,以为这样就能发送成功了,可问题又来了,我输入AT+CNMI=?返回error,输入atd,返回no carrier,不知道什么原因,请高手把邮箱留下,我把程序发给你,请有空帮忙看看!不胜感激了!