import usb.core
import usb.util
dev = usb.core.find(idVendor=0x03EB, idProduct=0x2421) # 设备管理器中确认这两个参数值
if dev is None:
raise ValueError('Device not found')
dev.set_configuration()
configuration = dev.get_active_configuration()
cfg_ = configuration[(0, 0)]
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)
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
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个字节
read = inPoint.read(64) # 读取64个字节
异常说明:
window/system32中缺少libusb-1.0.dll
没有读取到内容超时
写入或者读取字节数不足
手机扫一扫
移动阅读更方便
你可能感兴趣的文章