USB 设备在 MonoLibUsb 中无效

USB device not valid in MonoLibUsb

我试图在 MonoLibUsb 中打开一个 USB 设备句柄(在 Linux 上),但每次我打开它时,我都会在设备句柄上得到 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 -

文档中的这一行很容易被忽略:

The user must have appropriate access permissions to the usb device before it can be used with linux.

如果我以 root 身份(或通过 sudo)启动应用程序,设备句柄将变为有效。