其他分享
首页 > 其他分享> > 我如何使用pyusb与该设备通信?

我如何使用pyusb与该设备通信?

作者:互联网

我有一个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

它还使用lsusb命令显示在列出的USB设备中:

Bus 002 Device 011: ID 171b:2001

如果我在这里错了,请更正我,但我认为我需要取消绑定设备,以便可以使用pyusb与设备通信.当我尝试运行代码时,出现资源繁忙错误.

因此,我将CD压缩到/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环境中工作.

解决方法:

以我的经验,您必须为udev系统的设备设置一个规则文件.

>遵循以下原则:

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)
>将用户名添加到plugdev组

adduser username plugdev

>重新加载规则

sudo udevadm control –reload(即减负再加载)
sudo udevadm触发器
>拔下并重新插入设备,或重新启动计算机,看看它是否有效.

注意:某些系统使用组输入而不是plugdev,因此请进行相应编辑.
要检查您的系统使用什么,请查看/ dev / usb或/ dev / input中相关条目的组名.

标签:libusb,python,linux,usb
来源: https://codeday.me/bug/20191011/1891494.html