Python USB通讯
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)
更多推荐
所有评论(0)