Ejemplo n.º 1
0
        protected override void SortChildrenAfterLoadLocked(List <MeteringNode> Children)
        {
            base.SortChildrenAfterLoadLocked(Children);

            if (Children != null)
            {
                UsbState State = Module.GetState(this.portName);
                if (State != null)
                {
                    MeteringNode[] Children2 = Children.ToArray();

                    Task.Run(() =>
                    {
                        lock (State.Pins)
                        {
                            foreach (INode Node in Children2)
                            {
                                if (Node is Pin Pin)
                                {
                                    State.Pins[Pin.PinNrStr] = Pin;
                                }
                            }
                        }
                    });
                }
            }
        }
Ejemplo n.º 2
0
        public override Task DestroyAsync()
        {
            Task Result = base.DestroyAsync();

            if (this.Parent is UsbConnectedDevice UsbConnectedDevice)
            {
                UsbState State = Module.GetState(UsbConnectedDevice.PortName);
                State?.RemovePin(this.PinNrStr, this);
            }

            return(Result);
        }
Ejemplo n.º 3
0
        public override async Task <bool> RemoveAsync(INode Child)
        {
            bool Result = await base.RemoveAsync(Child);

            UsbState State = Module.GetState(this.portName);

            if (State != null && Child is Pin Pin)
            {
                State.RemovePin(Pin.PinNrStr, Pin);
            }

            return(Result);
        }
Ejemplo n.º 4
0
        public override Task AddAsync(INode Child)
        {
            Task Result = base.AddAsync(Child);

            UsbState State = Module.GetState(this.portName);

            if (State != null && Child is Pin Pin)
            {
                State.AddPin(Pin.PinNrStr, Pin);
            }

            return(Result);
        }
Ejemplo n.º 5
0
        private async void Init(ManualResetEvent Done)
        {
            try
            {
                DeviceInformationCollection Devices = await UsbSerial.listAvailableDevicesAsync();

                UsbState State;

                foreach (DeviceInformation DeviceInfo in Devices)
                {
                    if (!DeviceInfo.IsEnabled)
                    {
                        continue;
                    }

                    lock (serialPorts)
                    {
                        State = new UsbState()
                        {
                            Name = DeviceInfo.Name,
                            DeviceInformation = DeviceInfo,
                            SerialPort        = new UsbSerial(DeviceInfo)
                        };

                        State.SerialPort.ConnectionEstablished += State.SerialPort_ConnectionEstablished;

                        State.Device                         = new RemoteDevice(State.SerialPort);
                        State.Device.DeviceReady            += State.Device_DeviceReady;
                        State.Device.AnalogPinUpdated       += State.Device_AnalogPinUpdated;
                        State.Device.DigitalPinUpdated      += State.Device_DigitalPinUpdated;
                        State.Device.DeviceConnectionFailed += State.Device_DeviceConnectionFailed;
                        State.Device.DeviceConnectionLost   += State.Device_DeviceConnectionLost;

                        serialPorts[DeviceInfo.Name] = State;
                    }

                    State.SerialPort.begin(57600, SerialConfig.SERIAL_8N1);
                }
            }
            catch (Exception ex)
            {
                Log.Critical(ex);
            }
            finally
            {
                Done.Set();
            }
        }
Ejemplo n.º 6
0
        public void Stop()
        {
            UsbState[] States;

            lock (serialPorts)
            {
                States = new UsbState[serialPorts.Count];
                serialPorts.Values.CopyTo(States, 0);
                serialPorts.Clear();
            }

            foreach (UsbState State in States)
            {
                State.Dispose();
            }
        }
Ejemplo n.º 7
0
        public Task Stop()
        {
            UsbState[] States;

            lock (serialPorts)
            {
                States = new UsbState[serialPorts.Count];
                serialPorts.Values.CopyTo(States, 0);
                serialPorts.Clear();
            }

            foreach (UsbState State in States)
            {
                State.Dispose();
            }

            return(Task.CompletedTask);
        }