Dohamsg
Dohamsg

Reputation: 151

Find gsm modem port in c#

I want to loop through the available ports: System.IO.Ports.SerialPort.GetPortNames() to find if a port is used by a gsm modem. Any idea please.

Upvotes: 7

Views: 8866

Answers (2)

Nitin Gupta
Nitin Gupta

Reputation: 202

                // Check each Availble COM port
                foreach (string l_sport in l_available_ports)
                {
                    GlobalVars.g_serialport = GlobalFunc.OpenPort(l_sport, Convert.ToInt32(this.cboBaudRate.Text), Convert.ToInt32(this.cboDataBits.Text), Convert.ToInt32(this.txtReadTimeOut.Text), Convert.ToInt32(this.txtWriteTimeOut.Text));
                    if (GlobalVars.g_serialport.IsOpen)
                    {
                        GlobalVars.g_serialport.WriteLine("AT\r");
                        Thread.Sleep(500);
                        string l_response = GlobalVars.g_serialport.ReadExisting();
                        if (l_response.IndexOf("OK") >= 0)
                        {
                            GlobalVars.g_serialport.WriteLine("AT+CMGF=1\r");
                            Thread.Sleep(500);
                            string l_response1 = GlobalVars.g_serialport.ReadExisting();
                            if (l_response1.IndexOf("OK") >= 0)
                            {
                                GlobalVars.g_PhoneNo = txt_PhNum.Text;
                                MessageBox.Show("Connected Successfully", "Connection", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                lblConnectionStatus.Text = "Phone Connected Successfully.";
                                btnOK.Enabled = false;
                                btnDisconnect.Enabled = true;

                                GlobalVars.g_serialport.WriteLine("AT+CGSN\r");
                                Thread.Sleep(500);
                                string l_imei = GlobalVars.g_serialport.ReadExisting();
                                Console.WriteLine("Modem IMEI:" + l_imei);
                                if (l_imei.IndexOf("OK", 1) > 0)
                                {
                                    l_imei = l_imei.Replace("AT+CGSN\r\r\n", null);
                                    l_imei = l_imei.Replace("\r\n\r\nOK\r\n", null);
                                    lbl_ModemIMEI.Text = l_imei;
                                }
                                else
                                {
                                    lblConnectionStatus.Text = "Phone Connected Successfully. Error reading IMEI.";
                                }
                                EnableSMSNotification(GlobalVars.g_serialport);

                                break;
                            }
                            else
                            {
                                Console.WriteLine("No AT+CMGF cmd response");
                            }
                        }
                        else
                        {
                            Console.WriteLine("No AT cmd response");
                        }
                    }
                    else
                    {
                        Console.WriteLine("No Phone At:" + l_sport);
                    }
                }

Upvotes: 0

Alex Klaus
Alex Klaus

Reputation: 8954

What I did in my application for one similar task:

  1. To check that a modem is connected to particular port you can send AT command into this port. This function below returns true if we found a modem on the current COM port:

    private bool CheckExistingModemOnComPort(SerialPort serialPort)
    {
        if ((serialPort == null) || !serialPort.IsOpen)
            return false;
    
        // Commands for modem checking
        string[] modemCommands = new string[] { "AT",       // Check connected modem. After 'AT' command some modems autobaud their speed.
                                                "ATQ0" };   // Switch on confirmations
        serialPort.DtrEnable = true;    // Set Data Terminal Ready (DTR) signal 
        serialPort.RtsEnable = true;    // Set Request to Send (RTS) signal
    
        string answer = "";
        bool retOk = false;
        for (int rtsInd = 0; rtsInd < 2; rtsInd++)
        {
            foreach (string command in modemCommands)
            {
                serialPort.Write(command + serialPort.NewLine);
                retOk = false;
                answer = "";
                int timeout = (command == "AT") ? 10 : 20;
    
                // Waiting for response 1-2 sec
                for (int i = 0; i < timeout; i++)
                {
                    Thread.Sleep(100);
                    answer += serialPort.ReadExisting();
                    if (answer.IndexOf("OK") >= 0)
                    {
                        retOk = true;
                        break;
                    }
                }
            }
            // If got responses, we found a modem
            if (retOk)
                return true;
    
            // Trying to execute the commands without RTS
            serialPort.RtsEnable = false;
        }
        return false;
    }
    
  2. On the next stage we can collect some data from the modem. I used the following commands:

    • ATQ0 - switch on confirmations (receive OK on each request)
    • ATE0 - switch on echo
    • ATI - get modem details
    • ATI3 - get extended modem details (not all modems supports this command)

Upvotes: 6

Related Questions