python libusb winerror 126_windows usb之python(pyusb)

软件安装

windows需要安装python环境,使用usb需要安装pyusb模块,pip install pyusb或者python -m pip install pyusb。

代码测试

#!/usr/bin/python

# -*- coding:utf-8 -*-

import usb.util

import sys

#USB\VID_1C4F&PID_0051

dev = usb.core.find(idVendor= 0x1C4F, idProduct= 0x0051)

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

cfg = dev.get_active_configuration()

I downloaded latest libusb windows binaries from here: http://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.20/libusb-1.0.20.7z/download then I copied libusb1.dll according to this: when you are on 64-bit Windows, MS64\dll\libusb-1.0.dll must be copied into C:\Windows\System32 and (for running 32-bit applications that use libusb) MS32\dll\libusb-1.0.dll must be copied into C:\Windows\SysWOW64. and then I copied libusb1.lib from binaries to lib dir in python directory and now pyusb works for me. hope it helps.

根据描述执行相应的操作后,在执行上述代码之后就可以看到usb的相关描述信息了。

附usb读取操作

#!/usr/bin/python

# -*- coding:utf-8 -*-

import usb.core

import usb.util

import array

dev = usb.core.find(idVendor=0x0416, idProduct=0x5020)

if dev is None:

raise ValueError('Device not found')

while True:

try:

data = dev.read(0x85, 64)

print(data)

except usb.core.USBError as e:

if e.args == ('Operation timed out',):

continue

dev.read(0x85, 64) 0x85是usb的端点号,根据要求填写即可

另附python获取usb设备的代码,需安装python -m pip install pypiwin32

import win32com.client

wmi = win32com.client.GetObject ("winmgmts:")

for usb in wmi.InstancesOf ("win32_usbcontrollerdevice"):

print(usb.Dependent)

可打印出usb设备,查找到对应的vid和pid

原文链接:https://blog.csdn.net/qq_39718408/article/details/105087521

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值