Python USB通讯

2023-11-15

1.下载libusb:

地址:Releases · libusb/libusb · GitHub

下载7z压缩包文件到本地:

解压后将32位版本的dll文件拷贝到C:\Windows\System32,64位的dll文件拷贝到C:\Windows\SysWOW64

2.安装python的usb模块

pip install pyusb

校验安装:

>>> import usb.core
>>> dev = usb.core.find(all=True)

将所有USB设备打印出来。

3.获取USB设备的读写endpoint

device.set_configuration()
    cfg = device.get_active_configuration()
    intf = cfg[(0, 0)]
    usb.util.claim_interface(device, intf)
    # 获取写endpoint
    write_ep = usb.util.find_descriptor(
        intf,
        # match the first OUT endpoint
        custom_match= \
            lambda e: \
                usb.util.endpoint_direction(e.bEndpointAddress) == \
                usb.util.ENDPOINT_OUT
    )
    # 获取读endpoint
    read_ep = usb.util.find_descriptor(
        intf,
        # match the first IN endpoint
        custom_match= \
            lambda e: \
                usb.util.endpoint_direction(e.bEndpointAddress) == \
                usb.util.ENDPOINT_IN
    )

5.通过endpoint读写数据

w_len = write_ep.write(data, timeout)

data = read_ep.read(len, timeout=timeout)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python USB通讯 的相关文章