Exemple #1
0
        public async void RequestToGetInputReport()
        {
            const Int32 readTimeout = 5000;

            String byteValue = null;

            Byte[] inputReportBuffer = null;

            Boolean success = false;

            //  If the device hasn't been detected, was removed, or timed out on a previous attempt
            //  to access it, look for the device.

            if (!_deviceHandleObtained)
            {
                _deviceHandleObtained = FindTheHid();
            }

            if (_deviceHandleObtained)
            {
                //  Don't attempt to exchange reports if valid handles aren't available
                //  (as for a mouse or keyboard under Windows 2000 and later.)

                if (!_hidHandle.IsInvalid)
                {
                    //  Read an Input report.

                    //  Don't attempt to send an Input report if the HID has no Input report.
                    //  (The HID spec requires all HIDs to have an interrupt IN endpoint,
                    //  which suggests that all HIDs must support Input reports.)

                    if (_myHid.Capabilities.InputReportByteLength > 0)
                    {
                        //  Set the size of the Input report buffer.

                        inputReportBuffer = new Byte[_myHid.Capabilities.InputReportByteLength];

                        if (_transferType.Equals(TransferTypes.Control))
                        {
                            {
                                _transferInProgress = true;

                                //  Read a report using a control transfer.

                                success             = _myHid.GetInputReportViaControlTransfer(_hidHandle, ref inputReportBuffer);
                                _transferInProgress = false;
                            }
                        }
                        else
                        {
                            {
                                _transferInProgress = true;

                                //  Read a report using interrupt transfers.
                                //  Timeout if no report available.
                                //  To enable reading a report without blocking the calling thread, uses Filestream's ReadAsync method.

                                // Create a delegate to execute on a timeout.

                                Action onReadTimeoutAction = OnReadTimeout;

                                // The CancellationTokenSource specifies the timeout value and the action to take on a timeout.

                                var cts = new CancellationTokenSource();

                                // Cancel the read if it hasn't completed after a timeout.

                                cts.CancelAfter(readTimeout);

                                // Specify the function to call on a timeout.

                                cts.Token.Register(onReadTimeoutAction);

                                // Stops waiting when data is available or on timeout:

                                Int32 bytesRead = await _myHid.GetInputReportViaInterruptTransfer(_deviceData, inputReportBuffer, cts);

                                // Arrive here only if the operation completed.

                                // Dispose to stop the timeout timer.

                                cts.Dispose();

                                _transferInProgress = false;

                                if (bytesRead > 0)
                                {
                                    success = true;
                                }
                            }
                        }
                    }
                }

                if (!success)
                {
                    CloseCommunications();
                }
            }
        }