private void AddDevice(SerialPortManager.SerialPortInfo port, DeviceModelBase device)
        {
            if (connected.ContainsKey(port))
            {
                return;
            }

            connected.Add(port, device);

            OnDeviceConnected(new DeviceEventArgs(device));
        }
        private async Task <DeviceModelBase> Connect(SerialPortManager.SerialPortInfo port, CancellationToken ct)
        {
            DeviceModelBase    device  = null;
            SerialMavlinkLayer mavlink = new SerialMavlinkLayer(port.Device);

            Msg_configuration_control configCMD = new Msg_configuration_control();

            configCMD.command = (byte)CONFIG_COMMAND.CONFIG_COMMAND_GET_VERSION;
            Msg_data_transmission_handshake handshake = new Msg_data_transmission_handshake();

            PacketReceivedEventHandler pr = (o, e) =>
            {
                if (e.Message.GetType() == typeof(Msg_configuration_version2))
                {
                    Msg_configuration_version2 msg = e.Message as Msg_configuration_version2;
                    device = new AnySense(mavlink, msg.fw_version);
                }
                else if (e.Message.GetType() == typeof(Msg_configuration_version3))
                {
                    Msg_configuration_version3 msg = e.Message as Msg_configuration_version3;

                    switch (msg.hw_version)
                    {
                    case 1:
                        device = new AnySensePro(mavlink, msg.fw_version);
                        break;

                    default:
                        break;
                    }
                }
            };

            mavlink.PacketReceived += pr;

            do
            {
                ct.ThrowIfCancellationRequested();
                mavlink.SendMessage(configCMD);
                await Task.Delay(500, ct);
            } while (device == null);

            mavlink.PacketReceived -= pr;

            if (device == null)
            {
                try{
                    mavlink.Dispose();
                }
                catch { }
            }

            return(device);
        }
        private async void StartPollDevice(SerialPortManager.SerialPortInfo port)
        {
            if (!isPolling)
            {
                return;
            }

            if (tasks.ContainsKey(port))
            {
                return;
            }

            if (filters.Count > 0)
            {
                bool match = false;
                foreach (string filter in filters)
                {
                    if (port.Description.ToLower().Contains(filter.ToLower()))
                    {
                        match = true;
                        break;
                    }
                }
                if (!match)
                {
                    return;
                }
            }


            tasks.Add(port, new CancellationTokenSource());

            try
            {
                DeviceModelBase device = await Connect(port, tasks[port].Token);

                if (device != null)
                {
                    AddDevice(port, device);
                }
            }
            catch { }

            tasks.Remove(port);
        }
 public DeviceEventArgs(DeviceModelBase device)
 {
     Device = device;
 }