如何使用pyusb与此设备通信?

时间:2015-08-13 15:08:05

标签: python linux usb libusb

我有一台Netware uniFlow设备。当我插入它时,它显示在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

它也出现在列出的USB设备中,带有lsusb命令:

Bus 002 Device 011: ID 171b:2001

如果我错了,请纠正我,但我认为我需要取消绑定设备,以便我可以使用pyusb与设备进行通信。当我尝试运行我的代码时,我遇到了资源繁忙的错误。

然后我转到/sys/bus/usb/devices/usb2/2-1/2-1.4(采用dmesg形式),然后尝试使用

取消绑定设备
sudo sh -c echo "2-1.4:1.0 > unbind"

但我得到了许可否认。所以我以root身份登录,但我仍然被拒绝了。

以下是我尝试用来与设备通信的源代码:

#!/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

以下是我尝试运行程序时出现的错误:

$ 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

我做错了什么?如何与此设备通信?

是的,我安装了libusb,还有pyusb。我在Ubuntu环境中工作。

1 个答案:

答案 0 :(得分:2)

根据我的经验,您必须为udev系统设备设置规则文件

  1. 以下内容:

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

    在名为/lib/udev/rules.d/50-YourSoftwareName.rules的文件中(例如,在man udev中搜索文件命名规则,但50-usbhax.rules应该这样做)

  2. 将您的用户名添加到plugdev群组

    adduser username plugdev
    
  3. 重新加载规则

    sudo udevadm control --reload(即减去负载重新加载)
    sudo udevadm trigger

  4. 拔下并重新插入设备或重新启动设备,看它是否有效。

  5. 注意:有些系统会使用小组input而不是plugdev,因此请相应地修改上述内容。
    要查看系统使用的内容,请查看group/dev/usb

    中相关条目的/dev/input名称