How can I comunicate with this device using pyusb?

泪湿孤枕 提交于 2019-12-20 02:59:15

问题


I have a Netware uniFlow device. When I plug it in it shows up in dmesg:

[ 2962.369905] usb 2-1.4: new full-speed USB device number 11 using ehci-pci
[ 2962.463867] usb 2-1.4: New USB device found, idVendor=171b, idProduct=2001
[ 2962.463871] usb 2-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 2962.463874] usb 2-1.4: Product: USB Keyboard
[ 2962.463876] usb 2-1.4: Manufacturer: RFIDeas
[ 2962.465361] input: RFIDeas USB Keyboard as /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.0/0003:171B:2001.0008/input/input17
[ 2962.465481] hid-generic 0003:171B:2001.0008: input,hidraw3: USB HID v1.10 Keyboard [RFIDeas USB Keyboard] on usb-0000:00:1d.0-1.4/input0

It also appears in the listed USB devices, with the lsusb command:

Bus 002 Device 011: ID 171b:2001

Correct me if I am wrong here, but I think I need to unbind the device, so that I may use pyusb to to communicate with the device. When I try to run my code, I get resource busy errors.

So then I cd'd down to /sys/bus/usb/devices/usb2/2-1/2-1.4 (taken form dmesg) and then tried to unbind the device using

sudo sh -c echo "2-1.4:1.0 > unbind"

But I got permission denied. So I logged in as root, but I still got permission denied.

Here is my source code that I am trying to use to communicate with the device:

#!/usr/bin/env python
import usb.core
import usb.util
import sys

# Find our device from lsusb
# Vendor : Product     171b:2001
dev = usb.core.find(idVendor=0x171b, idProduct=0x2001)

# 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()

    # Let's fuzz around!

    # Let's start by reading one byte from the device using different requests
    # bRequest is a byte so there are 255 different values
for bRequest in range(255):
    try:
        ret = dev.ctrl_transfer(0xC0, bRequest, 0, 0, 1)
        print "bRequest ",bRequest
        print ret
    except:
       # Failed to get data for this request
         pass

And here are the errors, I get when I try to run the program:

$ sudo python usbhax.py
Traceback (most recent call last):
  File "usbhax.py", line 16, in <module>
    dev.set_configuration()
  File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 799, in set_configuration
    self._ctx.managed_set_configuration(self, configuration)
  File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 128, in managed_set_configuration
    self.backend.set_configuration(self.handle, cfg.bConfigurationValue)
  File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 730, in set_configuration
    _check(self.lib.libusb_set_configuration(dev_handle.handle, config_value))
  File "/usr/local/lib/python2.7/dist-packages/usb/backend/libusb1.py", line 552, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 16] Resource busy

What am I doing wrong? What can I do to communicate with this device?

Yes, I have libusb installed, and pyusb. I am working from a Ubuntu environment.


回答1:


In my experience, you have to have set up a rules file for the device for the udev system

  1. Something along the lines of:

    ACTION=="add", SUBSYSTEMS=="usb", ATTRS{idVendor}=="171b", ATTRS{idProduct}=="2001", MODE="660", GROUP="plugdev"
    

    in a file called /lib/udev/rules.d/50-YourSoftwareName.rules (for example, dig around in man udev for file naming rules but 50-usbhax.rules should do)

  2. Add your username to the plugdev group

    adduser username plugdev
    
  3. Reload rules

    sudo udevadm control --reload (that is minus minus reload)
    sudo udevadm trigger

  4. Unplug and replug the device or reboot your machine and see if it works.

NOTE: Some systems use the group input rather than plugdev, so edit the above accordingly.
To check what your system uses have a look at the group name for the relevant entries in /dev/usb or /dev/input



来源:https://stackoverflow.com/questions/31992058/how-can-i-comunicate-with-this-device-using-pyusb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!