public void ChercheModem() { SerialPort oSerialPort = new SerialPort(); string CMD = "AT\r"; string DataReceived = ""; string[] sPortList = System.IO.Ports.SerialPort.GetPortNames(); for (int i = 0; i < sPortList.Length; i++) { try { oSerialPort.PortName = sPortList[i]; oSerialPort.ReadTimeout = 500; oSerialPort.Open(); if (oSerialPort.IsOpen) { oSerialPort.WriteLine(CMD); Application.DoEvents(); DataReceived = oSerialPort.ReadLine().ToString(); } if (DataReceived.Length == 0) { label7.Text = sPortList[i] + " : Aucun modem détecté"; Application.DoEvents(); } else { label7.Text = "Modem détecté sur le port " + (sPortList[i]); PortModemTrouve = sPortList[i]; } } catch { label7.Text = sPortList[i] + " : Aucun modem détecté"; Application.DoEvents(); } finally { if (oSerialPort.IsOpen) { oSerialPort.Close(); } } } }
public void ChercheModem() { SerialPort oSerialPort = new SerialPort(); string CMD = "AT\r"; string DataReceived = ""; string[] sPortList = System.IO.Ports.SerialPort.GetPortNames(); for (int i = 0; i <= sPortList.Length; i++) { try { oSerialPort.PortName = sPortList[i]; oSerialPort.Open(); if (!oSerialPort.IsOpen) { oSerialPort.WriteLine(CMD); } Thread.Sleep(500); // serial ports are slow DataReceived += oSerialPort.ReadLine().ToString(); //MessageBox.Show(sPortList[i] + " port responded " + DataReceived); if (DataReceived.Length == 0) label7.Text = "Aucun modem détecté"; else label7.Text = "Modem détecté sur le port " + (sPortList[i]); PortModemTrouve = sPortList[i]; } catch { } finally { if (oSerialPort.IsOpen) { oSerialPort.Close(); } } } }