使用 Python 3 获取 USB 设备的序列号 [英] Get Serial Number of USB device with Python 3

查看:81
本文介绍了使用 Python 3 获取 USB 设备的序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 pyusb 访问通过 USB 插入的打印机的详细信息.我目前有以下代码工作,但似乎不同的设备需要不同的索引.这是我当前的代码:

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

dev 的代码运行良好,输出序列号,但 dev2 输出Zebra",即制造商名称.如果我将 3 更改为 6 或 7 就可以了,但是第一个开发人员会返回错误.

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.

Python 2 中的一种解决方案是使用 print(dev.serial_number),但在 Python 3 的 pyusb 中似乎不存在 serial_number 属性.

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.

推荐答案

对于目前的 PyUSB 1.0.2 版,我发现回答这个问题的正确语法是:

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

这篇关于使用 Python 3 获取 USB 设备的序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆