PyUSB dev.set_configuration()
Asked Answered
M

2

4

I am trying to send data to a usb stick using the python library PyUSB. The code I am using is the following:

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')

however I seem to be getting the following error:

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)

can somebody walk me through this error and tell me how to fix it

thanks

Myronmyrrh answered 25/2, 2013 at 19:0 Comment(1)
USB permissions are controlled by the underlying operating system. Are you working with Linux or Windows? If Linux, what distribution are you working on? You can always try running your application with superuser/administrator permissions but ideally you will want to avoid that if you can.Studdingsail
S
4

I just realized from the tags that you are using Ubuntu. In Ubuntu USB device permissions are tied to the USB device numbers. When Ubuntu ships it comes with global permissions for some standard devices (e.g. flash drives) so that non-root users can use these. However, it does not globally unlock USB devices because some USB devices could be harmful. Please see this post to understand how to modify the Ubuntu configuration (in particular /etc/udev/rules.d/40-basic-permissions.rules) in order to allow your device to be used by non-root users.

Studdingsail answered 26/2, 2013 at 2:21 Comment(0)
I
0

you have to do - on ubuntu:

sudo usermod -a -G uucp YOURUSER
sudo usermod -a -G dialout YOURUSER

and then

sudo echo "SUBSYSTEM==\"usb\", ATTR{idVendor}==\"YOURVENDOR\", ATTR{idProduct}==\"YOURID\", MODE=\"666\"">/etc/udev/rules.d/99-YOURDEVICENAME.rules

in Your example:

 sudo echo "SUBSYSTEM==\"usb\", ATTR{idVendor}==\"090c\", ATTR{idProduct}==\"1000\", MODE=\"666\"">/etc/udev/rules.d/99-YOURDEVICENAME.rules
Idaho answered 30/3, 2020 at 18:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.