Find gsm modem port in c #

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

+7
source share
2 answers

What I did in my application for one similar task:

  • To verify that the modem is connected to a specific port, you can send an AT command to that 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; } 
  • In the next step, we can collect some data from the modem. I used the following commands:

    • ATQ0 - enable confirmations (receive OK at each request)
    • ATE0 - enable echo
    • ATI - get modem information
    • ATI3 - get advanced modem details (not all modems support this command)
+6
source
  // 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); } } 
0
source

All Articles