Author Topic: Sending report to a HID-dev via WriteFile failed with 87 errorcode  (Read 33544 times)

Shrike

  • Member
  • ***
  • Posts: 4
Hi,
I'm a new to USB/HID development.
I need to interact with a HID-device.

I've followed the way decribed in "USB Complete" book (ch.11) for finding my device and getting a handler for him.
The handler is good as I can use it with HidP_GetCaps function.

But when I try to send some bytes with calling WriteFile to my device I get 87 error (parameter is incorrect).
Frankly I don't fully understand all this concepts of report id/report types/pipes/collections etc. But I used a sniffer tool to get interaction log with my device (when it's used from some proprietary software). This tool is BusDog. I also tried USB Monitor (Device Monitoring Studio) but it's more complex.

I can see in the BusDog's log the following bytes when the device is used from proprietary software:
0b 04... and some vendor specific payload
 
Then tried to send exactly such bytes from my program:
var bytes = new byte[] { 0x0b, 0x04, 0x2f, 0x7a, 0x77, 0x0d, 0, 0 };
bool success = NativeMethods.WriteFile(m_hDevice, bytes,
                                                bytes.Length,
                                                ref numberOfBytesWritten,
                                                IntPtr.Zero);
I tried adding 0x00 byte at the begging.
I tried allocate 64-bytes array. Tried to get OutputReportByteLength from HidP_GetCaps (it equals 8).

But in all cases WriteFile fails with 87 error.

Any ideas?

update: I also tried allocate 65 byte length buffer and first zero.
« Last Edit: December 17, 2010, 12:07:00 pm by Shrike »

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Sending report to a HID-dev via WriteFile failed with 87 errorcode
« Reply #1 on: December 17, 2010, 01:19:28 pm »
What is OutputReportByteLength? How about FeatureReportByteLength? (It might be a Feature report.) The WriteFile buffer definitely needs a zero or other report ID in the first byte.

Jan

Shrike

  • Member
  • ***
  • Posts: 4
Re: Sending report to a HID-dev via WriteFile failed with 87 errorcode
« Reply #2 on: December 17, 2010, 01:31:51 pm »
FeatureReportByteLength=8
OutputReportByteLength=8
InputReportByteLength=8
Usage=1
UsagePage=64K

But inspite of GetCaps reports OutputReportByteLength=8 in sniffer I can see a report of 64 byte length.
I try both 8+1 (first zero) and 64+1 (first zero)

var data = new byte[65];
var bytes = new byte[] { 0, 0x0b, 0x04, 0x2f, 0x7a, 0x77, 0x0d, 0, 0 };
Array.Copy(bytes, data, bytes.Length);
Int32 numberOfBytesWritten = 0;
WriteFile(m_hDevice, data, data.Length, ref numberOfBytesWritten, IntPtr.Zero);

How to find out required report id for sure? I suspect BusDog tool to not show it.

UPDATE: here's the BusDog tool - http://code.google.com/p/busdog/
UPDATE2: I've attached HID descriptor

[attachment deleted by admin]
« Last Edit: December 17, 2010, 02:21:36 pm by Shrike »

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Sending report to a HID-dev via WriteFile failed with 87 errorcode
« Reply #3 on: December 17, 2010, 08:47:46 pm »
The report descriptor you posted shows 28 report IDs. Report ID zero is used only by devices that have at most one of each report type (Input, Output, Feature).

To send a report to your device, pick a report ID and send that report's quantity of data. For example, Output Report ID 2 has 7 bytes, so you would send 02h followed by seven data bytes.

Make the WriteFile buffer = the largest report size + 1.

Jan

Shrike

  • Member
  • ***
  • Posts: 4
Re: Sending report to a HID-dev via WriteFile failed with 87 errorcode
« Reply #4 on: December 20, 2010, 06:56:44 am »
Jan, thanks for your help!

But I can't get the point why I'm getting "parameter is incorrect" from WriteFile while I'm sending exactly the same sequence of data as I catch in the sniffer.

In BusDog tool (sniffer) I catch such report data: 0x0b, 0x04, 0x2f, 0x7a, 0x77, 0x0d. Ok, the first byte is report id = 0x0b. There's such report type in HID descriptor I attached ealier. Then the rest bytes is payload. In the sniffer I can see sending of 64 bytes, but HID descriptor for 0x0b report type says "report size=8, report count 63".

Actualy the device I'm trying to work with is a custom solution (it's a device with rs232 interface connected via custom controller which emulates HID interfaces to the OS), I just need to send them some commands and not very care about its descriptors (if it's not required to work with it of cause!).

I'm calling WriteFile with the showed above sequence in the buffer of size 64 and get "parameter is incorrent" without touching the bus (there's no any activity in the sniffer I mean).

In another sniffer (HHD Software Device Monitoring Studio) I catched more info with "Packet view" tool. The command I see in BusDog ("/zw") is sending to "USB_FUNCTION_BULK_OR_INTERUPT_TRANSFER" function, but before I can see other output packets with function "USB_FUNCTION_CLASS_INTERFACE". I've attached log with all data.

Should I do something to reproduce these comunications with function "USB_FUNCTION_CLASS_INTERFACE" in my application (or this's just OS HID driver's activity)?


[attachment deleted by admin]

Shrike

  • Member
  • ***
  • Posts: 4
Re: Sending report to a HID-dev via WriteFile failed with 87 errorcode
« Reply #5 on: December 20, 2010, 09:15:58 am »
Ok, I've sorted out with "parameter is incorrect". The error was caused by the fact how I opened the handle to device. I opened it with FILE_FLAG_OVERLAPPED flag but tried to write synchronously.

So my question now is only about USB_FUNCTION_CLASS_INTERFACE: should (and how) I reproduce these kind communication in my program?
Thanks.

DavidClarke

  • Member
  • ***
  • Posts: 24
Re: Sending report to a HID-dev via WriteFile failed with 87 errorcode
« Reply #6 on: May 03, 2019, 05:33:42 pm »
Hi
How exactly did you change from "write synchronously" to a synchronously?
Do you have an example of what you did wrong and then how you did it right?

Thanks
David

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Sending report to a HID-dev via WriteFile failed with 87 errorcode
« Reply #7 on: May 03, 2019, 08:50:35 pm »
Asynchronous uses overlapped transfers, synchronous doesn't.

https://docs.microsoft.com/en-us/windows/desktop/fileio/synchronous-and-asynchronous-i-o