Exemple #1
0
        private void Connection_ConnectionUnavailible(object sender, ConnectionEventArgs e)
        {
            //send events
            PortScanned?.Invoke(this, new PortScannedEventArgs()
            {
                Port = e.Port, IsRemoved = true
            });

            //remove from the arm list
            Arm arm;

            if (!_arms.TryRemove(e.Port, out arm))
            {
                return;
            }

            //close connection
            try
            {
                arm.Communication.Disconnect();
            }
            catch { }

            //update current arm
            if (CurrentArm == arm)
            {
                CurrentArm = (_arms.Count != 0) ? _arms.First().Value : null;
            }
        }
Exemple #2
0
 protected virtual void OnPortScanned(PortScannedArgs e)
 {
     PortScanned?.Invoke(this, e);
 }
Exemple #3
0
        private void Connection_ConnectionAvailible(object sender, ConnectionEventArgs e)
        {
            //send scanning event
            if (_scanningCount == 0)
            {
                ScanningChanges?.Invoke(this, new ScanningChangesEventArgs()
                {
                    Scanning = true
                });
            }
            _scanningCount++;

            Task.Run(async() =>
            {
                var portScan = new PortScannedEventArgs()
                {
                    Port = e.Port, PossibleArm = e.PossibleArm
                };
                Arm addArm = null;

                //open the connection
                var arm = new Arm(e.Communication);
                try
                {
                    e.Communication.Connect(e.Port);
                }
                catch (CommunicationException ex)
                {
                    portScan.ErrorMessage = ex.Message;
                }
                catch { }
                if (e.Communication.IsConnected)
                {
                    portScan.CanOpen = true;

                    //ping
                    if (!e.ExistingDevice)
                    {
                        await Task.Delay(2000); //wait 2 seconds after plug in to load usb driver - bug fix
                    }
                    var ping = await arm.PingAsync();
                    //try one more time
                    if (!ping)
                    {
                        await Task.Delay(2000);
                        ping = await arm.PingAsync();
                    }
                    if (!ping)
                    {
                        e.Communication.Disconnect();
                    }
                    else
                    {
                        portScan.HasArm = true;
                        portScan.Arm    = arm;

                        //get all settings
                        await arm.LoadSettingsAsync();

                        //reload the arm if the servo count should be diffrent
                        if (arm.Settings != null)
                        {
                            if (arm.Settings.ActiveServos.HasValue && arm.Settings.ActiveServos.Value != arm.Servos.Count)
                            {
                                arm = new Arm(arm.Communication, arm.Settings.ActiveServos.Value);
                                await arm.LoadSettingsAsync();
                            }

                            portScan.Model    = arm.Settings.ModelNumber;
                            portScan.NickName = arm.Settings.NickName;

                            //add the Arm
                            addArm = arm;
                        }
                        else
                        {
                            portScan.NeedsSettingsReset = true;
                            portScan.Arm.Communication.Disconnect();
                        }
                    }
                }

                //send port scanned event
                PortScanned?.Invoke(this, portScan);

                //add to the arm list
                if (addArm != null)
                {
                    _arms.TryAdd(e.Port, addArm);
                    if (CurrentArm == null)
                    {
                        CurrentArm = addArm;
                    }
                }

                //send finished scanning event
                _scanningCount--;
                if (_scanningCount == 0)
                {
                    ScanningChanges?.Invoke(this, new ScanningChangesEventArgs()
                    {
                        Scanning = false
                    });
                }
            });
        }