public override void Dispose()
 {
     if (_firmata != null)
     {
         _firmata.Dispose();
         _firmata = null;
     }
 }
 public FirmataTargetDevice(string name, FirmataEngine firmata)
 {
     _name = name;
     _firmata = firmata;
     Task.Run(() => InitializeAsync());
 }
Beispiel #3
0
        private async void UpdateFirmataDeviceList()
        {
            using (var releaser = await _firmataDeviceListLock.LockAsync())
            {
                var useOnlyThesePorts = new List<string>();
                if (Settings.Default.ScanCOMPorts != null && Settings.Default.ScanCOMPorts.Count > 0)
                {
                    useOnlyThesePorts = Settings.Default.ScanCOMPorts.Cast<string>().ToList();
                }

                // Treat serial ports as firmata first, deployable targets secondly
                // Deployment usually happens on USB and firmata is always found on serial,
                //   so making this presumption speeds things up.
                var serialPorts = SerialPort.GetPortNames();
                foreach (var portname in serialPorts)
                {
                    // If the com-port list is not empty, and the current port is not in that list
                    //   then skip this candidate port. The user has excluded it.
                    if (useOnlyThesePorts.Count > 0 &&
                        !useOnlyThesePorts.Any(x => x.ToLowerInvariant() == portname.ToLowerInvariant()))
                        continue;

                    // only probe if we don't already have this port registered
                    if (_devices.Any(x => x is FirmataTargetDevice && ((FirmataTargetDevice)x).DisplayName == portname))
                        continue;

                    bool probeSuccessful = false;
                    var engine = new FirmataEngine(portname);
                    try
                    {
                        await engine.ProbeAndOpen();
                        var fwVers = await engine.GetFullFirmwareVersion();
                        Debug.WriteLine("Found on {0} : App:'{1}' app version v{2} protocol version v{3}", portname, fwVers.Name, fwVers.AppVersion, fwVers.Version);
                        probeSuccessful = true;
                    }
                    catch
                    {
                        probeSuccessful = false;
                    }
                    if (probeSuccessful)
                    {
                        lock (_devices)
                        {
                            _devices.Add(new FirmataTargetDevice(portname, engine));
                        }
                    }
                }

                // Remove items that have disappeared
                foreach (var knownItem in _devices.ToArray())
                {
                    if (knownItem is FirmataTargetDevice)
                    {
                        var item = (FirmataTargetDevice)knownItem;
                        if (!serialPorts.Any(x => x == item.DisplayName))
                        {
                            lock (_devices)
                            {
                                knownItem.Dispose();
                                _devices.Remove(knownItem);
                            }
                        }
                    }
                }
            }
        }