通过C#使用AT命令发送文本消息时出错

时间:2016-03-01 22:01:50

标签: c# gsm at-command

我在使用AT命令时遇到了困难。我的GSM调制解调器与我的电脑连接为Com端口。发送短信时我的代码工作正常但发送的消息格式不正确。例如,我发送了“Hello World'但发送的消息显示''在手机里。 Plz指导我错在哪里。 这是我的代码。

try
        {

            string recievedData = ExecCommand(port, "AT", 300, "No phone connected");

            recievedData = ExecCommand(port, "AT+CMGF?", 300, "Failed to accept phoneNo");
            recievedData = ExecCommand(port, "AT+CMGF=1", 300, "Failed to set message format.");

            string command = "AT+CMGS=\"" + PhoneNo + "\"";
            recievedData = ExecCommand(port, command, 300, "Failed to accept phoneNo");

            command = "hello world!" + "\x1A";
            recievedData = ExecCommand(port, command, 3000, "Failed to send message"); //3 seconds

            if (recievedData.EndsWith("\r\nOK\r\n"))
            {
                isSend = true;
            }
            else if (recievedData.Contains("ERROR"))
            {
                isSend = false;
            }
            return isSend;
        }
        catch (Exception ex)
        {
            throw ex; 
        }

这是执行方法

public string ExecCommand(SerialPort port,string command, int responseTimeout, string errorMessage)
    {
        try
        {

            port.DiscardOutBuffer();
            port.DiscardInBuffer();
            receiveNow.Reset();
            port.Write(command + "\r");

            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 ex;
        }
    }

0 个答案:

没有答案