python - 使用 PyUSB usb.util.get_string() 获取字符串描述符

标签 python windows pyusb libusb-1.0

我无法获取 USB 设备的字符串描述符。我正在寻找的是人性化的制造商和产品名称。我使用 libusb-1.0 作为后端,并且能够使用提供的 libusb 测试程序获取制造商名称,所以我知道它存在。

PyUSB 帮助文件说您可以访问 usb_get_string_simple(从 libusb 后端)使用:

get_string(dev, length, index, langid=None)

   Retrieve a string descriptor from the device.
   dev is the Device object to which the request will be sent to.

   length is the length of string in number of characters.

   index is the string descriptor index and langid is the Language
   ID of the descriptor. If langid is omitted, the string descriptor
   of the first Language ID will be returned.

   The return value is the unicode string present in the descriptor.
import usb
#help(usb.core) 
busses = usb.busses()
for bus in busses:
  devices = bus.devices
  for dev in devices:
    _name = usb.util.get_string(dev.dev,256,0)  #This is where I'm having trouble
    print "device name=",_name
    print "Device:", dev.filename
    print "  Device class:",dev.deviceClass
    print "  Device sub class:",dev.deviceSubClass
    print "  Device protocol:",dev.deviceProtocol
    print "  Max packet size:",dev.maxPacketSize
    print "  idVendor:",hex(dev.idVendor)
    print "  idProduct:",hex(dev.idProduct)
    print "  Device Version:",dev.deviceVersion
    for config in dev.configurations:
      print "  Configuration:", config.value
      print "    Total length:", config.totalLength 
      print "    selfPowered:", config.selfPowered
      print "    remoteWakeup:", config.remoteWakeup
      print "    maxPower:", config.maxPower
      for intf in config.interfaces:
        print "    Interface:",intf[0].interfaceNumber
        for alt in intf:
          print "    Alternate Setting:",alt.alternateSetting
          print "      Interface class:",alt.interfaceClass
          print "      Interface sub class:",alt.interfaceSubClass
          print "      Interface protocol:",alt.interfaceProtocol
          for ep in alt.endpoints:
            print "      Endpoint:",hex(ep.address)
            print "        Type:",ep.type
            print "        Max packet size:",ep.maxPacketSize
            print "        Interval:",ep.interval

我们将不胜感激。

最佳答案

对索引进行硬编码不是一个好主意。

我建议使用这样的东西:

usb.util.get_string(dev, 256, dev.iSerialNumber)
usb.util.get_string(dev, 256, dev.iManufacturer)

正如您将在下面看到的,索引因设备而异。

lsusb -v 的输出:

device 1:            #index #string_descriptor
      iManufacturer  3 Linux 3.8.13 musb-hcd
      iProduct       2 MUSB HDRC host driver
      iSerial        1 musb-hdrc.1.auto

device 2:
      iManufacturer  2 E-boda
      iProduct       3 SUNNY V35
      iSerial        4 0123456789ABCDEF

关于python - 使用 PyUSB usb.util.get_string() 获取字符串描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5943847/

相关文章:

windows - 设置 Windows 推送通知服务 (WNS) 服务器端

python - 为什么 Python pyusb usb.core 访问因权限而被拒绝,为什么 rules.d 不修复它?

python - 计算 3D 点云的 EMD 非常慢

java - 部署 Java WAR 文件时出错 : SEVERE: Exception fixing docBase for context

windows - 测试跨 LAN/WAN 的文件传输速度

python - 使用 PyUSB 从 Tenma 72-7732 万用表读取数据

python - 无法访问 PyUSB 后端

python - 在 Python 中迭代打印两种数据类型

python - for循环前的数字是什么意思

python - 从字典创建 pandas 数据框