import usb.core
import usb.util
# find our device
dev = usb.core.find(idVendor=0x03EB, idProduct=0x2421) # 设备管理器中确认这两个参数值
# was it found?
if dev is None:
raise ValueError(‘Device not found‘)
# set the active configuration. With no arguments, the first
# configuration will be the active one
dev.set_configuration()
# get an endpoint instance
configuration = dev.get_active_configuration()
cfg_ = configuration[(0, 0)]
# 0x2 写入节点
outPoint = usb.util.find_descriptor(
cfg_,
# match the first OUT endpoint
custom_match=lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT)
# 0x81 读取节点
inPoint = usb.util.find_descriptor(
cfg_,
# match the first IN endpoint
custom_match=lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN)
assert outPoint, inPoint is not None
# print(dev.__str__()) #可打印设备具体信息
# print(dev.langids)
# print(dev.product)
# print(dev.serial_number)
# print(dev.manufacturer)
# print(dev.address)
# print(dev.bus)
# print(dev.port_number)
# print(dev.speed)
# print(dev.bNumConfigurations)
# print(dev.bDeviceClass)
msg = [1, 2, 3, 4, 5, 6] # bytes
temp0 = [0 for i in range(64 - len(msg))] # 缺少的字节
msg.extend(temp0) # 合并后64个字节[1, 2, 3, 4, 5, 6,0,0,0,0,......]
outPoint.write(msg) # 写入 注意必须满足64个字节
# dev.write(0x2, msg, 0) #效果同上写入
read = inPoint.read(64) # 读取64个字节
# read = dev.read(0x81, 64, 0) #效果同上读取
异常说明:
-
window/system32中缺少libusb-1.0.dll
-
没有读取到内容超时
-
写入或者读取字节数不足