我面临的问题是,对于python,pyusb可以成功获得idVendor,idProduct,但我的开发仍然没有,这将影响我以后的操作,我需要帮助,谢谢。
大部分pyusb读写功能都在工作,所以我很担心。
代码
import time
from usb.core import find
from usb.util import get_string
import usb.backend.libusb1
import re
import usb.core
import usb.util
import os
import numpy as np
def info_usb_device(dev):
xdev = find(idVendor=dev.idVendor, idProduct=dev.idProduct)
if xdev.bDeviceClass == 9:
return
if xdev._manufacturer is None:
xdev._manufacturer = get_string(xdev, xdev.iManufacturer, langid=1033)
if xdev._product is None:
xdev._product = get_string(xdev, xdev.iProduct, langid=1033)
device_info = '[%20s] %8d %9d %s - %s' % (xdev.serial_number, dev.idVendor, dev.idProduct,
str(xdev._manufacturer).strip(),
str(xdev._product).strip())
return xdev.serial_number, device_info
def add_usb_devices(device_dict):
new_devices = []
for bus in usb.busses():
for dev in bus.devices:
if dev is None:
continue
serial_info = info_usb_device(dev)
if serial_info is not None:
(serial, info) = serial_info
if serial not in device_dict:
new_devices.append(serial)
device_dict[serial] = info
return new_devices
if __name__ == "__main__":
device_dict = {}
print('%22s %8s %9s %s' % ('serial', 'idVendor', 'idProduct', 'Manufacturer - Product'))
print('-'*22, '-'*8, '-'*9, '-'*30)
# first scan
add_usb_devices(device_dict)
for device_info in device_dict.values():
print(device_info)
# next scans
for i in range(5): # run 5 more scans
new_serials = add_usb_devices(device_dict)
if len(new_serials) > 0:
print('** (scan %d) FOUND NEW USB DEVICES/SERIALS: %s' % (i, new_serials))
for serial in new_serials:
print(device_dict[serial])
time.sleep(3) # waiting 3 seconds before new scan
str_device_dict_serial = device_dict[serial]
listed__device_dict_serial = re.findall(r'bd+b', str_device_dict_serial)
print('Scans completed.')
print("+ + + + + + + + + + + + +")
print(device_dict[serial])
print(listed__device_dict_serial)
input_idVendor = listed__device_dict_serial[0]
input_idProduct = listed__device_dict_serial[1]
dev = usb.core.find(idVendor= input_idVendor, idProduct=input_idProduct)
print(input_idVendor)
print(input_idProduct)
print(dev)
#print(ep)
输出:
(base) joy@joy-System-Product-Name:~$ sudo python '/home/joy/fe_dir/combined_function.py'
serial idVendor idProduct Manufacturer - Product
---------------------- -------- --------- ------------------------------
[ None] 1133 49271 Logitech - USB Optical Mouse
[ 2004888] 1423 37728 - USB Reader
[5334354E373539325A315A4B] 5117 2112 Generic - External
** (scan 2) FOUND NEW USB DEVICES/SERIALS: ['01DZTW5EXY5TSUF8']
[ 01DZTW5EXY5TSUF8] 34148 4096 JetFlash - Mass Storage Device
Scans completed.
+ + + + + + + + + + + + +
[ 01DZTW5EXY5TSUF8] 34148 4096 JetFlash - Mass Storage Device
['34148', '4096']
34148
4096
None
底部的";无";显示我无法获得开发
请参阅PyUSB文档中的示例
请阅读文档。find
函数需要参数idVendor
和idProduct
的数字参数,而不是字符串。
参见文档中的示例
# find our device
dev = usb.core.find(idVendor=0xfffe, idProduct=0x0001)
通过转换为数字进行修复
数字可以以十六进制格式(如0x0001
(指定,也可以以十进制1
指定。
在这里,正则表达式搜索返回字符串。因此,您需要将这些字符串转换为数字,例如使用int(str)
:
device_formatted_str = device_dict[serial]
(idVendor, idProduct, *_) = re.findall(r'bd+b', device_formatted_str) # unpack tuple into 2 required arguments. Remainder neglected.
print(type(idVendor), idVendor, type(idProduct), idProduct) # debug print types and values
dev = usb.core.find(idVendor=int(idVendor), idProduct=int(idProduct)) # convert string to number, here int
print(dev)
另请参阅:
- 开箱教程:Python-开箱元组