1

I wrote an application to send an SMS using AT commands as follows:

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(); 

This code works well if the phone is installed and if a COM port number is available.

When a dual SIM phone is connected to the computer, is there any way to select the SIM to send message from?

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Snj
  • 883
  • 3
  • 9
  • 20
  • 1
    It's not your question, but with regard to your code, please take into account the answer to this question: http://stackoverflow.com/questions/16402403/unable-to-send-sms-using-at-commands/16404193#16404193 – user1725145 May 13 '13 at 13:16

2 Answers2

1

There is no standard AT command defined in the 3GPP specification. It seems that individual device manufacturers have defined their own AT commands to choose the SIM, for example this one.

Contact the device manufacturer and ask them what they have defined for this.

user1725145
  • 3,993
  • 2
  • 37
  • 58
-2
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;
    }
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339