Пример #1
0
 public MainViewModel()
 {
     _Random            = new Random();
     SerialPorts        = SerialPort.GetPortNames();
     BaudRates          = new[] { 300, 600, 1200, 2400, 9600, 14400, 19200, 38400, 57600, 115200 };
     SelectedSerialPort = SerialPorts.Where(p => p == DefaultPortName).FirstOrDefault();
     SelectedBaudRate   = BaudRates.Where(r => r == DefaultBaudRate).FirstOrDefault();
     InitialiseSerialPort();
 }
Пример #2
0
        public async Task RefreshSerialPortsAsync()
        {
            var activePorts = SerialPorts.Where(p => p.IsActive).Select(p => p.PortID).ToList();

            SerialPorts.Clear();
#if NETFX_CORE
            var serialPortSelector = Windows.Devices.SerialCommunication.SerialDevice.GetDeviceSelector();
            var devices            = await DeviceInformation.FindAllAsync(serialPortSelector);

            foreach (var item in devices)
            {
                bool isActive = activePorts.Contains(item.Id);
                SerialPorts.Add(new SerialPortInfo(item.Id, item.Name, isActive));
                if (isActive)
                {
                    activePorts.Remove(item.Id);
                }
            }
#else //.NET
            foreach (var item in System.IO.Ports.SerialPort.GetPortNames())
            {
                bool isActive = activePorts.Contains(item);
                SerialPorts.Add(new SerialPortInfo(item, item, isActive));
                if (isActive)
                {
                    activePorts.Remove(item.Id);
                }
            }
#endif
#if DEBUG
            // SerialPorts.Add(new SerialPortInfo("//COM-TEST/1", "Test Port 1"));
            // SerialPorts.Add(new SerialPortInfo("//COM-TEST/2", "Test Port 2"));
            // SerialPorts.Add(new SerialPortInfo("//COM-TEST/3", "Test Port 3"));
            // SerialPorts.Add(new SerialPortInfo("//COM-TEST/4", "Test Port 4"));
#endif

            // Remove controllers on ports that were active but lost
            foreach (var item in activePorts)
            {
                Watcher.Instance.RemoveController(item);
            }
        }