编程语言
首页 > 编程语言> > python-PyUSB dev.set_configuration()

python-PyUSB dev.set_configuration()

作者:互联网

我正在尝试使用python库PyUSB将数据发送到USB记忆棒.我使用的代码如下:

import usb.core
import usb.util

# find our devices
#dev = usb.core.find(idVendor=0xfffe, idProduct=0x0001)
dev = usb.core.find(idVendor=0x090c, idProduct=0x1000)

# was it found?
if dev is None:
    raise ValueError('Device not found')

# set the active configuration. With no arguments, the first
# configuration will be the active one
dev.set_configuration()

# get an endpoint instance
cfg = dev.get_active_configuration()
interface_number = cfg[(0,0)].bInterface_number
alternate_setting = usb.control.get_interface(interface_number)
intf = usb.util.find_descriptor(
    cfg, bTnerfaceNumber = interface_number,
    bAlternateSettings = alternate_setting
)

ep = usb.util.find_descriptor(
    intf,
    # match the first OUT endpoint
    custom_match = \
    lambda e:
        usb.util.endpoint_direction(e.bEndpointAddress) == \
        usb.util.ENDPOINT_OUT
)

assert ep is not None

# write the data
ep.write('test')

但是我似乎收到以下错误:

Traceback (most recent call last):
  File "/home/usman/Desktop/c_code/libusb_test_data/pyusb_code/send_data_rev_one.py", line 14, in <module>
    dev.set_configuration()
  File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 554, in set_configuration
    self._ctx.managed_set_configuration(self, configuration)
  File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 91, in managed_set_configuration
    self.managed_open()
  File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 70, in managed_open
    self.handle = self.backend.open_device(self.dev)
  File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 494, in open_device
    _check(_lib.libusb_open(dev.devid, byref(handle)))
  File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 403, in _check
    raise USBError(_str_error[ret], ret, _libusb_errno[ret])
USBError: [Errno 13] Access denied (insufficient permissions)

有人可以引导我解决这个错误并告诉我如何解决它

谢谢

解决方法:

我只是从您正在使用Ubuntu的标签中意识到.在Ubuntu中,USB设备权限与USB设备编号相关. Ubuntu出厂时,它具有某些标准设备(例如闪存驱动器)的全局权限,以便非root用户可以使用它们.但是,它不能全局解锁USB设备,因为某些USB设备可能有害.请参阅this post,以了解如何修改Ubuntu配置(尤其是/etc/udev/rules.d/40-basic-permissions.rules),以允许非root用户使用您的设备.

标签:python-2-7,ubuntu,usb,pyusb,python
来源: https://codeday.me/bug/20191031/1974037.html