Voglio passare in rassegna le porte disponibili: System.IO.Ports.SerialPort.GetPortNames() per trovare se una porta viene utilizzata da un modem GSM. Qualche idea per favore.Trova porta modem GSM in C#
7
A
risposta
6
Quello che ho fatto nella mia domanda di un compito simile:
Per verificare che un modem è collegato alla particolare porta è possibile inviare comandi AT in questa porta. Questa funzione qui sotto restituisce true se abbiamo trovato un modem sulla porta COM corrente:
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; }
sulla prossima fase siamo in grado di raccogliere alcuni dati dal modem. Ho usato i seguenti comandi:
- ATQ0 - accendere conferme (ricezione OK su ciascuna richiesta)
- ATE0 - accendere eco
- ATI - ottenere dettagli modem
- ATI3 - richiama i dettagli del modem esteso (non tutti i modem supportano questo comando)
+0
Non riesco a far funzionare questa risposta –
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);
}
}
Credo che si sa come comunicare con questo dispositivo (non mi) - ci dovrebbe essere una semplice operazione (dicono chiedendo versione apparecchio/numero di serie) è possibile inviare a ogni porta per trovare il dispositivo – Carsten
Posso inviare comandi AT a una porta che non è connessa a un modem? – Dohamsg
sicuro - perché no, non vedrai alcuna risposta che sia IMHO tutto – Carsten