文章目录提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
- 前言
- 一、查找电脑上多有USB设备,的PID和VID
- 二、调用指定的USB端口设备
前言
使用Python解决同一台如何调用多台usb打印机
一、查找电脑上多有USB设备,的PID和VID
"""
查看电脑连接的usb设备的
端口号,硬件ID
"""
import usb.util
dev = usb.core.find(find_all=True)
print(type(dev))
# print(next(dev))
print("!!")
with open('.data.txt', "a") as f:
for i in dev:
print(type(i))
# if i.idVendor==0x0a5f :
print("i.idVendor:",i.idVendor)
print("i.iSerialNumber:",i.iSerialNumber)
print("i.bNumConfigurations:",i.bNumConfigurations)
print(i)
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
f.write("n")
f.write(str(i))
f.write("nnnn ##################################################")
f.close()
运行之后可以看到如下结果:
DEVICE ID 8086:43ed on Bus 002 Address 000 =================
bLength : 0x12 (18 bytes)
bDescriptorType : 0x1 Device
bcdUSB : 0x300 USB 3.0
bDeviceClass : 0x9 Hub
bDeviceSubClass : 0x0
bDeviceProtocol : 0x3
bMaxPacketSize0 : 0x9 (9 bytes)
idVendor : 0x8086
idProduct : 0x43ed
bcdDevice : 0x100 Device 1.0
iManufacturer : 0x0
iProduct : 0x0
iSerialNumber : 0x0
bNumConfigurations : 0x1
CONFIGURATION 1: 0 mA ====================================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x2 Configuration
wTotalLength : 0x1f (31 bytes)
bNumInterfaces : 0x1
bConfigurationValue : 0x1
iConfiguration : 0x0
bmAttributes : 0xc0 Self Powered
bMaxPower : 0x0 (0 mA)
INTERFACE 0: Hub =======================================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x4 Interface
bInterfaceNumber : 0x0
bAlternateSetting : 0x0
bNumEndpoints : 0x1
bInterfaceClass : 0x9 Hub
bInterfaceSubClass : 0x0
bInterfaceProtocol : 0x0
iInterface : 0x0
ENDPOINT 0x81: Interrupt IN ==========================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x81 IN
bmAttributes : 0x3 Interrupt
wMaxPacketSize : 0x3 (3 bytes)
bInterval : 0xc
这是一个设备的内容。
DEVICE ID 8086:43ed on Bus 002 Address 000 =================
这个结果第一行 VID:8086 PID:43ed USB端口号:000
记住这个结果,后面有用
dev_all = usb.core.find(find_all=True)
for i in dev_all:
if i.idVendor == 0x0a5f:
list_i = list(str(i).split(" "))
if list_i[7]=="006":
dev=i
print("dev=i")
# print("print_usb_a")
dev.set_configuration()
cfg = dev.get_active_configuration()
intf = cfg[(0, 0)]
ep_a = usb.util.find_descriptor(
intf,
custom_match=
lambda e:
usb.util.endpoint_direction(e.bEndpointAddress) ==
usb.util.ENDPOINT_OUT)
这段程序的idVendor 需要改成你需要的设备id。
自己设备的ID可以在设备管理器中查看。
list_i[7] 为上面查询的usb端口号



