usb之python(pyusb)

电脑系统为WIN7 64位

python:为python3.6 32位

需要插件PyUSB-1.0.0.tar,pywinusb-0.4.2。

按照的步骤我偷懒了,自己百度一下。

我们先看设备管理的

测试代码如下

#!/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()

 测试结果

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
================== RESTART: C:\Users\libra\Desktop\2222.py ==================
DEVICE ID 1c4f:0051 on Bus 003 Address 002 =================
 bLength                :   0x12 (18 bytes)
 bDescriptorType        :    0x1 Device
 bcdUSB                 :  0x110 USB 1.1
 bDeviceClass           :    0x0 Specified at interface
 bDeviceSubClass        :    0x0
 bDeviceProtocol        :    0x0
 bMaxPacketSize0        :    0x8 (8 bytes)
 idVendor               : 0x1c4f
 idProduct              : 0x0051
 bcdDevice              :  0x110 Device 1.1
 iManufacturer          :    0x1 
 iProduct               :    0x2 
 iSerialNumber          :    0x0 
 bNumConfigurations     :    0x1
  CONFIGURATION 1: 100 mA ==================================
   bLength              :    0x9 (9 bytes)
   bDescriptorType      :    0x2 Configuration
   wTotalLength         :   0x22 (34 bytes)
   bNumInterfaces       :    0x1
   bConfigurationValue  :    0x1
   iConfiguration       :    0x0 
   bmAttributes         :   0xa0 Bus Powered, Remote Wakeup
   bMaxPower            :   0x32 (100 mA)
    INTERFACE 0: Human Interface Device ====================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x0
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x1
     bInterfaceClass    :    0x3 Human Interface Device
     bInterfaceSubClass :    0x1
     bInterfaceProtocol :    0x2
     iInterface         :    0x0 
      ENDPOINT 0x81: Interrupt IN ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x81 IN
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :    0x4 (4 bytes)
       bInterval        :    0xa

如果先出现系统无法识别的话

可以参考

https://stackoverflow.com/questions/33972145/pyusb-on-windows-8-1-no-backend-available-how-to-install-libusb

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.

 

继续

#!/usr/bin/python
# -*- coding:utf-8 -*-

import
usb.core 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() print("#"*60) print(cfg)
================= RESTART: C:/Users/libra/Desktop/3333333.py =================
DEVICE ID 1c4f:0051 on Bus 003 Address 002 =================
 bLength                :   0x12 (18 bytes)
 bDescriptorType        :    0x1 Device
 bcdUSB                 :  0x110 USB 1.1
 bDeviceClass           :    0x0 Specified at interface
 bDeviceSubClass        :    0x0
 bDeviceProtocol        :    0x0
 bMaxPacketSize0        :    0x8 (8 bytes)
 idVendor               : 0x1c4f
 idProduct              : 0x0051
 bcdDevice              :  0x110 Device 1.1
 iManufacturer          :    0x1 
 iProduct               :    0x2 
 iSerialNumber          :    0x0 
 bNumConfigurations     :    0x1
  CONFIGURATION 1: 100 mA ==================================
   bLength              :    0x9 (9 bytes)
   bDescriptorType      :    0x2 Configuration
   wTotalLength         :   0x22 (34 bytes)
   bNumInterfaces       :    0x1
   bConfigurationValue  :    0x1
   iConfiguration       :    0x0 
   bmAttributes         :   0xa0 Bus Powered, Remote Wakeup
   bMaxPower            :   0x32 (100 mA)
    INTERFACE 0: Human Interface Device ====================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x0
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x1
     bInterfaceClass    :    0x3 Human Interface Device
     bInterfaceSubClass :    0x1
     bInterfaceProtocol :    0x2
     iInterface         :    0x0 
      ENDPOINT 0x81: Interrupt IN ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x81 IN
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :    0x4 (4 bytes)
       bInterval        :    0xa
############################################################
  CONFIGURATION 1: 100 mA ==================================
   bLength              :    0x9 (9 bytes)
   bDescriptorType      :    0x2 Configuration
   wTotalLength         :   0x22 (34 bytes)
   bNumInterfaces       :    0x1
   bConfigurationValue  :    0x1
   iConfiguration       :    0x0 
   bmAttributes         :   0xa0 Bus Powered, Remote Wakeup
   bMaxPower            :   0x32 (100 mA)
    INTERFACE 0: Human Interface Device ====================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x0
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x1
     bInterfaceClass    :    0x3 Human Interface Device
     bInterfaceSubClass :    0x1
     bInterfaceProtocol :    0x2
     iInterface         :    0x0 
      ENDPOINT 0x81: Interrupt IN ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x81 IN
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :    0x4 (4 bytes)
       bInterval        :    0xa
View Code

 

转载于:https://www.cnblogs.com/libra13179/p/7446719.html

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值