private void CheckConnectedPortsState(object sender, ElapsedEventArgs e)
        {
            var currentlyAvailablePorts = GetAvailablePorts();

            if (IsPortsCollectionChanged(_previouslyAvailablePorts, currentlyAvailablePorts))
            {
                AvailablePortsChanged?.Invoke(this,
                                              new AvailablePortsChangedEventArgs()
                {
                    AvailablePorts = currentlyAvailablePorts
                });

                _previouslyAvailablePorts = currentlyAvailablePorts;
            }
        }
Пример #2
0
        private void CheckAvailablePorts()
        {
            if (port.IsOpen)
            {
                if (DeviceConnectionState == DeviceState.ConnectionAttempt && awaitingDeviceConfirmation < 0)
                {
                    Disconnect();
                }
                else
                {
                    if (DeviceConnectionState == DeviceState.ConnectionAttempt)
                    {
                        awaitingDeviceConfirmation--;
                    }
                    return;
                }
            }

            if (DeviceConnectionState > DeviceState.ConnectionAttempt)
            {
                Disconnect();
            }


            var newPorts = new HashSet <string>(SerialPort.GetPortNames());

            if (availablePortNames.SetEquals(newPorts))
            {
                return;
            }

            var    changedPorts = newPorts.Except(availablePortNames);
            string newPortName  = string.Empty;

            if (changedPorts.Count() > 0 && newPorts.Contains(changedPorts.First()))
            {
                newPortName = changedPorts.First();
            }

            availablePortNames = newPorts;

            // Do this only when ports change to prevent UI blocking
            availablePorts = GetFullCOMData(availablePortNames);
            AvailablePortsChanged?.Invoke(AvailablePortNames, GetNewPortIndexByPortName(availablePortNames, newPortName));
        }