【问题标题】:Sending multi part sms with GSM using AT Commands使用 AT 命令通过 GSM 发送多部分短信
【发布时间】:2015-11-24 18:42:41
【问题描述】:

我有一个 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);
        }
    }

【问题讨论】:

  • 我正在发送波斯语短信,所以我必须将其转换为十六进制。此代码工作正常,但仅适用于少于 70 个字符。

标签: c# sms port gsm at-command


【解决方案1】:

一般 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 中的链接和注释)。

多部分短信

我认为这在文本模式下是不可能的。有关 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多