Python学习笔记之PyUSB使用之开发

在设备列表中找到要使用的USB,如下图所示:

请注意上述的VID_0A12以及PID_0001;这个在使用USB接口时要用到VID以及PID;

import usb.util
import sys
all_devs = usb.core.find(find_all=True)
for d in all_devs:
    if (d.idVendor == vid) & (d.idProduct == pid):
        print(d)

我本机输出信息如下所示:

DEVICE ID 0a12:0001 on Bus 000 Address 001 =================
 bLength                :   0x12 (18 bytes)
 bDescriptorType        :    0x1 Device
 bcdUSB                 :  0x200 USB 2.0
 bDeviceClass           :   0xe0 Wireless Controller
 bDeviceSubClass        :    0x1
 bDeviceProtocol        :    0x1
 bMaxPacketSize0        :   0x40 (64 bytes)
 idVendor               : 0x0a12
 idProduct              : 0x0001
 bcdDevice              : 0x8891 Device 136.91
 iManufacturer          :    0x0 
 iProduct               :    0x2 CSR8510 A10
 iSerialNumber          :    0x0 
 bNumConfigurations     :    0x1
  CONFIGURATION 1: 100 mA ==================================
   bLength              :    0x9 (9 bytes)
   bDescriptorType      :    0x2 Configuration
   wTotalLength         :   0xb1 (177 bytes)
   bNumInterfaces       :    0x2
   bConfigurationValue  :    0x1
   iConfiguration       :    0x0 
   bmAttributes         :   0xe0 Self Powered, Remote Wakeup
   bMaxPower            :   0x32 (100 mA)
    INTERFACE 0: Wireless Controller =======================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x0
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x3
     bInterfaceClass    :   0xe0 Wireless Controller
     bInterfaceSubClass :    0x1
     bInterfaceProtocol :    0x1
     iInterface         :    0x0 
      ENDPOINT 0x81: Interrupt IN ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x81 IN
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :   0x10 (16 bytes)
       bInterval        :    0x1
      ENDPOINT 0x2: Bulk OUT ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x2 OUT
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :   0x40 (64 bytes)
       bInterval        :    0x1
      ENDPOINT 0x82: Bulk IN ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x82 IN
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :   0x40 (64 bytes)
       bInterval        :    0x1
    INTERFACE 1: Wireless Controller =======================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x1
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x2
     bInterfaceClass    :   0xe0 Wireless Controller
     bInterfaceSubClass :    0x1
     bInterfaceProtocol :    0x1
     iInterface         :    0x0 
      ENDPOINT 0x3: Isochronous OUT ========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x3 OUT
       bmAttributes     :    0x1 Isochronous
       wMaxPacketSize   :    0x0 (0 bytes)
       bInterval        :    0x1
      ENDPOINT 0x83: Isochronous IN ========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x83 IN
       bmAttributes     :    0x1 Isochronous
       wMaxPacketSize   :    0x0 (0 bytes)
       bInterval        :    0x1
    INTERFACE 1, 1: Wireless Controller ====================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x1
     bAlternateSetting  :    0x1
     bNumEndpoints      :    0x2
     bInterfaceClass    :   0xe0 Wireless Controller
     bInterfaceSubClass :    0x1
     bInterfaceProtocol :    0x1
     iInterface         :    0x0 
      ENDPOINT 0x3: Isochronous OUT ========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x3 OUT
       bmAttributes     :    0x1 Isochronous
       wMaxPacketSize   :    0x9 (9 bytes)
       bInterval        :    0x1
      ENDPOINT 0x83: Isochronous IN ========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x83 IN
       bmAttributes     :    0x1 Isochronous
       wMaxPacketSize   :    0x9 (9 bytes)
       bInterval        :    0x1
    INTERFACE 1, 2: Wireless Controller ====================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x1
     bAlternateSetting  :    0x2
     bNumEndpoints      :    0x2
     bInterfaceClass    :   0xe0 Wireless Controller
     bInterfaceSubClass :    0x1
     bInterfaceProtocol :    0x1
     iInterface         :    0x0 
      ENDPOINT 0x3: Isochronous OUT ========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x3 OUT
       bmAttributes     :    0x1 Isochronous
       wMaxPacketSize   :   0x11 (17 bytes)
       bInterval        :    0x1
      ENDPOINT 0x83: Isochronous IN ========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x83 IN
       bmAttributes     :    0x1 Isochronous
       wMaxPacketSize   :   0x11 (17 bytes)
       bInterval        :    0x1
    INTERFACE 1, 3: Wireless Controller ====================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x1
     bAlternateSetting  :    0x3
     bNumEndpoints      :    0x2
     bInterfaceClass    :   0xe0 Wireless Controller
     bInterfaceSubClass :    0x1
     bInterfaceProtocol :    0x1
     iInterface         :    0x0 
      ENDPOINT 0x3: Isochronous OUT ========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x3 OUT
       bmAttributes     :    0x1 Isochronous
       wMaxPacketSize   :   0x19 (25 bytes)
       bInterval        :    0x1
      ENDPOINT 0x83: Isochronous IN ========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x83 IN
       bmAttributes     :    0x1 Isochronous
       wMaxPacketSize   :   0x19 (25 bytes)
       bInterval        :    0x1
    INTERFACE 1, 4: Wireless Controller ====================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x1
     bAlternateSetting  :    0x4
     bNumEndpoints      :    0x2
     bInterfaceClass    :   0xe0 Wireless Controller
     bInterfaceSubClass :    0x1
     bInterfaceProtocol :    0x1
     iInterface         :    0x0 
      ENDPOINT 0x3: Isochronous OUT ========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x3 OUT
       bmAttributes     :    0x1 Isochronous
       wMaxPacketSize   :   0x21 (33 bytes)
       bInterval        :    0x1
      ENDPOINT 0x83: Isochronous IN ========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x83 IN
       bmAttributes     :    0x1 Isochronous
       wMaxPacketSize   :   0x21 (33 bytes)
       bInterval        :    0x1
    INTERFACE 1, 5: Wireless Controller ====================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x1
     bAlternateSetting  :    0x5
     bNumEndpoints      :    0x2
     bInterfaceClass    :   0xe0 Wireless Controller
     bInterfaceSubClass :    0x1
     bInterfaceProtocol :    0x1
     iInterface         :    0x0 
      ENDPOINT 0x3: Isochronous OUT ========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x3 OUT
       bmAttributes     :    0x1 Isochronous
       wMaxPacketSize   :   0x31 (49 bytes)
       bInterval        :    0x1
      ENDPOINT 0x83: Isochronous IN ========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x83 IN
       bmAttributes     :    0x1 Isochronous
       wMaxPacketSize   :   0x31 (49 bytes)
       bInterval        :    0x1

配置部分内容,示例如下:

def usb_config(self):
    try:
        self.dev_u.detach_kernel_driver(0)
    except:
        pass

    self.dev_u.set_configuration(1)
    self.dev_u.set_interface_altsetting(interface=0, alternate_setting=0)
    self.dev_u.set_interface_altsetting(interface=1, alternate_setting=0)
    
    cfg = self.dev_u.get_active_configuration()
    intf = usb.util.find_descriptor(cfg, bInterfaceNumber=0)
    intf = usb.util.find_descriptor(cfg, bInterfaceNumber=1)

    ep1 = usb.util.find_descriptor(intf, bEndpointAddress=129)
    ep2 = usb.util.find_descriptor(intf, bEndpointAddress=130)
    ep3 = usb.util.find_descriptor(intf, bEndpointAddress=131)
    try:
        usb.control.clear_feature(self.dev_u, usb.control.ENDPOINT_HALT, ep1)
        usb.control.clear_feature(self.dev_u, usb.control.ENDPOINT_HALT, ep2)
        usb.control.clear_feature(self.dev_u, usb.control.ENDPOINT_HALT, ep3)
    except:
        _logging.error('No usb.control module available!!!')
        return
    _logging.info('transport usb opened.')

后续要读写请使用Read、Write接口即可,如下简单示例:

    # read loop
    # dev_u: usb handle
    # tx_que/rx_que is queue

    def rx_loop(self):
        while(True):  
            try:
                pkt = self.dev_u.read(0x82, 1048, 1000)
            except Exception as e:
                print(e)
            
            if len(pkt) >0:
                self.tx_que.put(pkt)

    # write loop
    def tx_loop(self):
        while True:
            try: 
                c = self.tx_que.get(block=False)
            except:
                c = b''
            if len(c) == 0:
                continue
            else:
                status = self.dev_u.write(2, c, 1000)

 

  • 6
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
嗨!对于使用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设备的规格和协议。 希望这个例子能帮到你!如果有任何更多的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值