Exemple #1
0
        private void ReadAsync(IAsyncResult asyncResult)
        {
            if (asyncResult != null)
            {
                state = (DeviceReadState)asyncResult.AsyncState;
                //FileStream deviceStream = state.DeviceStream;
                try
                {
                    state.DeviceStream.EndRead(asyncResult);
                }
                catch (IOException)
                {
                    state.DeviceStream.Close();
                    return;
                }
                catch (InvalidOperationException)
                {
                    // fStream.Close();
                    //if (!shouldStop) MessageBox.Show("EndRead failed...InvalidOperationEx!");
                }
            }

            if (!shouldStop)
            {
                try
                {
                    asyncResult = state.DeviceStream.BeginRead(
                    state.ReadArray, 0, state.ReadArray.Length,
                    new AsyncCallback(ReadAsync), state);
                }
                catch
                {
                    shouldStop = true;
                }

                if (asyncResult != null)
                {
                    byte[] report = state.ReadArray;//new byte[state.ReadArray.Length];
                    /*
                    for (int i = 0; i < state.ReadArray.Length; i++)
                    {
                        report[i] = state.ReadArray[i];
                    }
                    */
                    receivedReport.Invoke(report);
                }
            }
        }
Exemple #2
0
        private void InitializeFileStream()
        {
            if (dev.OpenDeviceForIoctl())
            {
                using (HidPreparsedData preparsedData = new HidPreparsedData(dev.Handle))
                {
                    NativeApi.HIDP_CAPS Capabilities = new NativeApi.HIDP_CAPS();
                    NativeApi.HidP_GetCaps(preparsedData.Handle, ref Capabilities);
                    bufferSize = Capabilities.InputReportByteLength;
                }

                if (dev.OpenDeviceForRead())
                {
                    state = new DeviceReadState(new FileStream(dev.Handle, FileAccess.Read), new byte[bufferSize]);
                }
                else
                {
                    throw new Exception("Unable to open device for Read");
                }
            }
        }