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

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
嗨!对于使用Python编程来控制USB设备,你可以使用`pyusb`库。`pyusb`允许你与USB设备进行通信,发送和接收数据。 如果你还没有安装`pyusb`库,你可以使用以下命令在Python环境中安装它: ``` pip install pyusb ``` 一旦你安装好了`pyusb`库,你可以开始使用它来控制USB设备。你需要先找到你要控制的USB设备的设备号,然后打开连接并与设备进行通信。 下面是一个简单的例子,演示如何使用`pyusb`发送和接收数据: ```python import usb.core # 在这里设置你的USB设备的vendor id和product id vendor_id = 0xXXXX product_id = 0xXXXX # 查找设备 dev = usb.core.find(idVendor=vendor_id, idProduct=product_id) # 打开设备连接 dev.open() # 发送数据 data = [0x01, 0x02, 0x03] dev.write(1, data) # 接收数据 recv_data = dev.read(0x81, 64) # 关闭设备连接 dev.close() ``` 在上面的代码中,你需要替换`vendor_id`和`product_id`为你要控制的USB设备的对应值。然后,使用`usb.core.find()`方法查找设备并打开连接。然后,你可以使用`dev.write()`方法发送数据,使用`dev.read()`方法接收数据,并使用`dev.close()`方法关闭连接。 请注意,使用`pyusb`控制USB设备需要一些USB设备的相关知识,如设备的vendor id、product id、端点地址等。你需要查找并了解你要控制的具体USB设备的规格和协议。 希望这个例子能帮到你!如果有任何更多的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值