使用 GSM 手机通过 AT 命令发送的 SMS 出现错误 [英] SMS by AT commands with GSM mobile is giving error

查看:36
本文介绍了使用 GSM 手机通过 AT 命令发送的 SMS 出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 GSM 手机通过 AT 命令发送短信.我想发送大量消息,如数千条.我读到通过 GSM 移动我们可以每分钟发送 6-8 条短信.但是当我发送消息时,有人会去,有人不会.我从 excel 文件中获取信息意味着目标号码和消息文本.你能告诉我为什么有些短信可以,有些不能.我的代码是

 SmsFields smsObj = null;列表smsCol = 空;SerialPort 串口 = 空;StringBuilder strbuild = new StringBuilder();尝试{//验证表单如果(!验证())返回;串行端口 = 新串行端口();////设置串口对象的属性serialport.PortName = cboPort.SelectedItem.ToString();串口.波特率 = 9600;serialport.Parity = Parity.None;串口.DataBits = 8;serialport.StopBits = StopBits.One;serialport.Handshake = Handshake.RequestToSend;serialport.DtrEnable = true;serialport.RtsEnable = true;//打开发送短信的端口串口.Open();//检查端口是否打开如果 (!serialport.IsOpen){MessageBox.Show("串口没有打开,请换其他端口试试");返回;}//创建smsFields类的对象并填充泛型集合中的数据smsObj = SmsFields.Instance;smsColl = smsObj.FillData(txtFilePath.Text);如果(smsCol == null){MessageBox.Show("在excel表格中找不到数据");返回;}//从SmsFields类中获取单条记录并发送消息foreach(smsColll 中的 SmsFields 短信){//检查手机状态serialport.WriteLine("AT" + Environment.NewLine);//配置消息为SMS(0为PDU格式)和(1为文本格式)serialport.WriteLine("AT+CMGF=1" + Environment.NewLine);//设置消息中心号码serialport.WriteLine("AT+CSCA="" + txtServiceNo.Text + """ + Environment.NewLine);//设置目的地号码serialport.WriteLine("AT+CMGS="" + sms.DestinationNo + """ + Environment.NewLine);//指定消息并发送Ctrl+zserialport.WriteLine(sms.Message + (char)26);//显示包含输出消息的缓冲区System.Threading.Thread.Sleep(4000);}

解决方案

我认为您的问题是您没有在发送下一个命令之前等待最终结果代码(即 OK、ERROR 和其他一些).问题在于,如果新命令未完成,则新命令将触发正在进行的命令的中止.引用 V.250:><块引用>

5.6.1 中止命令

...

中止命令的完成由从 DTE 到 DCE 的传输任何字符.

所以总是在发送 AT 命令时,您必须在发送下一个命令之前等待最终结果代码.

我可以建议将 serialport.WriteLine("ATxxx" + Environment.NewLine) 重构为 sendCommand(serialport, "ATxxx") 函数吗?然后可以在该函数的末尾添加等待最终结果的代码.

I am sending SMS by AT commands with GSM mobile phone. I wanna send bulk of message like thousands. I read that by GSM mobile we can send 6-8 sms per minute. But when I send messages then someone are going and someone not. I am getting information from excel file means destination number and message text. Can you tell me why some sms are going and some not. My code is

        SmsFields smsObj = null;
        List<SmsFields> smsColl = null;
        SerialPort serialport = null;
        StringBuilder strbuild = new StringBuilder();
        try
        {
            //Validate the form 
            if (!Validation()) return;

            serialport = new SerialPort();

            ////Sets the properties of serial port object
            serialport.PortName = cboPort.SelectedItem.ToString();
            serialport.BaudRate = 9600;
            serialport.Parity = Parity.None;
            serialport.DataBits = 8;
            serialport.StopBits = StopBits.One;
            serialport.Handshake = Handshake.RequestToSend;
            serialport.DtrEnable = true;
            serialport.RtsEnable = true;

            //Open the port to send sms
            serialport.Open();

            //Check if port is opened or not
            if (!serialport.IsOpen)
            {
                MessageBox.Show("Serial port is not opened. Please try with other port");
                return;
            }

            //Create smsFields class's object and fill the data in the generic collection
            smsObj = SmsFields.Instance;
            smsColl = smsObj.FillData(txtFilePath.Text);

            if (smsColl == null)
            {
                MessageBox.Show("No data found in the excel table");
                return;
            }
            //Gets the single record from SmsFields class and sends the message
            foreach (SmsFields sms in smsColl)
            {

                //checks phone status
                serialport.WriteLine("AT" + Environment.NewLine);
                //Configures message as SMS (0 for PDU format) and (1 for text format)
                serialport.WriteLine("AT+CMGF=1" + Environment.NewLine);

                //Sets message center number
                serialport.WriteLine("AT+CSCA="" + txtServiceNo.Text + """ + Environment.NewLine);

                //Sets destination number
                serialport.WriteLine("AT+CMGS="" + sms.DestinationNo + """ + Environment.NewLine);

                //Specifies message and sends Ctrl+z
                serialport.WriteLine(sms.Message + (char)26);

                //Displays buffer containing output messages
                System.Threading.Thread.Sleep(4000);
   }

解决方案

I think your problem is that you are not waiting for the final result code (i.e. OK, ERROR and a few others) before sending the next command. The problem with that is that the new command will trigger an abort of the ongoing command if it is not finished. To quote V.250:

5.6.1 Aborting commands

...

Aborting of commands is accomplished by the transmission from the DTE to the DCE of any character.

So ALWAYS when sending AT commands, you MUST wait for the final result code before sending the next command.

Might I suggest refactoring serialport.WriteLine("ATxxx" + Environment.NewLine) into a sendCommand(serialport, "ATxxx") function? And then you can add waiting for the final result code at the end of that function.

这篇关于使用 GSM 手机通过 AT 命令发送的 SMS 出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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