public void SettingsRestore(object settings) { //Debug.WriteLine("Apply ->" + parentWindow.ConnectionTabHelper.CurrentTabData.TabItem.Title); Settings Settings = (Settings)settings; if (Array.IndexOf(SerialOptions.DefaultBaudRate, Settings.Baudrate) == -1) { customBaudrate.value = Settings.Baudrate; ComboBox_BaudRate_Refresh(); this.ComboBox_Baudrate.SelectedIndex = SerialOptions.BaudRateList.Count - 1; } else { this.ComboBox_Baudrate.SelectedItem = Settings.Baudrate; } this.CheckBox_RTS.IsChecked = Settings.RTSEnable; this.CheckBox_DTR.IsChecked = Settings.DTREnable; this.Combo_DataBits.SelectedItem = Settings.DataBits; this.Combo_StopBits.SelectedItem = Settings.StopBits; this.Combo_Parity.SelectedItem = Settings.Parity; this.Combo_Handshake.SelectedItem = Settings.Handshake; Task.Factory.StartNew(() => { ProcessPortList.WaitOne(); ComPortItem nextSelect = null; Debug.WriteLine(Settings.ComPort, "RESTORE ADV"); if (Settings.ComPort != -1) { bool find = false; //尋找Port是否被移除 foreach (ComPortItem k in ComboBox_ComPort.Items) { if (k.ComID == Settings.ComPort) { nextSelect = k; find = true; break; } } //Port had been removed if (!find) { ComPortItem old = new ComPortItem(Settings.ComPort, null); old.Removed = true; ComPortList.Add(old); mainWindow.Dispatcher.Invoke(() => { ComboBox_ComPort_Refresh(); }); nextSelect = old; } } mainWindow.Dispatcher.Invoke(() => { ComboBox_ComPort.SelectedItem = nextSelect; }); }, cancellationToken: CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); }
public void UsedPortAliveCheck() { foreach (ConnectionTabData data in ComPortUsedTabList) { SerialHelper serial = ((SerialHelper)data.ConnectionObject); if (serial.Connected) { ComPortItem result = ComPortList.Find(item => (ComIdToString(item.ComID) == serial.SerialPort.PortName) & item.Removed); if (result != null) { serial.Close(); } } } }
public void ListSerialPort() { Task.Factory.StartNew(() => { ProcessPortList.Reset(); string[] SerialPortName = SerialPort.GetPortNames(); SerialPortName = SerialPortName.Distinct().ToArray(); //Array.Sort(SerialPortName); List <object[]> serialportData = new List <object[]>(); //Filiter COM Regex regex = new Regex(@"COM(\d+)(?=\D?)"); for (int k = 0; k < SerialPortName.Length; k++) { Match cmpResult = regex.Match(SerialPortName[k]); if (cmpResult.Success) { serialportData.Add(new object[] { int.Parse(cmpResult.Groups[1].ToString(), CultureInfo.InvariantCulture), "" }); } } //Get serial port more detail from Management List <ComPortItem> newPortList = new List <ComPortItem>(); var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'"); //get all port detail foreach (ManagementObject port in searcher.Get()) { // ex: Arduino Uno (COM7) string result = port.GetPropertyValue("Caption").ToString(); Match cmpResult = Regex.Match(result, @"(.+?)\(COM(\d+)(?=\D?)\)"); if (cmpResult.Success) { Match idResult = Regex.Match(port.GetPropertyValue("DeviceID").ToString(), @".+\\VID_(\w+)&PID_(\w+)\\.+"); if (idResult.Success) { int vid = int.Parse(idResult.Groups[1].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture); int pid = int.Parse(idResult.Groups[2].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture); } int ResultID = int.Parse(cmpResult.Groups[2].Value, CultureInfo.InvariantCulture); string ResultDescription = cmpResult.Groups[1].ToString(); newPortList.Add(new ComPortItem(ResultID, ResultDescription)); } } newPortList.Sort((x, y) => { return(x.ComID.CompareTo(y.ComID)); }); //Check Connection foreach (ConnectionTabData tab in ComPortUsedTabList) { SerialHelper serial = tab.ConnectionObject as SerialHelper; if (serial != null) { ComPortItem result = newPortList.Find((x) => ComIdToString(x.ComID) == serial.SerialPort.PortName); if (result != null) { result.Used = true; result.UsedTab = tab; } else { //找不到,已被移除 this.Dispatcher.InvokeAsync(() => { tab.ConnectionObject.Close(); ConnectionTabHelper tabHelper = mainWindow.ConnectionTabHelper; PageDialog dialog = new PageDialog { InfoTitle = "痾", InfoContent = String.Format(CultureInfo.InvariantCulture, "{0} 斷線惹...", DateTime.Now.ToString("[MM H:mm:ss.fff]", CultureInfo.InvariantCulture)), Tab = tab }; dialog.ButtonRedoAction = () => { tab.ReConnect(); }; tabHelper.ShowDialogOnReceiveWindow(tab, dialog.PopupDialog); }); } } } //Append to list this.Dispatcher.InvokeAsync((() => { int newIndex = -1; if (ComboBox_ComPort.SelectedIndex != -1) { int index = newPortList.FindIndex((x) => { return(x.ComID == ((ComPortItem)ComboBox_ComPort.SelectedItem).ComID); }); if (index != -1) { //找到 newIndex = index; } else { //找不到 SelectedRemovedItem = (ComPortItem)ComboBox_ComPort.SelectedItem; SelectedRemovedItem.Removed = true; if (SelectedRemovedItem != null) { newPortList.Add(SelectedRemovedItem); } newIndex = newPortList.Count - 1; } } ComPortList = newPortList; ComboBox_ComPort_Refresh(); ComboBox_ComPort.SelectedIndex = newIndex; })); ProcessPortList.Set(); }, cancellationToken: CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); }