【问题标题】:time delay between AT commands for sending sms发送短信的 AT 命令之间的时间延迟
【发布时间】:2015-12-13 10:56:44
【问题描述】:

我正在开发一个通过 GSM 调制解调器发送 SMS 的应用程序,为此我使用 AT 命令。 当我调试我的应用程序时,短信会发送到接收方,但是当我运行应用程序时,短信不会发送到接收方。 我的代码如下,我的 gsm 调制解调器是 D-Link DWM-156。 我的代码有什么问题吗? 谢谢。

如果 (SPort.IsOpen) {

            int i = 0;
            while (i < msgs.Count)
            {
                SPort.DiscardInBuffer();
                System.Threading.Thread.Sleep(1000);
                var res = SPort.ReadExisting();
                SPort.DiscardOutBuffer();
                System.Threading.Thread.Sleep(1000);

                SPort.Write("AT\r");
                System.Threading.Thread.Sleep(1000);
                while ((res = SPort.ReadExisting()).Contains("ERROR")) ;
                resultResponse += "1" + res + "---";

                SPort.Write("AT+CMGF=0\r");
                System.Threading.Thread.Sleep(100);
                while ((res = SPort.ReadExisting()).Contains("ERROR")) ;
                resultResponse += "2" + res + "---";
                SPort.Write("AT+CSCS=\"HEX\"\r");//char set
                System.Threading.Thread.Sleep(100);
                while ((res = SPort.ReadExisting()).Contains("ERROR")) ;
                resultResponse += "3" + res + "---";

                SPort.Write("AT+CSMP=17,71,0,17\r");
                System.Threading.Thread.Sleep(100);
                while ((res = SPort.ReadExisting()).Contains("ERROR")) ;
                resultResponse += "4" + res + "---";

                string hexString = msgs[i];

                SPort.Write(string.Format("AT+CMGS={0}\r", (hexString.Length - 16) / 2));
                System.Threading.Thread.Sleep(100);
                while ((res = SPort.ReadExisting()).Contains("ERROR")) ;
                resultResponse += "5" + res + "---";

                SPort.Write(string.Format("{0}{1}\n", hexString, Convert.ToChar(26)));
                System.Threading.Thread.Sleep(100);
                while ((res = SPort.ReadExisting()).Contains("ERROR")) ;
                resultResponse += "6" + res + "---";

                System.Threading.Thread.Sleep(1500);
                response += res;
                i++;
            }

        }

【问题讨论】:

    标签: c# gsm at-command


    【解决方案1】:

    我的代码有什么问题吗?

    是的,您需要重新处理您的响应处理,但实际上您已经很接近了(而且,当我看到调用 Sleep 时,我最初认为您正在使用的不幸并不少见的 send-sleep 模式要好得多)。并且您正确地使用\r 来终止 AT 命令行,所以好的开始。

    发送 AT 命令后,您应该不断地(一遍又一遍地)从调制解调器读取响应行,直到获得最终结果代码(使用 Contains("ERROR") 不够好)。请参阅this answer 了解代码流结构和用于确定某行是否为最终结果代码的函数。

    您可以在发送命令行和开始读取响应之间保持 100ms 的延迟;事实上,我最近在我的atinout 程序中添加了 200 毫秒(本地,尚未发布)。

    对于AT+CMGS 命令,您必须等到收到"\r\n&gt; " 字符串后才能发送任何有效负载数据(也包含在上面链接的答案中)。

    【讨论】:

    • 感谢您的有用回答,但是我还没有完成我的程序,我也遇到了同样的问题。关于您回答的问题:如果我收到“错误”响应,我该怎么办?我应该重新发送相同的命令吗?我应该取消相同的 SMS 命令并重新启动它吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    相关资源
    最近更新 更多