c# - 使用 AT 命令通过 GSM 发送多部分短信

标签 c# sms port gsm at-command

我有一个用于发送 SMS 连接到 GSM 调制解调器的 Windows 应用程序。我只使用 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);
        }
    }

最佳答案

通用 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 .

关于c# - 使用 AT 命令通过 GSM 发送多部分短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25761202/

相关文章:

c# - 仅基于 WPF、WCF 和 ADO.net 的 .Net 项目同时将 DB 作为 Sybase 是否可能?

Android:发送短信 (sendTextMessage) 时出现 Unicode/Charset 问题

android - 如何在 Android 中识别哪些短信有送达报告

PHP - fsockopen() 超时而不是返回 false?

c# - 数据集级联和内存消耗

c# - XmlSerializer 在使用泛型类型约束时抛出 InvalidOperationException

c# - 控制 System.Diagnostics.Process.Start C# 的 Action

mysql - 在 MySQL 上自动发送短信

node.js - 如果 url 中没有端口号,nginx 上的 NodeJs 将无法工作

c++ - 如何用C语言将字符串从服务器返回到客户端?