コード例 #1
0
 public void UpdateAvailablePorts()
 {
     AvailablePorts.Clear();
     foreach (string portName in SerialPortService.GetAvailableSerialPorts().OrderBy(s => s))
     {
         AvailablePorts.Add(new ComPortInfo()
         {
             PortName = portName
         });
     }
 }
コード例 #2
0
 private void RefreshAvailableSerialPort()
 {
     try
     {
         uxAvaibleSerialPorts.Items.Clear();
         var availableSerialPorts = _serialPortService.GetAvailableSerialPorts();
         if (availableSerialPorts.Count <= 0)
         {
             return;
         }
         foreach (var serialport in availableSerialPorts)
         {
             uxAvaibleSerialPorts.Items.Add(serialport);
         }
     }
     catch (Exception ex)
     {
         uxConsole.Nodes.Add(ex.Message).EnsureVisible();
     }
 }
コード例 #3
0
        public MainWindow()
        {
            InitializeComponent();

            // Initialize COM Port GUI options and add handler for COM Port changes
            cbPorts.ItemsSource             = SerialPortService.GetAvailableSerialPorts();
            SerialPortService.PortsChanged += (sender, eventArgs) => {
                cbPorts.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => {
                    cbPorts.ItemsSource = SerialPortService.GetAvailableSerialPorts();
                }));
            };

            _COMPort.DataReceived += COMPortDataReceived;

            _controller.Connected += (sender, eventArgs) => {
                btnConnectController.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => {
                    btnConnectController.Content = $"Disconnect";
                    Controller.Text = $"Controller {_controller.UserIndex}";
                    Controller.Background = Brushes.GreenYellow;
                }));
            };

            _controller.ConnectedFailed += (sender, eventArgs) => {
                MessageBox.Show("Could not connect to controller!");
            };

            _controller.Disconnected += (sender, eventArgs) => {
                btnConnectController.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => {
                    btnConnectController.Content = "Connect";
                    Controller.Text = "Not Connected";
                    Controller.Background = Brushes.Red;
                }));
                MessageBox.Show("The controller has been disconnected!");
            };

            // Add handler to call closed function upon program exit
            Closed += new EventHandler(OnMainWindowClosed);
        }