Skip to content

Navigation Menu

Sign in
Sign up

Windows: hid_write returns larger number of bytes then the size of the report #255

Answered by Youw
gfisys asked this question in Q&A
Discussion options

I'm not sure if I should post this here or in 'Issues', I'm not sure sure if this is an issue, or something that I did wrong.

In my hid report descriptor I specified two reports,
Report_ID_1 has Report_Count = 15 bytes (for both in and out),
Report_ID_2 has Report_Count = 32 bytes (for both in and out).
My device is a microcontroller with USB hardware, setup for hid interface.

Writing with Report Id 1 returns 'number of bytes sent' as 33, which is the report size specified for Report Id 2.

dataBuffer[0] = 0x01; // Report Id 1
int err = hid_write(devHandle, dataBuffer, 15 + 1); // This returns err = 33.

However,
hid_read(devHandle, dataBuffer, 15 + 1)
returns the expected number of bytes, which is 16.

The transmitted and received data are correct, without errors.
I've rechecked and rechecked my descriptors and they are correct. Am I doing something wrong?

You must be logged in to vote

I'm assuming you're using latest 0.10.1 version of hidapi.

Can you try the very same code on Linux/macOS?

Your expectations are natural from HID point of view, but I'm afraid this is likely is a Windows backend limitation. And not because of the hidapi implementation on Windows, but because of the WinAPI itself. hidapi is jut strying to be a thin proxy between a user and underlying OS API, when that is possible.
WinAPI requires that the buffer passed to WriteFile (underlying implementation of hid_write) is large-enugh to hold the largest report (the size of larger report is reported by WinAPI as well, and used by hidapi). And, apparently, it doesn't return how many actual bytes are sent t...

Replies: 2 comments 1 reply

Comment options

I'm assuming you're using latest 0.10.1 version of hidapi.

Can you try the very same code on Linux/macOS?

Your expectations are natural from HID point of view, but I'm afraid this is likely is a Windows backend limitation. And not because of the hidapi implementation on Windows, but because of the WinAPI itself. hidapi is jut strying to be a thin proxy between a user and underlying OS API, when that is possible.
WinAPI requires that the buffer passed to WriteFile (underlying implementation of hid_write) is large-enugh to hold the largest report (the size of larger report is reported by WinAPI as well, and used by hidapi). And, apparently, it doesn't return how many actual bytes are sent to the device.

What is your Windows version?

You must be logged in to vote
1 reply
Comment options

Thanks for the swift reply!

Yes I'm using the latest version of hidapi and I'm on Windows 10.

I've now realized that the number of bytes sent was really 15+1 bytes, only the returned value of hid_write() was wrong, further tests revealed that the returned test is always the largest report size specified in the report descriptor.

Thanks for the explanation. It's an inconvenience but it's something that I can deal with.
I'll try it later on on macOS.

Regards,
Henry

Answer selected by mcuee
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /