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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5943847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:49:25  来源:igfitidea点击:

Get string descriptor using PyUSB usb.util.get_string()

pythonwindowspyusblibusb-1.0

提问by smattmyers

I am having trouble getting the string descriptor of a USB device. What I'm looking for is the human friendly Manufacturer and Product names. I am using libusb-1.0 as backend and am able to get the Manufacturer name using the provided libusb test program, so I know it exists.

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

PyUSB help file says you can access usb_get_string_simple(from libusb backend) using:

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.

get_string(开发,长度,索引,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

Any help will be appreciated.

任何帮助将不胜感激。

回答by David Fokkema

(Update July 2019:See Teodor-Bogdan Barbieru's answer below for a clear description of why you should not actually hardcode indexes!)

2019 年 7 月更新:请参阅下面的 Teodor-Bogdan Barbieru 的回答,以清楚地说明为什么不应该对索引进行硬编码!)

(Second update July 2019:See gog's comment below for a link to the USB specification containing a table listing all the device descriptor fields.)

2019 年 7 月第二次更新:请参阅下面的 gog 评论以获取指向 USB 规范的链接,其中包含一个列出所有设备描述符字段的表格。)

The following line in your code:

代码中的以下行:

usb.util.get_string(dev.dev,256,0)

is indeed the problem. I'm not terribly sure what the USB specification says, but in my current hardware project, I get the following for choices of index:

确实是问题所在。我不太确定 USB 规范说了什么,但在我当前的硬件项目中,我得到以下索引选择:

  • index=0 (your choice) returns a single unicode character
  • index=1 sends the manufacturer
  • index=2 sends the device description
  • index=3 sends device serial number
  • index=0(您的选择)返回单个 unicode 字符
  • index=1 发送制造商
  • index=2 发送设备描述
  • index=3 发送设备序列号

So, try:

所以,试试:

usb.util.get_string(dev.dev, 256, 2)

Good luck!

祝你好运!

回答by Boggio

It's not a good idea to hard-code the index.

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

I would recommend using something like this:

我会建议使用这样的东西:

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

As you will see below the index is different from device to device.

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

Output of lsusb -v:

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

回答by fyngyrz

This will retrieve the strings from the device:

这将从设备中检索字符串:

dev = usb.core.find(idVendor=0x077d, idProduct=0x0410) # fill in your own device, of course
if dev is None:
    print 'Our device is not connected'
else:
    if dev._manufacturer is None:
        dev._manufacturer = usb.util.get_string(dev, dev.iManufacturer)
    print str(dev._manufacturer)
    if dev._product is None:
        dev._product = usb.util.get_string(dev, dev.iProduct)
    print str(dev._product)

回答by user7025870

modify to usb.util.get_string(dev, dev.iSerialNumber)

修改为 usb.util.get_string(dev, dev.iSerialNumber)