通过双SIM卡手机使用AT命令发送短信

时间:2013-05-12 02:24:11

标签: c# sms serial-port at-command

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

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卡手机连接到电脑时,有没有办法选择SIM卡发送信息?

2 个答案:

答案 0 :(得分:1)

the 3GPP specification.中没有定义标准AT命令 似乎各个设备制造商已经定义了他们自己的AT命令来选择SIM,例如this one.

与设备制造商联系,询问他们为此定义的内容。

答案 1 :(得分:-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;
    }