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

smattmyers picture smattmyers · May 10, 2011 · Viewed 16.3k times · Source

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.

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

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

Any help will be appreciated.

Answer

David Fokkema picture David Fokkema · Jun 12, 2012

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

(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.)

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:

  • 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

So, try:

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

Good luck!