Author Topic: Can't have succes on ReadFile with Win 7  (Read 18235 times)

Dobaz

  • Member
  • ***
  • Posts: 23
Can't have succes on ReadFile with Win 7
« on: November 22, 2011, 06:35:21 pm »
Hello,

I have such a hard time trying to read data from my HID device.  I made some code and I could write to the device but could not use ReadFile with success.  WaitForSingle Object expires before any success (wait time is 6000).   I thought my code was the problem so I tried with Jan's VS6 C++ example but the same occurs.

I used USBTrace tool and I saw that the report is sent to the device and the device answer back with good data but ReadFile can get the data.

I suspect a win7 problem or because I work on a 64 bits platform.  Any other clues?

Thanks!

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Can't have succes on ReadFile with Win 7
« Reply #1 on: November 23, 2011, 03:32:38 pm »
ReadFile returns when one or more reports of the expected size are available. Be sure the device is returning the number of bytes in a report as defined in the HID's report descriptor. The ReadFile buffer must be at least this size + 1.

Jan

Dobaz

  • Member
  • ***
  • Posts: 23
Re: Can't have succes on ReadFile with Win 7
« Reply #2 on: November 23, 2011, 04:53:05 pm »
Thank you for your answer.  I am so lost and stock...

You mean:

   Result = ReadFile
      (ReadHandle,
      InputReport,
      Capabilities.InputReportByteLength,
      &NumberOfBytesRead,
      (LPOVERLAPPED) &HIDOverlapped);

The InputReport  buffer is much larger than the expected one..

I saw in your code example that you use a HANDLE to Write and another HANDLE to Read. I did use CreateFile with both Read and Write.

              CreateFile
         (DevicePath,
         GENERIC_WRITE | GENERIC_READ,
         FILE_SHARE_READ|FILE_SHARE_WRITE,
         (LPSECURITY_ATTRIBUTES)NULL,
         OPEN_EXISTING,
         FILE_FLAG_OVERLAPPED,
         NULL);


Is it ok to use the same handle for both read and write?

Also,  I did not specify (to be short)  that this is a composite device.  One Joystick interface and one control interface.  I am able to read continuously the joystick interface.  My problem is on the Control interface,  I can write to it  and the device answers back with good data (saw it with USBTracer) but the ReadFile makes no success and timout expires.

I know I write to the right interface because the device answer back but maybe my problem is that I don't read the right interface??

I use CreateFile with the control interface path to open the communication and then use the same handle to read and write.  Is there something wrong there,  how can I be sure that I read from the control interface?

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Can't have succes on ReadFile with Win 7
« Reply #3 on: November 23, 2011, 06:53:52 pm »
1. You can use the same handle for read and write, but each HID interface must have its own set of handles. After finding the first HID with the desired VID and PID, keep looking for a second one. It's not clear from your post if you're doing that.

This might also help:

http://www.microchip.com/forums/m570547-print.aspx

2. If the device isn't sending a number of bytes that corresponds to the number of bytes specified in its report descriptor, ReadFile will ignore the data.

Jan

Dobaz

  • Member
  • ***
  • Posts: 23
Re: Can't have succes on ReadFile with Win 7
« Reply #4 on: November 24, 2011, 08:10:47 pm »
Well thank you for your answer!

There was no correspondence between the report length in the device descriptor (8 bytes) and the IN bytes.

But I thought it was for IN Reports to be shorter or equal length than the descriptor.

Is this something that happens only on Windows or is it the same for Linux?

You are a great reference! Thank you.


Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Can't have succes on ReadFile with Win 7
« Reply #5 on: November 25, 2011, 09:54:48 am »
The host and device should send complete reports only. If the size of the data can vary, use report IDs or pad with zeros or other data.

Jan