【问题标题】:USB device not valid in MonoLibUsbUSB 设备在 MonoLibUsb 中无效
【发布时间】:2016-06-04 19:12:13
【问题描述】:

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

【问题讨论】:

    标签: c# mono libusb libusbdotnet


    【解决方案1】:

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

    用户必须对 USB 设备具有适当的访问权限才能与 linux 一起使用。

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

    【讨论】:

      猜你喜欢
      • 2019-03-11
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多