如何使用ctrl_transfer()发送数据

时间:2019-06-26 08:04:50

标签: python pyusb

我想使用pyUSB库与设备建立双向USB通信(读/写)。到目前为止,我的阅读工作正常,但无法弄清楚如何向设备发送命令。问题是设备没有OUT端点:

有关设备的信息:

DEVICE ID c201:1318 on Bus 007 Address 004 =================
 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               : 0xc201
 idProduct              : 0x1318
 bcdDevice              :  0x100 Device 1.0
 iManufacturer          :    0x1 UAB ELDES    
 iProduct               :    0x2 ESIM-384        
 iSerialNumber          :    0x3             
 bNumConfigurations     :    0x1
  CONFIGURATION 1: 100 mA ==================================
   bLength              :    0x9 (9 bytes)
   bDescriptorType      :    0x2 Configuration
   wTotalLength         :   0x22 (34 bytes)
   bNumInterfaces       :    0x1
   bConfigurationValue  :    0x1
   iConfiguration       :    0x0 
   bmAttributes         :   0x80 Bus Powered
   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 :    0x0
     bInterfaceProtocol :    0x0
     iInterface         :    0x4 Error Accessing String
      ENDPOINT 0x81: Interrupt IN ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x81 IN
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :   0x30 (48 bytes)
       bInterval        :    0x8

在这种情况下我应该使用ctrl_transfer()吗?如果可以,该如何使用?我不知道要为此功能传递哪些参数。

这是我的代码:

import usb.core


VENDOR_ID = 0xC201
PRODUCT_ID = 0x1318

# find the USB device
device = usb.core.find(idVendor=VENDOR_ID,
                       idProduct=PRODUCT_ID)
if device is None:
    raise ValueError('Device not found')
#use the first/default configuration
device.set_configuration()

print(device)
# first endpoint
endpoint = device[0][(0, 0)][0]


# read a data packet
data = None


def send_data(dev):
    b = bytearray()
    b.extend(map(ord, 'getversion\r'))
    buffer = [0] * 48
    buffer[0] = 0
    buffer[1] = len(b)
    for x in range(0, len(b)):
        buffer[x + 2] = b[x]
    dev.ctrl_transfer(0x01, 0x08, 0x0, 0x0, buffer)


def print_data(byte_array):
    a = ""
    for y in range(0, len(byte_array)):
        a += chr(byte_array[y])
    print(a)


byte_packet = []

while True:
    try:
        data = device.read(endpoint.bEndpointAddress,
                           endpoint.wMaxPacketSize)
    except usb.core.USBError as e:
        data = None
        if e.args == ('Operation timed out',):
            continue

    if data:
        if data[1] > 0:
            for i in range(1, len(data)):
                if data[i] == 0:
                    continue
                elif data[i] != 10:
                    byte_packet.append(data[i])
                elif data[i] == 10:
                    print_data(byte_packet)
                    byte_packet = []
                    send_data(device)
                    break

如何在此处找出参数?

dev.ctrl_transfer(0x01, 0x08, 0x0, 0x0, buffer)

任何帮助将不胜感激。

0 个答案:

没有答案