Cannot read from HID device after migrating from fileapi.h ReadFile in windows to hidapi hid_read in Ubuntu

huangapple go评论141阅读模式
英文:

Cannot read from HID device after migrating from fileapi.h ReadFile in windows to hidapi hid_read in Ubuntu

问题

I have a library for communicating with a HID device. The library works fine under windows using fileapi.h and read/write with ReadFile and WriteFile. I now want to make this library platform independent so I decided to migrate to hidapi. On Ubuntu writing to the device with hid_write works fine, but I seem to struggle with hid_read_timeout. The read works, but the values I receive are not correct, i.e not the same as in Windows using fileapi.h.

I want to read some bytes into my data buffer

std::shared_ptr<unsigned char[]> data_buffer(new unsigned char[65]);

using

this->device_handle = hid_open(vid, pid, NULL);
hid_read_timeout(this->device_handle, data_buffer.get(), 65, 5000);

the read succeeds and the data consists of an array of ints so I later pairwise combine the bytes into ints

unsigned char high_byte = data_buffer[i];
unsigned char low_byte = data_buffer[i + 1];
uint16_t t = ((uint16_t)high_byte << 8) | low_byte;

This works fine in windows, and I can't figure out what I'm doing wrong. My knowledge about USB and HID protocols is very limited.

For reference in Windows, I use

this->device_handle = CreateFile(device_path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);

To create a read file handle and the read looks something like

if (!ReadFile(this->device_handle, (LPVOID)data_buffer.get(), 65, &num_bytes_read, &overlapped)){
  switch (WaitForSingleObject(read_done_handle, 3000)){
    case WAIT_OBJECT_0:
      GetOverlappedResult(read_handle, &overlapped, &num_bytes_read, false);
      break;
    ...
  }
}

Any help/tips are appreciated!

I tried switching backend from libusb to hidraw but that did not make a difference. I also tried many other different things but my knowledge is limited when it comes to HID and USB communication.

英文:

I have a library for communicating with a HID device. The library works fine under windows using fileapi.h and read/write with ReadFile and WriteFile. I now want to make this library platform independent so I decided to migrate to hidapi. On Ubuntu writing to the device with hid_write works fine, but I seem to struggle with hid_read_timeout. The read works, but the values I receive are not correct, i.e not the same as in Windows using fileapi.h.

I want to read some bytes into my data buffer

std::shared_ptr&lt;unsigned char[]&gt; data_buffer(new unsigned char[65]);

using

this-&gt;device_handle = hid_open(vid, pid, NULL);
hid_read_timeout(this-&gt;device_handle, data_buffer.get(), 65, 5000);

the read succeeds and the data consists of an array of ints so i later pairwise combine the bytes into ints

unsigned char high_byte = data_buffer[i];
unsigned char low_byte = data_buffer[i + 1];
uint16_t t = ((uint16_t)high_byte &lt;&lt; 8) | low_byte;

This works fine in windows, and i cant figure out what i'm doing wrong. My knowledge about USB and HID protocols are very limited.

For reference in Windows i use

this-&gt;device_handle = CreateFile(device_path, GENERIC_READ,  FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);

To create a read file handle and the read looks something like

if (!ReadFile(this-&gt;device_handle, (LPVOID)data_buffer.get(), 65, &amp;num_bytes_read, &amp;overlapped)){
  switch (WaitForSingleObject(read_done_handle, 3000)){
    case WAIT_OBJECT_0:
      GetOverlappedResult(read_handle, &amp;overlapped, &amp;num_bytes_read, false);
      break;
    ...
  }
}

Any help/tips are appreciated!

I tried switching backen from libusb to hidraw but that did not make a difference. I also tried many other different things but my knowledge is limited when it comes to HID and USB communication.

答案1

得分: 0

问题是在Ubuntu上使用hidapi时,从固件接收到的字节数组与Windows上的比较时发生了偏移。在Windows上,我在接收到的数组开头多了一个额外的字节!

英文:

The problem was that on Ubuntu using hidapi the array of bytes received from the firmware was shifted as when compared to Windows. On Windows I got an extra byte in the beginning of the received array!

huangapple
  • 本文由 发表于 2023年3月9日 23:15:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定