1

我编写了一个使用 AT 命令发送 SMS 的应用程序,如下所示:

SerialPort _serialPort = new SerialPort("COM40", 115200);
Thread.Sleep(1000);
_serialPort.Open();
Thread.Sleep(1000);
_serialPort.Write("AT+CMGF=1\r");
Thread.Sleep(1000);
_serialPort.Write("AT+CMGS=\"" + toPhoneNumber + "\"\r\n");
Thread.Sleep(1000);
_serialPort.Write("Test" + "\x1A");
Thread.Sleep(1000);
_serialPort.Close(); 

如果电话已安装且 COM 端口号可用,则此代码运行良好。

当双卡手机连接到电脑时,有什么方法可以选择发送信息的 SIM 卡吗?

4

2 回答 2

1

3GPP 规范中没有定义标准的 AT 命令。 似乎个别设备制造商已经定义了自己的 AT 命令来选择 SIM,例如这个。

联系设备制造商并询问他们为此定义了什么。

于 2013-05-13T09:34:05.607 回答
-1
public string ExecCommand(SerialPort port, string command, int responseTimeout, string errorMessage)
    {
        try
        {

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

            string input = ReadResponse(port, responseTimeout);
            if ((input.Length == 0) || ((!input.EndsWith("\r\n> ")) && (!input.Contains("\r\nOK\r\n"))))
                throw new ApplicationException("No success message was received.");
            return input;
        }
        catch (Exception ex)
        {
            //int a = ErrorCmd(port);
            throw ex;
        }
    }

    public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            if (e.EventType == SerialData.Chars)
            {
                receiveNow.Set();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public string ReadResponse(SerialPort port, int timeout)
    {
        string buffer = string.Empty;
        try
        {
            do
            {
            //Thread.Sleep(5000);
            jump: if (receiveNow.WaitOne(timeout, false))
                {
                    string t = port.ReadExisting();
                    buffer += t;
                }
                else
                {
                    if (buffer.Length == 0)
                    {
                        goto jump;
                    }
                    if (buffer.Length > 0)
                    {
                        //goto jump;
                        throw new ApplicationException("Response received is incomplete.");
                    }
                    else
                        throw new ApplicationException("No data received from phone.");
                }
            }
            while (!buffer.Contains("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n"));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return buffer;
    }

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

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

            string input = ReadResponse(port, responseTimeout);
            if ((input.Length == 0) || ((!input.EndsWith("\r\n> ")) && (!input.Contains("\r\nOK\r\n"))))
                throw new ApplicationException("No success message was received.");
            return input;
        }
        catch (Exception ex)
        {
            //int a = ErrorCmd(port);
            throw ex;
        }
    }

    public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            if (e.EventType == SerialData.Chars)
            {
                receiveNow.Set();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public string ReadResponse(SerialPort port, int timeout)
    {
        string buffer = string.Empty;
        try
        {
            do
            {
            //Thread.Sleep(5000);
            jump: if (receiveNow.WaitOne(timeout, false))
                {
                    string t = port.ReadExisting();
                    buffer += t;
                }
                else
                {
                    if (buffer.Length == 0)
                    {
                        goto jump;
                    }
                    if (buffer.Length > 0)
                    {
                        //goto jump;
                        throw new ApplicationException("Response received is incomplete.");
                    }
                    else
                        throw new ApplicationException("No data received from phone.");
                }
            }
            while (!buffer.Contains("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n"));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return buffer;
    }
于 2015-08-04T13:14:28.653 回答