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.
Asked
Active
Viewed 8,642 times
7
-
I guess you know how to communicate with this device (I don't) - there should be some simple operation (say asking for device version/serial number) you can send to each port to find the device – Random Dev Aug 21 '11 at 15:02
-
Can I send AT Commands to a port that's not connected to a modem? – Dohamsg Aug 21 '11 at 15:09
-
sure - why not, you won't see any response thats IMHO all – Random Dev Aug 21 '11 at 15:11
-
You would be unlucky to get an adverse response to sending an AT command to a port that is not connected. Either the port has nothing on it - in which case no response will be given, in the event that something is connected to the port (decreasingly likely nowadays) - you would have to match the serial port settings and the expected messages that this device is waiting for. – iandotkelly Aug 21 '11 at 15:21
2 Answers
6
What I did in my application for one similar task:
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; }
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)

Alex Klaus
- 8,168
- 8
- 71
- 87
0
// 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);
}
}

Nitin Gupta
- 202
- 3
- 9