示例#1
0
        private static string Console_InitializePortName()
        {
            int cursorTop = Console.CursorTop;

            string[] portNames = InterfaceCommunicationSettings.GetAvailablePortNames();
            Console.WriteLine("Port Name:");
            for (int i = 0; i < portNames.Length; i++)
            {
                Console.WriteLine(string.Format("{0} - {1}", i, portNames[i]));
            }
            Console.Write("Selection (confirm with ENTER): ");
            string portNameIndexTxt = Console.ReadLine();

            if (int.TryParse(portNameIndexTxt, out int portNameIndex))
            {
                if (portNameIndex < portNames.Length)
                {
                    Console.WriteLine(string.Format("\t{0} selected", portNames[portNameIndex]));
                    return(portNames[portNameIndex]);
                }
            }

            //Selection failed
            Console.SetCursorPosition(0, cursorTop);
            return(Console_InitializePortName());
        }
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     //Get port names and populate ComboBox
     string[] portNames = InterfaceCommunicationSettings.GetAvailablePortNames();
     if (portNames != null)
     {
         if (portNames.Length > 0)
         {
             foreach (string device in portNames)
             {
                 comboBox_PortSelect.Items.Add(device);
             }
             comboBox_PortSelect.IsEnabled = true;
             button_OpenPort.IsEnabled     = true;
         }
     }
 }