C++ libusb 无法打开 USB 设备,权限问题。NetBeans/Ubuntu

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22713834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:10:30  来源:igfitidea点击:

libusb cannot open USB device, permission isse. NetBeans/Ubuntu

c++ubuntu-12.04netbeans6.8libusb-1.0

提问by raff5184

I'm writing a C/C++ application in NetBeans based on libusb-1.0 on Ubuntu 12.04. I can get basic information from the USB device (for example, the interface description) but I am not able to open the device. The function libusb_open gives me the error:

我正在基于 Ubuntu 12.04 上的 libusb-1.0 在 NetBeans 中编写 C/C++ 应用程序。我可以从 USB 设备获取基本信息(例如,接口描述),但我无法打开设备。函数 libusb_open 给了我错误:

libusb:error [op_open] libusb couldn't open USB device /dev/bus/usb/002/003: Permission denied.
libusb:error [op_open] libusb requires write access to USB device nodes.

I understand that I need to change the permissions but I don't know how (I am a very basic Linux-user). Thank you!

我知道我需要更改权限,但我不知道如何更改(我是一个非常基本的 Linux 用户)。谢谢!

采纳答案by Mantosh Kumar

Well you can run your application in root user mode and get rid with the permission issue. However you should be aware about the consequences of running the program in root user and should not be do until you fully understand the user/group permission on UNIX based system. However if it is your test machine, i think you can do the following(for program name ./a.out):

那么您可以在 root 用户模式下运行您的应用程序并摆脱权限问题。但是,您应该了解以 root 用户运行程序的后果,并且在您完全了解基于 UNIX 的系统上的用户/组权限之前不应这样做。但是,如果它是您的测试机,我认为您可以执行以下操作(对于程序名称 ./a.out):

  1. Open the terminal
  2. Go to the directory where your program executable is present(check your netbeans creates the project and type cd "completepath).
  3. sudo ./a.out
  4. Now command prompt would ask to enter root password
  1. 打开终端
  2. 转到您的程序可执行文件所在的目录(检查您的 netbeans 创建项目并键入 cd "completepath")。
  3. 须藤./a.out
  4. 现在命令提示符会要求输入 root 密码

Now you should be able to run the program successfully.

现在您应该能够成功运行该程序。

However if you want to provide/change the permission of read/write/execute for a particular user you should try to understand about chmodcommand. You would have to change the permission to directory(/dev/bus/usb/002/) and any particular file residing under this directory. Hope this information would be useful.

但是,如果您想为特定用户提供/更改读/写/执行权限,您应该尝试了解chmod命令。您必须更改目录(/dev/bus/usb/002/)和驻留在此目录下的任何特定文件的权限。希望这些信息有用。

回答by Preston

I think the best way to do this is to create a udev rules file for your devices. Simply create a text file names something like myVendor.rulesand put the following text in it (where 1234is your vendor ID:

我认为最好的方法是为您的设备创建一个 udev 规则文件。只需创建一个类似名称的文本文件myVendor.rules并将以下文本放入其中1234(您的供应商 ID在哪里:

SUBSYSTEM=="usb", ATTRS{idVendor}=="1234", MODE="0666"
SUBSYSTEM=="usb_device", ATTRS{idVendor}=="1234", MODE="0666"

Put this udev file in your /etc/udev/rules.d/directory. This udev file will grant read and write access to ALL users, include non-privileged users, for ALL USB devices that have a matching Vendor ID. This means your device is accessible to non-root users even without modifying your executable or running it with sudo.

将此 udev 文件放在您的/etc/udev/rules.d/目录中。对于所有具有匹配供应商 ID 的 USB 设备,此 udev 文件将授予所有用户(包括非特权用户)读写访问权限。这意味着即使不修改您的可执行文件或使用 sudo 运行它,非 root 用户也可以访问您的设备。

This udev example is specific to the idVendor, but you can restrict it to a VID and PID to be more strict. Check this article for writing udevrules for more information.

此 udev 示例特定于 idVendor,但您可以将其限制为更严格的 VID 和 PID。查看此文章以了解更多信息以编写 udev规则。

回答by JaeJun LEE

I think this might be a temporary solution for the problem while Preston's solutionwould work consistently.

我认为这可能是该问题的临时解决方案,而Preston 的解决方案将始终如一地工作。

1. Check which usb port is assigned for your device

1. 检查为您的设备分配了哪个 USB 端口

You can figure out which usb port is assigned to your device by invoking lscommand two times(first with device disconnectedand second with device connected).

您可以通过ls两次调用命令来确定哪个 USB 端口分配给您的设备(第一次与设备断开连接,第二次与设备连接)。

$ ls -l /dev/bus/usb/00*
/dev/bus/usb/001:
total 0
crw-rw-r-- 1 root root 189, 0  1? 10 12:08 001
crw-rw-r-- 1 root root 189, 1  1? 10 12:08 002

/dev/bus/usb/002:
total 0
crw-rw-r-- 1 root root 189, 128  1? 10 12:08 001
crw-rw-r-- 1 root root 189, 129  1? 10 12:08 002

/dev/bus/usb/003:
total 0
crw-rw-r-- 1 root root 189, 256  1? 10 12:08 001
crw-rw-r-- 1 root root 189, 257  1? 10 12:08 002
crw-rw-r-- 1 root root 189, 258  1? 10 12:08 003
crw-rw-r-- 1 root root 189, 259  1? 10 12:08 004
crw-rw-r-- 1 root root 189, 260  1? 10 12:08 005
crw-rw-r-- 1 root root 189, 263  1? 10 15:42 008 <-- this is your device

Let's say /dev/bus/usb/003/008is your device.

假设/dev/bus/usb/003/008是您的设备。

2. Giving write permission for everyone(other)

2. 给所有人(其他)写权限

According to the output of ls -lcommand, root user(group)has read/write permission on 003/008 portwhile other userhas only read permission.

根据ls -l命令的输出,root用户(组)有读/写权限,003/008 port其他用户只有读权限。

crw-rw-r-- 1 root root 189, 263  1? 10 15:42 008

You can allow every userto write on specific device using chmodcommand. While using chmodcommand, you will need sudopermission.

您可以允许每个用户使用chmod命令在特定设备上写入。使用chmod命令时,您将需要sudo许可。

$ sudo chmod o+w /dev/bus/usb/003/008

or

或者

$ sudo chmod a+w /dev/bus/usb/003/008

Now if you check the permission of usb, you have to see this output

现在如果你检查usb的权限,你必须看到这个输出

$ ls -l /dev/bus/usb/003/008
crw-rw-rw- 1 root root 189, 263  1? 10 15:42 /dev/bus/usb/003/008

3. Everytime plugging it out or shutting down the system repeat step 1,2

3. 每次拔掉或关闭系统重复步骤1,2

If you plug the device out from usb port or shut down the system, what you did for your usb port will reset.

如果您将设备从 USB 端口拔出或关闭系统,您对 USB 端口所做的操作将重置。

You have to repeat step 1,2 again.

您必须再次重复步骤 1,2。

This is why I'm saying my solution is temporary(volatile).

这就是为什么我说我的解决方案是临时的(易变的)。

Further readings

进一步阅读

I find these two blog articles would be helpful to your understanding.

我发现这两篇博客文章对您的理解有所帮助。

回答by Andrei Emeltchenko

After adding rule to /etc/udev/rules.d/something like:

将规则添加到以下内容后/etc/udev/rules.d/

SUBSYSTEM=="usb", ATTRS{idVendor}=="1234", ATTRS{idProduct}=="1234", MODE="0666", OWNER="YOU_USER_ID"

do not forget to reload rules with:

不要忘记重新加载规则:

$ sudo udevadm control --reload-rules