Author Topic: HID - Sending OUT Report while IN Report is Waiting  (Read 23883 times)

Victor M

  • Member
  • ***
  • Posts: 11
Re: HID - Sending OUT Report while IN Report is Waiting
« Reply #15 on: September 13, 2018, 11:28:39 am »
Jan,

In the link you gave it states "To use this option, you should turn on the asynchronous option (with the option useAsync: true) when creating an instance of the FileStream class. "

But as I mentioned before, when I set the useAsync option true, I get a runtime error - "Additional information: Handle does not support asynchronous operations. The parameters to the FileStream constructor may need to be changed to indicate that the handle was opened synchronously (that is, it was not opened for overlapped I/O)."

This seems to say there is something about the file handle that is incompatible with asynchronous (overlapped) operation. I have no idea what do do about that.

Victor M

  • Member
  • ***
  • Posts: 11
Re: HID - Sending OUT Report while IN Report is Waiting
« Reply #16 on: September 24, 2018, 10:56:31 am »
Jan,
In searching for an answer to this, I came across https://github.com/sky8273/GenericHid . This code is primarily yours from an earlier release, 5.0. In it I noticed the code for creating the file handle:

Code: [Select]
hidHandle = FileIo.CreateFile(myDevicePathName, FileIo.GenericRead | FileIo.GenericWrite, FileIo.FileShareRead | FileIo.FileShareWrite, IntPtr.Zero, FileIo.OpenExisting, FileIo.FileFlagOverlapped, IntPtr.Zero);
The flag 'FileFlagOverlapped' is used which is not in the current version 6.2. I had to add a definition for it in FileIODeclarations.cs -

Code: [Select]
internal const Int32 FileFlagOverlapped = 0X40000000;
Then I was able to create the FIleStream object with the Async parameter set to 'true', and it compiles and runs okay.

Code: [Select]
_deviceData = new FileStream(_hidHandle, FileAccess.Read | FileAccess.Write, _myHid.Capabilities.InputReportByteLength, true);
FileStream.ReadAsync() is used to read IN reports, and is called repeatedly as IN reports are received - so it is always running. ReadTimeout is set to -1, so there is no timeout. The HID device can send an IN report at any time and it will be received.

In the meantime an OUT report can be sent at any time (with button click for instance) using FileStream.WriteAsync(). It is not blocked.

There is one problem however - for some unknown reason the first OUT report will not be sent until a second OUT report is written. The host driver code seems to buffer the the first report, and both reports are sent only when the second one is written. This behavior continues, i.e. a third report will be buffered until a fourth report is written, etc.

This problem does not occur however if OUT reports are sent synchronously using FileStream.Write().

Do you know what causes this behavior?

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: HID - Sending OUT Report while IN Report is Waiting
« Reply #17 on: September 24, 2018, 11:30:52 am »
Thanks for continuing to dig into this and reporting what you've found.

WriteAsync always sends two reports at once?

Victor M

  • Member
  • ***
  • Posts: 11
Re: HID - Sending OUT Report while IN Report is Waiting
« Reply #18 on: September 24, 2018, 03:21:24 pm »
Quote
WriteAsync always sends two reports at once?

Yes (and no), One other thing I noticed - when an OUT report is written (but not sent), and later an IN report is received, then the OUT report will be sent. So it's another way a 'stuck' OUT report gets 'unstuck', strange behavior.

Note: this occurs only when the 'FileFlagOverlapped' option is used and OUT reports are written asynchronous. I wanted to use overlapped IO so I could receive unsolicited IN reports as soon as they occur, and still be able to send OUT reports at any time. Sending OUT reports synchronously seems to be a solution.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: HID - Sending OUT Report while IN Report is Waiting
« Reply #19 on: September 25, 2018, 05:39:05 am »
Due to buffering perhaps. You're right that sending OUT reports synchronously is a workaround since those generally don't result in a delay.