我正在使用一个通过 DATA 管道接收命令的设备(wiimote),并且只接受与命令本身一样长的命令数据包。例如,它将接受:
0x11 0x10
但它不会接受:
0x11 0x10 0x00 0x00 0x00 ... etc.
这是 windows 上的一个问题,因为 windows 上的 WriteFile() 要求传递给它的 byte[] 至少与 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() 出错。
有什么方法可以将数据传递给任意大小的设备?提前感谢您的任何帮助。