在设备列表中找到要使用的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)