Exemple #1
0
        /// <summary>
        /// Starts scanning for devices in background. Attach to DeviceFound event to be notified
        /// about new devices. Scan exceptions might be received through ScanException event.
        /// ATTENTION: Device connections are closed prior to scanning.
        /// </summary>
        public void StartScan(IScanConfiguration config)
        {
            if (config is ScanConfigurationEmulation scanConfiguration)
            {
                lock (_foundDevices)
                {
                    int nr = 0;
                    foreach (EmulationSettings settings in scanConfiguration.EmulationDevicesSettings)
                    {
                        // Add emulated device
                        IEECommDevice emulatedDevice = new EECommDeviceEmulation(nr++, settings);
                        if (_foundDevices.FirstOrDefault(x => x.InterfaceType == InterfaceType.Emulation && x.InterfaceId == emulatedDevice.InterfaceId) == null)
                        {
                            _foundDevices.Add(emulatedDevice);

                            // Fire found device event
                            FireDeviceFound(emulatedDevice);
                        }
                    }
                }
            }
            if (config is ScanConfigurationBLE scanConfigurationBLE)
            {
                StartScanBLE(scanConfigurationBLE);
            }
            if (config is ScanConfigurationUART scanConfigurationUART)
            {
                StartScanUART(scanConfigurationUART);
            }
        }
Exemple #2
0
        public IEECommDevice TryGetDevice(IScanConfiguration config, ushort busAddr)
        {
            if (config is ScanConfigurationUART scanConfigurationUART)
            {
                if (busAddr == 0)
                {
                    throw new ArgumentException("Device bus address must not be 0", "busAddr");
                }

                IEECommDevice device = null;

                // Search in already existing devices before starting a new scan
                lock (_foundDevices)
                {
                    device = _foundDevices.FirstOrDefault(x => x.InterfaceType == InterfaceType.UART && x.InterfaceId == busAddr.ToString());
                }

                if (device != null)
                {
                    // Try to revitalize device
                    IEECommProtocol  protEE31 = EECommProtocolFactory.GetEE31Protocol(device);
                    IEECommandResult result   = null;

                    // Force silence
                    Thread.Sleep(50);

                    // For non-EE31 devices (Modbus, etc.): Send protocol switch command to make them speak EE31 (again)
                    ushort discoveryId = DiscoveryCmdParams.BuildNewDiscoveryId();
                    try
                    {
                        result = protEE31.ExecuteCommand(EE31Command.Discovery, new DiscoveryCmdParams(discoveryId, busAddr, 0, 100));
                    }
                    catch { }

                    try
                    {
                        result = protEE31.ExecuteCommand(EE31Command.Discovery, new DiscoveryCmdParams(discoveryId, busAddr, 0, 50));
                    }
                    catch { }

                    if (result != null && result.Code == EECmdResultCode.Success)
                    {
                        return(device);
                    }
                }

                return(device);
            }
            else if (config is ScanConfigurationEmulation scanConfigurationEmulation)
            {
                IEECommDevice emulatedDevice = new EECommDeviceEmulation(0, scanConfigurationEmulation.EmulationDevicesSettings.ElementAt(0));
                var           existing       = _foundDevices.FirstOrDefault(x => x.InterfaceType == InterfaceType.Emulation && x.InterfaceId == emulatedDevice.InterfaceId);
                if (existing == null)
                {
                    existing = emulatedDevice;
                }

                return(existing);
            }

            throw new NotImplementedException("Currently it is only possible to get UART or emulated devices.");
        }