我无法获取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
任何帮助都将不胜感激。
硬编码索引不是个好主意。
我建议你用这样的方法:
如下所示,每个设备的索引都不同。
lsusb-v输出:
(更新日期:2019年7月:请参阅下面的Teodor Bogdan Barbieru的答案,以获得不应实际硬编码索引的明确说明!)
(2019年7月第二次更新:有关USB规范的链接(包含列出所有设备描述符字段的表),请参阅下面的gog注释。)
代码中的以下行:
确实是个问题。我不太清楚USB规范说明了什么,但在我当前的硬件项目中,我得到了以下索引选项:
所以,试着:
祝你好运!
这将从设备中检索字符串:
相关问题 更多 >
编程相关推荐