pyusb环境搭建和无法发包问题

pyusb环境搭建和无法发包问题

项目需要对usb设备进行开发调试,选择搭建pyusb环境进行调试测试,这里记录下完整流程和中间解决的一些问题。

我使用的环境是window10 64bit, vscode 1.84.0 , Python 3.11.6

1 安装流程

参考github上的 https://github.com/pyusb/pyusb 的readme。

安装步骤分为两步

  1. 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:\Windows\System32 (通过 libusb:https://libusb.info/ 官网可以下载)

    这里使用了安装libusb-package的方式

pip install libusb-package
  1. Installing 安装pyusb

    这里直接安装最新版本即可。

# the latest official release
python -m pip install pyusb
2 使用流程

官网提供的demo示例如下

import usb.core
import usb.util

# find our device
dev = usb.core.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 = dev.get_active_configuration()
intf = cfg[(0,0)]

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)

assert ep is not None

# write the data
ep.write('test')

因为这里使用libusb-package,参考https://github.com/pyocd/libusb-package/ 介绍的查找设备的方式不同

dev = usb.core.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.core 
import usb.util
import libusb_package
import usb.backend.libusb1

devlist = libusb_package.find(find_all=True, idVendor=0xfffe, idProduct=0x0001)

for dev in devlist :
    print(dev)
    dev.set_configuration()

    # get an endpoint instance
    cfg = dev.get_active_configuration()

    intf = cfg[(0,0)]
    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)

    assert ep is not None
    # write the data
    count = ep.write('test',10)
    print("send count " + str(count))

最终能得到结果如下,显示了usb设备的 设备描述符,配置描述符,接口描述符,端点描述符
在这里插入图片描述

驱动安装

基于上述代码在运行时会报如下错误

 in __init__
    _check(_lib.libusb_open(self.devid, byref(self.handle)))
  File "C:\Users\xx\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\usb\backend\libusb1.py", line 600, in _check
    raise NotImplementedError(_strerror(ret))
NotImplementedError: Operation not supported or unimplemented on this platform

可以看到提示libusb_open未实现
参考如下博客:https://blog.csdn.net/weixin_42967006/article/details/108755972

需要为设备修改驱动,默认的usb设备会有对应的默认驱动,这里需要使用libusb去访问设备,需要安装特定的驱动。

Zadig下载地址:https://zadig.akeo.ie/

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驱动,而不会使用 其原先自动安装的驱动。

驱动恢复

因为这里我还是需要支持设备原先的功能,但是用默认的驱动检索安装方式都无法安装,所以简单介绍一下恢复到原有驱动的方法,参考如下博客的介绍:http://www.wrgho.com/help53.html

在这里插入图片描述
在这里插入图片描述

参考文档

https://blog.csdn.net/weixin_42967006/article/details/108755972 Windows环境下基于Python的PyUSB库开发USB通讯

https://blog.csdn.net/zbb297918657/article/details/103012320 Python中pyusb的开发及使用

https://github.com/pyusb/pyusb

https://github.com/pyusb/pyusb/blob/master/docs/tutorial.rst tutorial

https://github.com/pyocd/libusb-package/

https://blog.csdn.net/qq_31094099/article/details/102935180 【USB】Zadig 工具的使用说明与下载

https://github.com/pbatard/libwdi/wiki/Zadig Zadig

http://www.wrgho.com/help53.html 如何强制安装指定驱动

https://libusb.info/ libusb

要在Python上搭建libusb环境,可以按照以下步骤进行操作: 步骤1:安装libusb库 首先,确保你的系统已经安装了libusb库。在大多数Linux发行版上,可以使用以下命令安装: ``` sudo apt-get install libusb-1.0-0-dev ``` 步骤2:安装pyusb库 接下来,你需要安装pyusb库,它是一个用于Python的libusb绑定库。你可以使用pip来安装pyusb,运行以下命令: ``` pip install pyusb ``` 步骤3:设置udev规则(仅限Linux) 如果你正在使用Linux系统,则需要设置udev规则,以便允许非特权用户访问USB设备。创建一个名为`/etc/udev/rules.d/50-myusb.rules`的文件,并将以下内容添加到文件中: ``` SUBSYSTEM=="usb", ATTR{idVendor}=="xxxx", ATTR{idProduct}=="yyyy", MODE="0666" ``` 其中,`xxxx``yyyy`分别是你的USB设备的供应商ID产品ID。你可以使用`lsusb`命令来获取这些信息。 保存文件后,重新加载udev规则: ``` sudo udevadm control --reload-rules sudo udevadm trigger ``` 步骤4:测试代码 现在,你可以使用以下示例代码来测试libusb环境是否成功搭建: ```python import usb.core # 查找设备 dev = usb.core.find(idVendor=0xXXXX, idProduct=0xYYYY) # 如果设备未找到,则抛出异常 if dev is None: raise ValueError('Device not found') # 进行设备操作 # ... ``` 确保将`0xXXXX``0xYYYY`替换为你的USB设备的供应商ID产品ID。 这样,你就可以在Python中使用libusb库了。希望对你有所帮助!如果有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值