发送短信的AT命令之间的时间延迟 [英] time delay between AT commands for sending sms

查看:116
本文介绍了发送短信的AT命令之间的时间延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个通过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++;
            }

        }

解决方案

我的代码在这里有什么错误吗?

是的,您需要对响应进行一些处理,但是实际上您已经很接近了(而且更好的是,当我看到对Sleep的呼叫时,我最初以为您正在使用的不幸的并不常见的send-sleep模式).而且您正确地使用\r终止了AT命令行,所以很好的开始.

发送AT命令后,您应该连续不断地(反复)读取调制解调器的响应行,直到获得最终结果代码(使用 Contains("ERROR")不够好).参见此答案以获取代码流结构以及对确定行是否为最终结果代码的函数的引用.

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

对于AT+CMGS命令,您必须等到收到"\r\n> "字符串后,再发送任何有效载荷数据(上面的答案中也包含了这一内容).

I am working on an application that sends SMS through GSM Modem, and i use AT Commands for that. When i debug my application, Then sms is sent to reciever, but when i run the application sms is not sent to Reciever. My code is below and my gsm Modem is D-Link DWM-156. is there any wrong here in my code? thanks.

if (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++;
            }

        }

解决方案

is there any wrong here in my code?

Yes, you need to rework your response handling a little bit, but you are actually close (and way better that the unfortunately not uncommon send-sleep pattern that I initially thought you were using when I saw a call to Sleep). And you are correctly using \r to terminate AT command lines, so good start.

After sending an AT command you should continuously read (over and over again) response lines from the modem until you get a Final result code (using Contains("ERROR") is not good enough). See this answer for the code flow structure and reference to a function for determining if a line is a final result code.

You can keep the 100ms delay between sending the command line and starting to read the response; I have in fact added 200ms to my atinout program recently (locally, not released yet).

And for the AT+CMGS command, you must wait until you have received the "\r\n> " string before sending any payload data (also covered in the answer linked above).

这篇关于发送短信的AT命令之间的时间延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆