windows下调试usb通讯

Windows环境下基于Python的PyUSB库开发USB通讯_notimplementederror: operation not supported or un-CSDN博客

1,安装PyUSB

pip install pyusb

2,调试程序,读出usb列表

#!/usr/bin/python
# 此代码仅供示例,未测试
# 原始链接 http://www.bubuko.com/infodetail-2281652.html
import sys
import usb.core
# find USB devices
dev = usb.core.find(find_all=True)
# loop through devices, printing vendor and product ids in decimal and hex
for cfg in dev:
  sys.stdout.write('Decimal VendorID=' + str(cfg.idVendor) + ' & ProductID=' + str(cfg.idProduct) + '\n')
  sys.stdout.write('Hexadecimal VendorID=' + hex(cfg.idVendor) + ' & ProductID=' + hex(cfg.idProduct) + '\n\n')

3,读写fpga对应的设备

import usb.core
import usb.util

dev =  usb.core.find(idVendor= 0x04B4, idProduct= 0x1004)
if dev is None:
    raise ValueError('Device not found')
print(dev)
# 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)
print(ep)
assert ep is not None

# write the data
ep.write('test')
C:\Users\86181\venv\Scripts\python.exe D:\pycharm_prj\usbtest\test.py 
Traceback (most recent call last):
  File "D:\pycharm_prj\usbtest\test.py", line 10, in <module>
    dev.set_configuration()
  File "C:\Users\86181\venv\lib\site-packages\usb\core.py", line 915, in set_configuration
    self._ctx.managed_set_configuration(self, configuration)
  File "C:\Users\86181\venv\lib\site-packages\usb\core.py", line 113, in wrapper
    return f(self, *args, **kwargs)
  File "C:\Users\86181\venv\lib\site-packages\usb\core.py", line 158, in managed_set_configuration
    self.managed_open()
  File "C:\Users\86181\venv\lib\site-packages\usb\core.py", line 113, in wrapper
    return f(self, *args, **kwargs)
  File "C:\Users\86181\venv\lib\site-packages\usb\core.py", line 131, in managed_open
    self.handle = self.backend.open_device(self.dev)
  File "C:\Users\86181\venv\lib\site-packages\usb\backend\libusb1.py", line 804, in open_device
    return _DeviceHandle(dev)
  File "C:\Users\86181\venv\lib\site-packages\usb\backend\libusb1.py", line 652, in __init__
    _check(_lib.libusb_open(self.devid, byref(self.handle)))
  File "C:\Users\86181\venv\lib\site-packages\usb\backend\libusb1.py", line 600, in _check
    raise NotImplementedError(_strerror(ret))
NotImplementedError: Operation not supported or unimplemented on this platform
DEVICE ID 04b4:1004 on Bus 001 Address 011 =================
 bLength                :   0x12 (18 bytes)
 bDescriptorType        :    0x1 Device
 bcdUSB                 :  0x200 USB 2.0
 bDeviceClass           :    0x0 Specified at interface
 bDeviceSubClass        :    0x0
 bDeviceProtocol        :    0x0
 bMaxPacketSize0        :   0x40 (64 bytes)
 idVendor               : 0x04b4
 idProduct              : 0x1004
 bcdDevice              :    0x0 Device 0.0
 iManufacturer          :    0x1 Error Accessing String
 iProduct               :    0x2 Error Accessing String
 iSerialNumber          :    0x0 
 bNumConfigurations     :    0x1
  CONFIGURATION 1: 100 mA ==================================
   bLength              :    0x9 (9 bytes)
   bDescriptorType      :    0x2 Configuration
   wTotalLength         :   0x2e (46 bytes)
   bNumInterfaces       :    0x1
   bConfigurationValue  :    0x1
   iConfiguration       :    0x0 
   bmAttributes         :   0x80 Bus Powered
   bMaxPower            :   0x32 (100 mA)
    INTERFACE 0: Vendor Specific ===========================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x0
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x4
     bInterfaceClass    :   0xff Vendor Specific
     bInterfaceSubClass :    0x0
     bInterfaceProtocol :    0x0
     iInterface         :    0x0 
      ENDPOINT 0x2: Bulk OUT ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x2 OUT
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :  0x200 (512 bytes)
       bInterval        :    0x0
      ENDPOINT 0x4: Bulk OUT ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x4 OUT
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :  0x200 (512 bytes)
       bInterval        :    0x0
      ENDPOINT 0x86: Bulk IN ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x86 IN
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :  0x200 (512 bytes)
       bInterval        :    0x0
      ENDPOINT 0x88: Bulk IN ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x88 IN
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :  0x200 (512 bytes)
       bInterval        :    0x0

进程已结束,退出代码1

dev.set_configuration()报错

安装驱动,重启后解决:

需要为设备安装WinUSB驱动,点击下面的链接下载Zadig,这是一个Windows平台上专门用于安装USB相关驱动的小软件,下载后可直接运行。

https://udomain.dl.sourceforge.net/project/libusb-win32/libusb-win32-releases/1.2.6.0/libusb-win32-bin-1.2.6.0.zip

软件界面如下图所示,如果当前插入的USB设备都已经安装了驱动(我们常用的键鼠、U盘等都是自动安装了驱动的),这里的设备选择栏里就会没有设备,因为现在显示的是没有安装驱动的USB设备。

点击Option,勾选List All Devices,就可以看到当前所有USB设备,选择想要操作的设备,驱动选择栏选择WinUSB,点击Install Driver即可安装驱动。

!!!注意:更改设备驱动将会导致该设备原来的功能不能使用,所以这里请谨慎操作,最好选择没用的设备或自己开发的USB设备进行测试


下面这个链接是Zadig的官方说明,供参考:
https://github.com/pbatard/libwdi/wiki/Zadig

至此我的测试程序跑通

4,测试数据读写:

import usb.core
import usb.util

dev =  usb.core.find(idVendor= 0x04B4, idProduct= 0x1004)
if dev is None:
    raise ValueError('Device not found')
# print(dev)
# 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()
# print(cfg)
# print("****************************************************")
# intf = cfg[(0,0)]
# print(intf)
# print("****************************************************")

while True:
    try:
        data = dev.read(0x86, 512, 1000)  # read(endpoint, size_or_buffer, timeout = None)
        print(data)
    except Exception as e:
        print(e)

    try:
        data = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        data = dev.write(0x2, data, 1000)  # write(endpoint, data, timeout = None)
        print(data)
    except Exception as e:
        print(e)
#
# epout = usb.util.find_descriptor(
#     intf,
#     # match the first OUT endpoint
#     custom_match = \
#     lambda e: \
#         usb.util.endpoint_direction(e.bEndpointAddress) == \
#         usb.util.ENDPOINT_OUT)
#
# epin = usb.util.find_descriptor(
#     intf,
#     # match the first OUT endpoint
#     custom_match = \
#     lambda e: \
#         usb.util.endpoint_direction(e.bEndpointAddress) == \
#         usb.util.ENDPOINT_IN)
#
#
# print(epout)
# print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
# print(epin)
# assert epout is not None
#
# ret = epin.read()
# print(ret)

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


数据正常输出输入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值