如何使用pyusb与该设备通信? [英] How can I comunicate with this device using pyusb?

查看:380
本文介绍了如何使用pyusb与该设备通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Netware uniFlow设备.当我将其插入时,它会显示在dmesg中:

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

使用lsusb命令,它也会出现在列出的USB设备中:

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

Bus 002 Device 011: ID 171b:2001

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

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.

因此,我将CD压缩到/sys/bus/usb/devices/usb2/2-1/2-1.4(取自dmesg),然后尝试使用取消绑定该设备

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"

但是我被拒绝了.因此,我以root用户身份登录,但是仍然遭到拒绝.

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?

是的,我已经安装了libusb和pyusb.我正在Ubuntu环境中工作.

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

推荐答案

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

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

  1. 类似的东西:

  1. Something along the lines of:

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应该这样做)

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)

将您的用户名添加到plugdev

adduser username plugdev

  • 重新加载规则

  • Reload rules

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

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

    拔出并重新插入设备,或重新启动计算机,看看它是否可以工作.

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

    注意:某些系统使用组input而不是plugdev,因此请进行相应编辑.
    要检查系统使用的内容,请查看/dev/usb/dev/input

    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

    这篇关于如何使用pyusb与该设备通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆