예제 #1
0
 public override void Dispose()
 {
     if (_firmata != null)
     {
         _firmata.Dispose();
         _firmata = null;
     }
 }
예제 #2
0
        private async void UpdateFirmataDeviceList()
        {
            // return immediately if a scan is already in progress
            if (1 == Interlocked.CompareExchange(ref _firmataScanInProgress, 1, 0))
            {
                return;
            }

            try
            {
                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);
                            }
                        }
                    }
                }
            }
            finally
            {
                Interlocked.Exchange(ref _firmataScanInProgress, 0);
            }
        }
예제 #3
0
 public FirmataTargetDevice(string name, FirmataEngine firmata)
 {
     _name    = name;
     _firmata = firmata;
     Task.Run(() => InitializeAsync());
 }