bool WaitForLongtimeOperation(int AxisIndex, byte CommandOrderExpected, out AxisStateReport AxisState)
        {
            bool     timeout      = false;
            int      lastPosition = 0;
            DateTime reqStart     = DateTime.Now;

            do
            {
                if (RequestAxisState(AxisIndex, out AxisState))
                {
                    var axisStateInReport = HidReport.AxisStateCollection[AxisIndex];
                    axisStateInReport.IsRunning   = AxisState.IsRunning;
                    axisStateInReport.IsHomed     = AxisState.IsHomed;
                    axisStateInReport.AbsPosition = AxisState.AbsPosition;
                    axisStateInReport.Error       = AxisState.Error;

                    OnReportUpdated?.Invoke(this, HidReport);

                    // Check whether the home process is alive
                    if (lastPosition != AxisState.AbsPosition)
                    {
                        reqStart     = DateTime.Now;
                        lastPosition = AxisState.AbsPosition;
                    }

                    //Trace.WriteLine(string.Format("{0:mm:ss.ffffff}\tCommandOrder in Report {1}, Desired {2} ...", DateTime.Now, AxisState.CommandOrder, CommandOrderExpected));

                    if (AxisState.CommandOrder == CommandOrderExpected)
                    {
                        // the operation is done
                        break;
                    }
                }

                // check if it's timeout
                if ((DateTime.Now - reqStart).TotalSeconds > 5)
                {
                    timeout = true;
                    break;
                }

                Thread.Sleep(20);
            } while (true);

            if (timeout)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Example #2
0
        /// <summary>
        /// rasie this event when a data pack containing up-to-date hid report is received
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnReportReceived(object sender, byte[] e)
        {
            // the first byte is report id
            int _report_id = (int)e[0];

            // report of device state
            if (_report_id == REPORT_ID_DEVICESTATE)
            {
                // copy the previous report before parsing the new report raw data
                DeviceStateReport _previous_report = this.Report.Clone() as DeviceStateReport;

                // parse the report from the up-to-date raw data
                this.Report.ParseRawData(e);

                // raise the event
                OnReportUpdated?.Invoke(this, this.Report);

                // if the state of input changes, raise the event
                for (int i = 0; i < this.Report.AxisStateCollection.Count; i++)
                {
                    if (this.Report.AxisStateCollection[i].IN_A != _previous_report.AxisStateCollection[i].IN_A)
                    {
                        OnInputIOStatusChanged?.Invoke(this, new InputEventArgs(i * 2, this.Report.AxisStateCollection[i].IN_A));
                    }

                    if (this.Report.AxisStateCollection[i].IN_B != _previous_report.AxisStateCollection[i].IN_B)
                    {
                        OnInputIOStatusChanged?.Invoke(this, new InputEventArgs(i * 2 + 1, this.Report.AxisStateCollection[i].IN_B));
                    }
                }
            }
            // report of firmware information
            else if (_report_id == REPORT_ID_FACTINFO)
            {
                buf_report_factinfo.AddRange(e);
            }
        }
        void StartObtainHidReport()
        {
            if (taskObtainDeviceState == null || taskObtainDeviceState.IsCompleted)
            {
                ctsObtainDeviceState = new CancellationTokenSource();
                ctObtainDeviceState  = ctsObtainDeviceState.Token;

                pauseObtainDeviceStateTask = new ManualResetEvent(true);

                var progressHandler = new Progress <byte[]>(value =>
                {
                    var report = HidReport.Clone() as DeviceStateReport;
                    this.HidReport.Parse(value);
                    OnReportUpdated?.Invoke(this, HidReport);

                    for (int i = 0; i < HidReport.AxisStateCollection.Count; i++)
                    {
                        if (this.HidReport.AxisStateCollection[i].IN_A != report.AxisStateCollection[i].IN_A)
                        {
                            OnInputIOStatusChanged?.Invoke(this, new InputIOEventArgs(i * 2, HidReport.AxisStateCollection[i].IN_A));
                        }

                        if (this.HidReport.AxisStateCollection[i].IN_B != report.AxisStateCollection[i].IN_B)
                        {
                            OnInputIOStatusChanged?.Invoke(this, new InputIOEventArgs(i * 2 + 1, HidReport.AxisStateCollection[i].IN_B));
                        }
                    }
                });
                var progress = progressHandler as IProgress <byte[]>;

                taskObtainDeviceState = Task.Factory.StartNew(() =>
                {
                    while (true)
                    {
                        DateTime start = DateTime.Now;
                        //Trace.WriteLine(string.Format("{0:mm:ss.ffffff}\tObtain HID report ...", start));

                        Request(EnumCommand.REQ_SYSTEM_STATE, out byte[] buff);

                        if (buff != null)
                        {
                            progress.Report(buff);
                        }

                        //Trace.WriteLine(string.Format("{0:mm:ss.ffffff}\tHID report is received, takes {1:F6}ms", DateTime.Now, (DateTime.Now - start).TotalMilliseconds));

                        /*
                         * Delay some seconds after the last operation
                         */
                        Thread.Sleep(200);
                        pauseObtainDeviceStateTask.WaitOne();

                        while ((DateTime.Now - timeLastOperation).TotalMilliseconds < 500)
                        {
                            Thread.Sleep(200);
                            ;
                        }

                        ctObtainDeviceState.ThrowIfCancellationRequested();
                    }
                }, TaskCreationOptions.LongRunning);

                //await taskObtainDeviceState;
            }
        }