private void listBox1_DoubleClick(object sender, EventArgs e) { if (listBox1.SelectedIndex > -1) { try { Modem modem = (Modem)listBox1.SelectedItem; Program.selectedModem = modem.Name; Program.selectedPort = modem.Port; Close(); } catch (Exception) { } } }
void addToList(Modem modem) { try { if (this.InvokeRequired) { Action a = () => _addToList(modem); Invoke(a); } else { _addToList(modem); } } catch (Exception) { } }
void _addToList(Modem modem) { bool flag = true; foreach (Object obj in listBox1.Items) { Modem mdm = (Modem)obj; if (mdm.Name == modem.Name) { flag = false; } } if (flag) { listBox1.Items.Add(modem); } }
private void SearchModem() { clearList(); disableControls(); try { String[] ports = Port.getAvailablePorts(); foreach (String port in ports) { try { Port p = new Port(); p.Notification = false; p.disableAutoThreadStart = true; p.setPort(port); p.open(); if (p.isOpen()) { p.getDirect().WriteLine("AT"); Thread.Sleep(300); String data = p.getDirect().ReadExisting(); if (data.ToLower().Contains("ok")) { p.getDirect().WriteLine("ATI");//p.write("AT+CGMI"); Thread.Sleep(500); String info = p.getDirect().ReadExisting(); String model = "", imei = "", manufacturer = ""; info = info.ToLower(); if (info.Contains("model:") && info.Contains("imei:") && info.Contains("manufacturer")) { int start = -1, end = -1; start = info.IndexOf("manufacturer:"); if (start > -1) { start += "manufacturer:".Length; } end = info.IndexOf((char)13, start); if (start > -1 && end > -1 && end > start) { manufacturer = info.Substring(start, end - start); } start = end = -1; start = info.IndexOf("model:"); if (start > -1) { start += "model:".Length; } end = info.IndexOf((char)13, start); if (start > -1 && end > -1 && end > start) { model = info.Substring(start, end - start); } start = end = -1; start = info.IndexOf("imei:"); if (start > -1) { start += "imei:".Length; } end = info.IndexOf((char)13, start); if (start > -1 && end > -1 && end > start) { imei = info.Substring(start, end - start); } Modem mod = new Modem(); mod.Port = port; manufacturer = manufacturer.Trim(); model = model.Trim(); imei = imei.Trim(); mod.Name = manufacturer.ToUpper() + " " + model.ToUpper() + " (" + imei + ")"; addToList(mod); } } } p.close(); } catch (Exception ex) { //MessageBox.Show(""+ex); } } } catch (Exception ex) { //MessageBox.Show(""+ex); } enableControls(); }