Ejemplo n.º 1
0
        public async Task <IEnumerable <ConnectedDeviceDefinition> > GetConnectedDeviceDefinitionsAsync(CancellationToken cancellationToken = default)
        {
#if NETSTANDARD
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                throw new ValidationException(Messages.ErrorMessageOperationNotSupportedOnPlatform);
            }
#endif

            var returnValue = new List <ConnectedDeviceDefinition>();

            //TODO: Logging

            var registryAvailable = false;

            try
            {
                using var key = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM");
                if (key != null)
                {
                    registryAvailable = true;

                    //We can look at the registry

                    var valueNames = key.GetValueNames();

                    returnValue.AddRange(from valueName in valueNames let comPortName = key.GetValue(valueName) select new ConnectedDeviceDefinition($@"\\.\{comPortName}", DeviceType.SerialPort, label: valueName));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
            }

            if (registryAvailable)
            {
                return(returnValue);
            }

            //We can't look at the registry so try connecting to the devices
            for (var i = 0; i < 9; i++)
            {
                var portName = $@"\\.\COM{i}";
                using var serialPortDevice = new WindowsSerialPortDevice(portName);
                await serialPortDevice.InitializeAsync(cancellationToken).ConfigureAwait(false);

                if (serialPortDevice.IsInitialized)
                {
                    returnValue.Add(new ConnectedDeviceDefinition(portName, DeviceType.SerialPort));
                }
            }

            return(new ReadOnlyCollection <ConnectedDeviceDefinition>(returnValue));
        }
Ejemplo n.º 2
0
        public async Task <IEnumerable <ConnectedDeviceDefinition> > GetConnectedDeviceDefinitionsAsync(FilterDeviceDefinition deviceDefinition)
        {
#if NETSTANDARD
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                throw new ValidationException(Messages.ErrorMessageOperationNotSupportedOnPlatform);
            }
#endif

            var returnValue = new List <ConnectedDeviceDefinition>();

            //TODO: Logging

            var registryAvailable = false;

            try
            {
                using (var key = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM"))
                {
                    if (key != null)
                    {
                        registryAvailable = true;

                        //We can look at the registry

                        var valueNames = key.GetValueNames();

                        foreach (var valueName in valueNames)
                        {
                            var comPortName = key.GetValue(valueName);
                            returnValue.Add(new ConnectedDeviceDefinition($@"\\.\{comPortName}")
                            {
                                Label = valueName
                            });
                        }
                    }
                }
            }
            catch
            {
                //TODO: Logging
            }

            if (!registryAvailable)
            {
                //We can't look at the registry so try connecting to the devices
                for (var i = 0; i < 9; i++)
                {
                    var portName = $@"\\.\COM{i}";
                    using (var serialPortDevice = new WindowsSerialPortDevice(portName))
                    {
                        await serialPortDevice.InitializeAsync();

                        if (serialPortDevice.IsInitialized)
                        {
                            returnValue.Add(new ConnectedDeviceDefinition(portName));
                        }
                    }
                }
            }

            return(returnValue);
        }