使用AT命令使用GSM发送多部分短信

时间:2014-09-10 08:55:26

标签: c# sms port gsm at-command

我有一个Windows应用程序,用于发送连接到GSM调制解调器的SMS。我只使用AT命令连接端口和发送文本。

我的问题是我无法发送更多信息(每个部分英文为160个字符,波斯语为70个字符)。

这是我命令端口使用AT命令发送短信的部分:

ExecCommand(port, "AT", 300, "No phone connected at " + strPortName + ".");
ExecCommand(port, "AT+CMGF=1", 300, "Failed to set message format.");
var command = "AT+CSCS=\"" + "HEX" + "\"";
ExecCommand(port, command, 300, "Failed to support unicode");
ExecCommand(port, "AT+CSMP=1,167,0,8", 300, "Failed to set message properties.");
command = "AT+CMGS=\"" + phoneNo + "\"";
ExecCommand(port, command, 300, "Failed to accept phoneNo");
message = message.ToCharArray().Select(Convert.ToInt32).Select(value => String.Format("{0:X}", value)).Aggregate("", (current, hexOutput) => current + hexOutput.PadLeft(4, '0'));
command = message + char.ConvertFromUtf32(26) + "\r";
var recievedData = ExecCommand(port, command, 3000, "Failed to send message"); 

这是ExecCommand方法

    public string ExecCommand(SerialPort port, string command, int responseTimeout, string errorMessage)
    {
        try
        {
            // receiveNow = new AutoResetEvent();
            port.DiscardOutBuffer();
            port.DiscardInBuffer();
            receiveNow.Reset();
            port.Write(command + "\r");

            //Thread.Sleep(3000); //3 seconds
            string input = ReadResponse(port, responseTimeout);
            if ((input.Length == 0) || ((!input.EndsWith("\r\n> ")) && (!input.EndsWith("\r\nOK\r\n"))))
                throw new ApplicationException("No success message was received.");
            return input;
        }
        catch (Exception ex)
        {
            throw new ApplicationException(errorMessage, ex);
        }
    }

1 个答案:

答案 0 :(得分:2)

一般AT命令处理

您走在正确的轨道上,我很高兴看到几项基本事情正确完成(使用\r终止AT命令行,等待"\r\n> " AT+CMGS,等待OK最终结果代码而不是睡觉),非常好的开始!

但您需要稍微更改结构。首先,您需要处理所有其他最终结果代码,而不仅仅是OK。您应该将AT+CMGS命令与其他命令区别对待,因为与等待一件事相比,您应该等待该命令的两件事(首先是前缀,后来是发送消息文本后的最终结果代码)(例如最终结果代码)。此外,调制解调器的所有响应都是完整的行("\r\n> "前缀除外),因此请更改算法以逐行处理响应。

String input;
do {
        input = ReadLine(port, responseTimeout);
} while (!isFinalResultCode(input));

您在问题中使用的所有命令都不会产生中间响应以供使用,但如果您要运行AT+CPBR之类的命令,则会在该循环内使用这些中间响应(并且您必须移动在尝试将该行用作中间响应之前,最终结果测试进入循环。)

您可以在atinout中看到is_final_result函数或在ST-Ericsson的U300 RIL中看到相应的函数(请参阅this answer中的链接和注释)。

Multipart SMS

我认为在文本模式下这是不可能的。有关PDU模式下多部分SMS的详细信息,请参阅Naser Asadi建议的链接。还有一些有用的信息 http://mobiletidings.com/2009/02/18/combining-sms-messages/http://developer.nokia.com/community/discussion/showthread.php/109602-Concatenated-SMS-in-PDU-Mode

相关问题