Exemple #1
0
        /// <summary>
        /// Create instance of EE31 protocol class.
        /// </summary>
        /// <param name="communicationInterface">The communication layer</param>
        /// <returns></returns>
        public static IEECommProtocol GetEE31Protocol(IEECommDevice communicationInterface)
        {
            if (communicationInterface == null)
            {
                throw new ArgumentNullException("communicationInterface");
            }

            // Only create one protocol instance per interface
            lock (_protectInstances)
            {
                IEECommProtocol protItf = _protInstances.Find(p =>
                                                              (p.CommunicationInterface.InterfaceType == communicationInterface.InterfaceType &&
                                                               p.CommunicationInterface.InterfaceId == communicationInterface.InterfaceId));

                if (protItf != null)
                {
                    return(protItf);
                }

                var prot = new EE31Protocol(communicationInterface);
                _protInstances.Add(prot);

                return(prot);
            }
        }
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.");
        }