Get Serial Number of USB device with Python 3

∥☆過路亽.° 提交于 2020-12-10 16:12:27

问题


I have been using pyusb to access the detail of a printer plugged in via USB. I currently have the following code working, but it appears that different devices require a different index. Here is my current code:

import usb

dev = usb.core.find(idProduct=0x001f)
print(usb.util.get_string(dev,256,3))

dev2 = usb.core.find(idProduct=0x0009)
print(usb.util.get_string(dev2,256,3))

The code for dev works perfectly, outputting a serial number, but dev2 outputs 'Zebra,' the manufacturer name. If I change 3 to either 6 or 7 it works, but then the first dev returns an error.

One solution in Python 2 is to use print(dev.serial_number), but in the serial_number attribute doesn't appear to exist in pyusb for Python 3.

Is there a way to get this working reliably for all devices? Thanks.


回答1:


For the present PyUSB version 1.0.2, I found the correct syntax to answer this question to be:

import usb

dev = usb.core.find(idProduct=0x001f)
print( usb.util.get_string( dev, dev.iSerialNumber ) )

dev2 = usb.core.find(idProduct=0x0009)
print( usb.util.get_string( dev2, dev2.iSerialNumber ) )


来源:https://stackoverflow.com/questions/24956198/get-serial-number-of-usb-device-with-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!