public PopupDialogHostMultiTab(Frame DialogFrame, Panel ContextPanel, ConnectionTabHelper tabHelper) : base(DialogFrame, ContextPanel)
 {
     this.TabHelper = tabHelper;
 }
Esempio n. 2
0
        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);
        }