我正在开发一个使用微处理器模拟 USB 复合设备的项目,该设备由 HID 键盘和 HID 鼠标组成。我的设备在我的 Windows 7 x64 和 Raspbian 主机上正确枚举和运行,一切看起来都不错,但我遇到的问题是让我的 winforms 应用程序(使用 HidSharp)打开连接的复合设备,以便我可以获取键盘端点中的原始数据。
问题似乎出在 TryOpen() 函数上,因为我可以通过匹配 VID 和 PID 找到连接的设备,我分配设备信息和报告描述符,但是当我尝试通过 TryOpen() 打开数据流时它失败了,我不知道为什么。不幸的是,该函数只返回一个 bool 值,所以我不知道为什么它失败,只是它无法打开数据流。我想知道打开一个我不知道的复合设备是否有一些有趣的事情?我用于查找设备并打开数据流的代码如下:
/*These vars are part of the class*/
byte[] keyboardBuffer; //EP1
HidSharp.Reports.Input.HidDeviceInputReceiver InputReceiver;
HidSharp.Reports.ReportDescriptor KeyboardRptDescriptor;
HidStream KeyboardStream;
HidDevice KeyboardDevice;
private void FindDevice()
{
var list = DeviceList.Local;
var stopwatch = Stopwatch.StartNew();
var hidDeviceList = list.GetHidDevices().ToArray();
foreach (HidDevice d in hidDeviceList)
{
if (d.VendorID == 0x0000 && d.ProductID == 0xA0A0)
{
/*Proper VID and PID Found*/
if (d.GetProductName() == "Keyboard")
{
KeyboardDevice = d;
KeyboardRptDescriptor = KeyboardDevice.GetReportDescriptor();
}
}
}
if (KeyboardDevice != null)
{
/*Device Found, open the datastream*/
if (KeyboardDevice.TryOpen(out KeyboardStream)) //PROBLEM LINE - Always False?
{
KeyboardReport = KeyboardRptDescriptor.InputReports.FirstOrDefault();
keyboardBuffer = new byte[KeyboardDevice.GetMaxInputReportLength()];
InputParser = KeyboardReport.DeviceItem.CreateDeviceItemInputParser();
InputReceiver = KeyboardRptDescriptor.CreateHidDeviceInputReceiver();
InputReceiver.Received -= new EventHandler(HidInputReceived);
InputReceiver.Received += new EventHandler(HidInputReceived);
InputReceiver.Start(KeyboardStream);
} else {
rtb_hidLog.AppendText("Unable to connect to device\r\n");
}
}
else
{
rtb_hidLog.AppendText("No Device Found\r\n");
}
}
现在我只是尝试从 HID 键盘读取数据,一旦我整理好键盘就会添加鼠标。找到设备似乎没有问题,但为什么打开它却出现这样的问题?我的 HIDSharp 库似乎是 v2.0.2.0(根据文件属性)。
预先感谢您的任何建议!
最佳答案
所以我在 HIDSharp 论坛上询问了这个问题,我得到了 answer from the dev :
事实证明,Windows 不允许您作为安全“功能”打开 HID 键盘设备,因此 HIDSharp 始终无法打开 HID 键盘设备的数据流。
关于c# - 为什么我无法使用 HIDSharp 连接到 USB 复合设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56296048/