I have an application that communicates to a PIC controller via its USB port using USB HID protocol. The application code is written in C#, .NET 3.5 Framework, VS2010, and is based on your generic_hid_cs example code.
The application runs and sends a command to the PIC at least once a minute. The application usually runs for a number of hours without a problem. However, the application eventually gets a "HID Write Output Failure", and the Win32 Last Error Code is 31, which means "A device attached to the system is not functioning." It takes 12-15 hours for this error to occur. I have tried to google this error with respect to HID / USB to no avail. I am trying to determine 1) why this error occurs, and 2) what the application can do to recover (or should something be done in the firmware?).
The application code opens both a read handle and a write handle to the usb:
readHandle = FileIO.CreateFile(myDevicePathName, FileIO.GENERIC_READ, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, IntPtr.Zero);
writeHandle = FileIO.CreateFile(myDevicePathName, FileIO.GENERIC_WRITE, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, IntPtr.Zero);
The application code writes to the HID using OutputReportViaInterruptTransfer - however I believe the application code creates a single object of this type for the entire duration of the application and simply continues to call Write():
success = FileIO.WriteFile(writeHandle, outputReportBuffer, outputReportBuffer.Length, ref numberOfBytesWritten, IntPtr.Zero);
I tried a test where after the WriteFile failed, and the error code 31 was logged, then the writeHandle did not get closed. However the application code was then "hung" anytime it tried to write to the HID.
Interestingly enough, the readHandle is still intact - we use it to read the HID and generate events that the application code receives. This still works after the write HID failure.
The application code runs on a specific device, based on Windows XP platform. There is no debugger installed on the target device (not sure how useful that would be anyway since it takes many hours to reproduce the problem). I am sure there is other information I should give, but that's all I can think of for now.
I am going to try another test to reinitialize the write error when an error occurs, but since it takes a long time to see if that works, I thought I would post a question here in the meantime to see if I can learn any additional information.