我正在使用通过DATA管道接收命令的设备(wiimote),并且只接受与命令本身一样长的命令数据包。例如,它会接受:hidapi:发送数据包小于caps.OutputReportByteLength
0x11 0x10
但它不会接受:
0x11 0x10 0x00 0x00 0x00 ... etc.
这是Windows上的问题,因为WriteFile的()的窗口上要求字节[]传递给它是至少与caps.OutputReportByteLength一样长。在Mac上,这个限制不存在,我的代码正常工作。下面是从hid.c导致此问题的代码:
/* Make sure the right number of bytes are passed to WriteFile. Windows
expects the number of bytes which are in the _longest_ report (plus
one for the report number) bytes even if the data is a report
which is shorter than that. Windows gives us this value in
caps.OutputReportByteLength. If a user passes in fewer bytes than this,
create a temporary buffer which is the proper size. */
if (length >= dev->output_report_length) {
/* The user passed the right number of bytes. Use the buffer as-is. */
buf = (unsigned char *) data;
} else {
/* Create a temporary buffer and copy the user's data
into it, padding the rest with zeros. */
buf = (unsigned char *) malloc(dev->output_report_length);
memcpy(buf, data, length);
memset(buf + length, 0, dev->output_report_length - length);
length = dev->output_report_length;
}
res = WriteFile(dev->device_handle, buf, length, NULL, &ol);
卸下上面的代码,如在评论中提到的,在一个错误的结果来自WriteFile的()。
有什么办法可以将数据传递给任意大小的设备?预先感谢您的帮助。