zoukankan      html  css  js  c++  java
  • PyUsb的使用记录--window中

    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.  写入或者读取字节数不足

  • 相关阅读:
    GitHub上如何创建组织?
    windows中使用Git如何解决文件冲突?
    GitHub上如何删除代码仓库?
    并发编程模型和访问控制
    大数据征信的应用和启示:ZestFinance的基于大数据的信用评估技术
    Hadoop的Python框架指南
    Redis+Django(Session,Cookie、Cache)的用户系统
    如何使你的Ajax应用内容可让搜索引擎爬行
    Web运营手记
    全屏slider--swiper
  • 原文地址:https://www.cnblogs.com/wtzl/p/13498087.html
Copyright © 2011-2022 走看看