我正在尝试在 MonoLibUsb(在 Linux 上)中打开 USB 设备句柄,但每次打开它时,我都会IsInvalid == true
进入设备句柄。
USB 设备绝对与 LibUsb 兼容,因为我已将它连接到我的 Windows PC 并且可以成功地使用 LibUsbDotNet 与之对话。如果我尝试在 Mono 中使用 LibUsbDotNet,则应用程序在尝试打开它时会挂起,所以我认为 LibUsbDotNet 是用于 Windows 的,而 MonoLibUsb 是用于 Mono 的(名称有点泄露)。但是,即使 MonoLibUsb 也无法正确使用该设备。
那么为什么返回的设备句柄无效呢?
代码
private void UsbInit() {
var sessionHandle = new MonoUsbSessionHandle();
var profileList = new MonoUsbProfileList();
profileList.Refresh(sessionHandle);
List<MonoUsbProfile> usbList = profileList.GetList().FindAll(MyVidPidPredicate);
foreach(MonoUsbProfile profile in usbList) {
var deviceHandle = profile.OpenDeviceHandle();
if (deviceHandle.IsInvalid) {
Console.WriteLine(string.Format("IsInvalid: {0} - {1}", MonoUsbSessionHandle.LastErrorCode, MonoUsbSessionHandle.LastErrorString));
}
}
}
private bool MyVidPidPredicate(MonoUsbProfile profile) {
if (profile.DeviceDescriptor.VendorID == 0xabcd && profile.DeviceDescriptor.ProductID == 0x1234)
return true;
return false;
}
输出
IsInvalid: Success -