项目需要对usb设备进行开发调试,选择搭建pyusb环境进行调试测试,这里记录下完整流程和中间解决的一些问题。
我使用的环境是window10 64bit, vscode 1.84.0 , Python 3.11.6
参考github上的 的readme。
安装步骤分为两步
Requirements and platform 安装依赖
pyusb是依赖于libusb库的python层面封装,所以要安装对应的库。
此处是windows环境,作者提供了2种选择
1.使用 pyocd/libusb-package 库进行安装,libusb-package库中自带了 libusb-1.0.dll的库。但是使用api的方式会有差异。
2.直接将libusb-1.0的dll拷贝到windows的系统目录,比如 C:WindowsSystem32 (通过 libusb:/ 官网可以下载)
这里使用了安装libusb-package的方式
pip install libusb-package
Installing 安装pyusb
这里直接安装最新版本即可。
# the latest official release
python -m pip install pyusb
官网提供的demo示例如下
import usb.util# find our device
dev = find(idVendor=0xfffe, idProduct=0x0001)# 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
cfg = _active_configuration()
intf = cfg[(0,0)]ep = usb.util.find_descriptor(intf,# match the first OUT endpointcustom_match = lambda e: dpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT)assert ep is not None# write the data
ep.write('test')
因为这里使用libusb-package,参考/ 介绍的查找设备的方式不同
dev = find(idVendor=0xfffe, idProduct=0x0001)
——》
import libusb_package
for dev in libusb_package.find(find_all=True):print(dev)
这里可以使用VendorID 和 ProductID 指定对应的USB设备。
这里修改后可用的发包代码如下
import os
import usb.util
import libusb_package
import usb.backend.libusb1devlist = libusb_package.find(find_all=True, idVendor=0xfffe, idProduct=0x0001)for dev in devlist :print(dev)dev.set_configuration()# get an endpoint instancecfg = _active_configuration()intf = cfg[(0,0)]ep = usb.util.find_descriptor(intf,# match the first OUT endpointcustom_match = lambda e: dpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT)assert ep is not None# write the datacount = ep.write('test',10)print("send count " + str(count))
最终能得到结果如下,显示了usb设备的 设备描述符,配置描述符,接口描述符,端点描述符
基于上述代码在运行时会报如下错误
in __init___check(_lib.libusb_open(self.devid, byref(self.handle)))File "C:UsersxxAppDataLocalPackagesPythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0LocalCachelocal-packagesPython311site-packagesusbbackendlibusb1.py", line 600, in _checkraise NotImplementedError(_strerror(ret))
NotImplementedError: Operation not supported or unimplemented on this platform
可以看到提示libusb_open未实现
参考如下博客:
需要为设备修改驱动,默认的usb设备会有对应的默认驱动,这里需要使用libusb去访问设备,需要安装特定的驱动。
Zadig下载地址:/
Zadig打开后,如果当前插入的USB设备都已经安装了驱动(我们常用的键鼠、U盘等都是自动安装了驱动的),这里的设备选择栏里就会没有设备,因为现在显示的是没有安装驱动的USB设备。可以通过勾选List All Devices查看所有设备。
Zadig包含了4种类型的驱动 WinUSB (v6.1) / Libusb-win32 / libusbK v3.1.0 / USB Serial CDC 。这里实际尝试 前3种都可以解决NotImplementedError的报错,Libusb-win32 / libusbK v3.1.0在抓包时无法抓到host发出的包,只有 WinUSB (v6.1)可以解决抓包的问题。
但是如上述博客所述,在修改对应设备的驱动为WinUSB (v6.1)驱动之后,会导致该设备原来的功能不能使用,主要是 VendorID 和 ProductID 相同的USB设备都会使用新安装的 WinUSB驱动,而不会使用 其原先自动安装的驱动。
因为这里我还是需要支持设备原先的功能,但是用默认的驱动检索安装方式都无法安装,所以简单介绍一下恢复到原有驱动的方法,参考如下博客的介绍:.html
Windows环境下基于Python的PyUSB库开发USB通讯
Python中pyusb的开发及使用
.rst tutorial
/
【USB】Zadig 工具的使用说明与下载
Zadig
.html 如何强制安装指定驱动
/ libusb
本文发布于:2024-02-03 08:53:13,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170692159349965.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |