首页 > Windows开发 > 详细

PyUsb的使用记录--window中

时间:2020-08-14 01:43:52      阅读:74      评论:0      收藏:0      [点我收藏+]
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) #效果同上读取

 异常说明:

  1.   技术分享图片

     

     window/system32中缺少libusb-1.0.dll

  2. 技术分享图片

     

     没有读取到内容超时

  3. 技术分享图片

     

     写入或者读取字节数不足

PyUsb的使用记录--window中

原文:https://www.cnblogs.com/wtzl/p/13498087.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!