PORTS Forum
Ports and Interfaces => USB => Topic started by: rallysjd on March 31, 2012, 12:39:18 pm
-
I have an STmicro HID demo app that sends reports to turn on LEDs. I understand the app code and the report f/w that works this. I'm adding some Hid API command support into another app using HidD_SetOutputReport.
In the ST demo, the report is sent using WriteFile. In my app, HidD_SetOutputReport fails with error 31 (Device not working), and equivalent WriteFile fails with error 6 (Invalid handle). In both cases, and in the ST app, CreateFile is called with the same device path. In my app I'm sending the same report bytes I send in the ST app. I can call other HID functions with the devide handle and get valid results.
HidD_SetOutputReport seems to be the correct HID API call to send a report to the board. In debug, just before the HidD_SetOutputReport I can run the ST app and the board responds correctly, turning on the LEDs, and yet I get these errors.
Suggestions?
SD
-
Be sure CreateFile requests write access.
Be sure the number of bytes you're sending corresponds to the number of bytes in a valid report + 1 byte for the report ID.
Jan
-
Thanks for the saturday reply. I've pretty much duplicated the code in the ST app in my code. WRITE access, and the report is 2 bytes: report # byte and state byte. I can debug step the ST app and see what's being sent. It all looks the same as mine, but clearly there's a problem.
I'll double check...
SD
-
Well, I can now send the reports to the boards using WriteFile. HidD_SetOutputReport still fails, no idea why at present. Here's what I found.
The ST demo code was creating a Read and Write thread. CreateFile was called with the FILE_FLAG_OVERLAPPED attribute, so the ReadFile and WriteFile calls included an OVERLAPPED pointer. This makes sense, as the ST code allowed INPUTs and OUTPUTs.
I did not implement thread, just wanted to send some commands to the board. Threads will be needed later.
hHidDeviceObject = CreateFile((LPCTSTR)lpDevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ FILE_SHARE_WRITE,
NULL, //&SecurityAttributes,
OPEN_EXISTING,
0, NULL);
and later
returnValue = WriteFile(hHidDeviceObject,
&ReportBuffer,2,
&dwBytesWritten,
NULL);
This (of course, now, in hindsight) makes sense. No OVERLAPPED I/O is needed in this case.
Still don't see why HidD_SetOutputReport fails.
SD
-
What is your code for HidD_SetOutputReport?
Jan