0

我正在尝试在 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 -
4

1 回答 1

1

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

用户在使用 linux 之前必须对 USB 设备具有适当的访问权限。

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

于 2016-06-06T11:55:03.880 回答