//méthode Connect() permet de connecter l'appareil GSM au port public bool Connecter() { if (gsmPort == null || !IsConnected || !gsmPort.IsOpen) { GSMcom com = RechercherModem(); if (com != null) { try { gsmPort.PortName = com.Name; //COM2 gsmPort.BaudRate = 33600; //28800 192.168.10.10 192.168.1.117 gsmPort.Parity = Parity.None; gsmPort.DataBits = 8; gsmPort.StopBits = StopBits.One; gsmPort.Handshake = Handshake.RequestToSend; gsmPort.DtrEnable = true; gsmPort.RtsEnable = true; gsmPort.NewLine = Environment.NewLine; gsmPort.Open(); IsConnected = true; } catch (Exception e) { Console.WriteLine(e.Message); IsConnected = false; } } else { IsConnected = false; } } return(IsConnected); }
// cette fonction permet de rechercher si un appareil GSM est connecté public GSMcom RechercherModem() { IEnumerator enumerator = List().GetEnumerator(); GSMcom com = enumerator.MoveNext() ? (GSMcom)enumerator.Current : null; if (com == null) { IsDeviceFound = false; Console.WriteLine("Désolé aucun modem GSM trouvé!"); Console.WriteLine("Veuillez brancher un appareil GSM et réessayer"); } else { //affiche la description et le nom du modem puis le connecte au port IsDeviceFound = true; Console.WriteLine(com.ToString()); Connecter(); } return(com); }
public GSMcom[] List() { List <GSMcom> gsmcom = new List <GSMcom>(); ConnectionOptions options = new ConnectionOptions(); options.Impersonation = ImpersonationLevel.Impersonate; options.EnablePrivileges = true; try { string path = $@"\\{Environment.MachineName}\root\cimv2"; ManagementScope scope = new ManagementScope(path, options); scope.Connect(); ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_POTSModem"); ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query); ManagementObjectCollection collection = search.Get(); foreach (ManagementObject obj in collection) { string portName = obj["AttachedTo"].ToString(); string portDescription = obj["Description"].ToString(); if (portName != "") { GSMcom com = new GSMcom(); com.Name = portName; com.Description = portDescription; gsmcom.Add(com); } } } catch (StackOverflowException ex) { Console.WriteLine(ex.Message); //IsConnected = false; } return(gsmcom.ToArray()); }