简体   繁体   English

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

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

I'm writing a C/C++ application in NetBeans based on libusb-1.0 on Ubuntu 12.04.我正在基于 Ubuntu 12.04 上的 libusb-1.0 在 NetBeans 中编写 C/C++ 应用程序。 I can get basic information from the USB device (for example, the interface description) but I am not able to open the device.我可以从 USB 设备获取基本信息(例如,接口描述),但我无法打开设备。 The function libusb_open gives me the error:函数 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).我知道我需要更改权限,但我不知道如何更改(我是一个非常基本的 Linux 用户)。 Thank you!谢谢!

I think the best way to do this is to create a udev rules file for your devices.我认为最好的方法是为您的设备创建一个 udev 规则文件。 Simply create a text file names something like myVendor.rules and put the following text in it (where 1234 is your vendor ID:只需创建一个类似于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.将此 udev 文件放在您的/etc/udev/rules.d/目录中。 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.对于所有具有匹配供应商 ID 的 USB 设备,此 udev 文件将授予所有用户(包括非特权用户)读写访问权限。 This means your device is accessible to non-root users even without modifying your executable or running it with sudo.这意味着即使不修改您的可执行文件或使用 sudo 运行它,非 root 用户也可以访问您的设备。

This udev example is specific to the idVendor, but you can restrict it to a VID and PID to be more strict.此 udev 示例特定于 idVendor,但您可以将其限制为更严格的 VID 和 PID。 Check this article for writing udev rules for more information.查看此文章以了解更多信息以编写 udev规则。

I think this might be a temporary solution for the problem while Preston's solution would 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 ls command two times(first with device disconnected and 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/008 is your device.假设/dev/bus/usb/003/008是您的设备。

2. Giving write permission for everyone(other) 2. 给所有人(其他)写权限

According to the output of ls -l command, root user(group) has read/write permission on 003/008 port while other user has 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 user to write on specific device using chmod command.您可以使用chmod命令允许每个用户在特定设备上写入。 While using chmod command, you will need sudo permission.使用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.我发现这两篇博客文章对您的理解有所帮助。

Well you can run your application in root user mode and get rid with the permission issue.那么您可以在 root 用户模式下运行您的应用程序并摆脱权限问题。 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.但是,您应该了解以 root 用户运行程序的后果,并且在您完全了解基于 UNIX 的系统上的用户/组权限之前不应这样做。 However if it is your test machine, i think you can do the following(for program name ./a.out):但是,如果它是您的测试机,我认为您可以执行以下操作(对于程序名称 ./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 ).转到您的程序可执行文件所在的目录(检查您的 netbeans 创建的项目并键入cd completepath )。
  3. sudo ./a.out
  4. Now command prompt would ask to enter root password现在命令提示符会要求输入 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 chmod command.但是,如果您想为特定用户提供/更改读/写/执行权限,您应该尝试了解chmod命令。 You would have to change the permission to directory(/dev/bus/usb/002/) and any particular file residing under this directory.您必须更改目录(/dev/bus/usb/002/)和驻留在此目录下的任何特定文件的权限。 Hope this information would be useful.希望这些信息有用。

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

For every piece of equipment you want to communicate with you need to find out the idVendor (and idProduct if you want).对于您想要与之通信的每件设备,您都需要找出 idVendor(如果需要,还需要找出 idProduct)。 This you can do by bash command 'lsusb'.这可以通过 bash 命令“lsusb”来完成。 You get idVendor:idProduct pairs.你得到 idVendor:idProduct 对。 Example:例子:

pstallinga@mycomputer:~$ lsusb
Bus 002 Device 005: ID 5345:1234 Owon PDS6062T Oscilloscope
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 005: ID 046a:000c Cherry GmbH 
Bus 001 Device 004: ID 0480:a006 Toshiba America Inc External Disk 1.5TB
Bus 001 Device 003: ID 046a:0023 Cherry GmbH Keyboard
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

and then create a rule for it.然后为其创建规则。 The rule you place in a file that ends with '.rules' and put that file in either '/etc/udev/rules.d/' or '/lib/udev/rules.d/' directory:您放置在以“.rules”结尾的文件中的规则,并将该文件放在“/etc/udev/rules.d/”或“/lib/udev/rules.d/”目录中:

  • The rules are processed alphabetically (so 70 comes after 60, etc.)规则按字母顺序处理(所以 70 在 60 之后,等等)
  • The '/etc/udev/rules.d/' rules are processed after the '/lib/udev/rules.d/' rules '/etc/udev/rules.d/' 规则在 '/lib/udev/rules.d/' 规则之后处理

To give an example, this worked for me for Owon oscilloscopes.举个例子,这对我来说适用于 Owon 示波器。 I put this text in a file named '70-owon.rules' in directory '/lib/udev/rules.d/':我将此文本放在目录“/lib/udev/rules.d/”中名为“70-owon.rules”的文件中:

SUBSYSTEMS=="usb", ATTRS{idVendor}=="5345", ATTRS{idProduct}=="1234", MODE="0666"

and I could communicate with the oscilloscope in my C program.我可以在我的 C 程序中与示波器通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM