【问题标题】:usb device identificationUSB设备识别
【发布时间】:2011-01-30 00:44:36
【问题描述】:

我在 ubuntu 9.04 上使用 python 假设我有两个 USB 设备连接到一台 PC。我如何在 python 代码中识别设备.....例如

如果 USB 端口 ID == A 将数据写入设备 1 如果 USB 端口 ID == B 向设备2写入数据

任何想法....

【问题讨论】:

    标签: python controls port device


    【解决方案1】:

    你试过pyUsb吗? 安装使用:

    pip install pyusb
    

    这里是你可以做什么的简单介绍:

    import usb
    busses = usb.busses()
    for bus in busses:
        devices = bus.devices
        for dev in devices:
            print("Device:", dev.filename)
            print("  idVendor: %d (0x%04x)" % (dev.idVendor, dev.idVendor))
            print("  idProduct: %d (0x%04x)" % (dev.idProduct, dev.idProduct))
    

    HerepyUsb的好教程。

    如需更多文档,请使用带有 dir() 和 help() 的 Python 交互模式。

    【讨论】:

    • 我已经在终端窗口中使用了上面的代码...它返回一个错误“对象没有属性设备”你能给我推荐一些可能有帮助的教程...或详细说明这个模块 usb
    • 代码在 Osx 上为我工作,Python 2.6 和 pyUsb 安装了端口。你的平台\Python 版本是什么?
    • 我现在已经安装了 pyusb 并运行示例 usbenum.py(你提到的代码看起来很相似)....我的 PC 上有 3 个 USB 端口,但结果显示有 6 个输出到 dev.filename ..它们是001或005等数字......当我插入设备时它们发生了变化......(我对USB标准不满意)......我只想识别每个设备/端口...... . 示例中的哪些参数对我有帮助....
    • 您可以与只知道它的 idVendor/idProduct 的设备交谈,而不管它连接到哪个端口。
    • 在 windows 上我没有输出
    【解决方案2】:

    @systempuntoout 的回答很好,但今天我找到了一种更简单的方法来查找或遍历所有设备:usb.core.find(find_all=True)

    按照你的例子:

    import usb
    for dev in usb.core.find(find_all=True):
        print "Device:", dev.filename
        print "  idVendor: %d (%s)" % (dev.idVendor, hex(dev.idVendor))
        print "  idProduct: %d (%s)" % (dev.idProduct, hex(dev.idProduct))
    

    【讨论】:

      【解决方案3】:

      但无论如何..有人会在某个时候寻找答案:

      我在 mac (osx 10.9).. 我成功地安装了带有 mac 端口的 libusb,但收到“没有可用的后端”消息。这是因为python找不到usb dylibs。

      您必须将 libusb 的路径添加到您的 $DYLD_LIBRARY_PATH(例如 /opt/local/lib,无论您的 macport 安装在哪里)。

      我一添加它,pyusb 就可以正常工作了。

      【讨论】:

      • 我刚遇到这个问题,通过自制软件成功安装了libusb:brew install libusb
      【解决方案4】:

      好的,我也在谷歌搜索答案,这里是 sn-p 的工作原理:

      def locate_usb():
      import win32file
      drive_list = []
      drivebits=win32file.GetLogicalDrives()
      for d in range(1,26):
          mask=1 << d
          if drivebits & mask:
              # here if the drive is at least there
              drname='%c:\\' % chr(ord('A')+d)
              t=win32file.GetDriveType(drname)
              if t == win32file.DRIVE_REMOVABLE:
                  drive_list.append(drname)
      return drive_list
      

      取自https://mail.python.org/pipermail/python-win32/2006-December/005406.html

      【讨论】:

        猜你喜欢
        • 2011-11-05
        • 2015-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多