示例#1
0
        /// <summary>
        /// Open the controller
        /// </summary>
        public bool OpenDevice()
        {
            int _reconn_counter = 0;

            this.IsConnected = false;

            while (true)
            {
                try
                {
                    _reconn_counter++;

                    if (_hid_device.Connect())
                    {
                        // Start the received task on the UI thread
                        OnConnectionStatusChanged?.Invoke(this, new ConnectionEventArgs(ConnectionEventArgs.EventType.ConnectionSuccess, null));

                        // start to read hid report from usb device in the background thread
                        ////_hid_device.StartRead();

                        // to realize the mechanism of timeout, save the time when the initialization process is started
                        DateTime _init_start_time = DateTime.Now;

                        // Wait the first report from HID device in order to get the 'TotalAxes'
                        do
                        {
                            // read hid report
                            byte[] report = _hid_device.Read();
                            if (report != null)
                            {
                                this.Report.ParseRawData(report);

                                this.TotalAxes = this.Report.TotalAxes;
                            }

                            // Don't check it so fast, the interval of two adjacent report is normally 20ms but not certain
                            Thread.Sleep(100);

                            // check whether the initialization process is timeout
                            if ((DateTime.Now - _init_start_time).TotalSeconds > 5)
                            {
                                this.LastError = "unable to get the total axes";
                                break;
                            }
                        } while (this.TotalAxes <= 0);

                        // TotalAxes <= 0 indicates that no axis was found within 5 seconds, exit initialization process
                        if (this.TotalAxes <= 0)
                        {
                            break;
                        }

                        // the total number of axes returned, generate the instance of each axis
                        this.Report.AxisStateCollection.Clear();

                        // create the axis collection according the TotalAxes property in the hid report
                        for (int i = 0; i < this.TotalAxes; i++)
                        {
                            // generate axis state object to the controller report class
                            this.Report.AxisStateCollection.Add(new AxisState()
                            {
                                AxisIndex = i
                            });

                            // generate axis control on the user window
                            this.AxisCollection.Add(new Axis()
                            {
                                // set the properties to the default value
                                MaxSteps          = 15000,
                                SoftCCWLS         = 0,
                                SoftCWLS          = 15000,
                                PosAfterHome      = 0,
                                MaxSpeed          = MAX_VELOCITY,
                                AccelerationSteps = ACC_DEC_STEPS
                            });
                        }

                        // start to read the hid report repeatedly
                        _hid_device.StartRead();


                        this.IsConnected = true;

                        // initialize this.Report property on UI thread
                        OnConnectionStatusChanged?.Invoke(this, new ConnectionEventArgs(ConnectionEventArgs.EventType.TotalAxesReturned, this.TotalAxes));
                        break;
                    }
                    else
                    {
                        // pass the try-times to UI thread
                        OnConnectionStatusChanged?.Invoke(this, new ConnectionEventArgs(ConnectionEventArgs.EventType.ConnectionRetried, _reconn_counter));
                        Thread.Sleep(500);

                        // check if reaches the max re-connect times
                        if (_reconn_counter > 10)
                        {
                            this.LastError = "the initialization process was timeout";
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.LastError = ex.Message;
                    break;
                }
            }

            return(IsConnected);
        }