使用usblib与USB设备通信

时间:2019-02-28 10:15:02

标签: ubuntu usb libusb-1.0 ptp

我正在尝试通过PTP(图片传输协议)和 usblib 库通过摄像机(特别是Panasonic G9)进行通信。 我正在使用最新版本的usblib在ubuntu 16下工作。

我正在尝试获取相机设备描述符。但是我无法完成所需命令的批量输出。 当我执行实例libusb_bulk_transfer时,总是出现错误,错误代码为 -7 ,实际上表示Operation timed out,并且以下消息显示为:资源暂时不可用。 谁能帮我找出命令超时的原因。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libusb-1.0/libusb.h>

#define CAMERA_IDVENDOR 0x04da
#define CAMERA_IDPRODUCT 0x2374
#define CAMERA_BINTERFACE_NUMBER 0x00
#define CAMERA_BALTERNATE_SETTING 0x00
#define CAMERA_OUT_ENDPOINT       0x01
#define CAMERA_IN_ENDPOINT        0x82
#define DATA_BUFFER_SIZE          500 

int main(int argc, char *argv[])
{
    libusb_device_handle *camera;
    int transfer_size;
    int ret;
    unsigned char data[DATA_BUFFER_SIZE];
    memset(data,0,DATA_BUFFER_SIZE);

    libusb_init(NULL);

    camera = libusb_open_device_with_vid_pid(NULL, CAMERA_IDVENDOR, CAMERA_IDPRODUCT);
    if (camera == NULL)
    {
        perror("Failed to find Panasonic G9\n");
        EXIT_FAILURE;
    }

    ret = libusb_reset_device( camera );
    if (ret != 0)
    {
        printf("ret = %d\n",ret);
        perror("Failed to reset USB port\n");
        EXIT_FAILURE;
    }

    usleep(10000);  

    ret = libusb_clear_halt(camera,CAMERA_OUT_ENDPOINT);
    if (ret != 0)
    {
        printf("ret = %d\n",ret);
        perror("Failed to clear halt out endpoint\n");
        EXIT_FAILURE;
    }

    ret = libusb_clear_halt(camera,CAMERA_IN_ENDPOINT);
    if (ret != 0)
    {
        printf("ret = %d\n",ret);
        perror("Failed to clear halt in endpoint\n");
        EXIT_FAILURE;
    }
    usleep(10000);      

    ret = libusb_claim_interface(camera, CAMERA_BINTERFACE_NUMBER);
    if (ret != 0)
    {
        perror("Failed to claim interface\n");
        EXIT_FAILURE;
    }

    ret = libusb_set_interface_alt_setting(camera, CAMERA_BINTERFACE_NUMBER, CAMERA_BALTERNATE_SETTING);
    if (ret != 0)
    {
        perror("Failed to set alternate setting\n");
        EXIT_FAILURE;
    } 


    data[0] = (unsigned char)((0x1001 & 0xFF00)>> 8);
    data[1] = (unsigned char)(0x1001 & 0x00FF);

    for (int i = 0; i < 18; i++)
    {
        printf("%02x ",data[i]);
    }
    printf("\n");


    ret = libusb_bulk_transfer (camera, CAMERA_OUT_ENDPOINT , data, 14, &transfer_size, 1000);
    if (ret != 0)
    {
        printf("ret = %d\n",ret);
        printf("transfer_size = %d\n", transfer_size);
        perror("Failed to BULK OUT transfer\n");
        return EXIT_FAILURE;
    }
    return 0;
}

在下面添加了lsusb命令的输出。

Bus 004 Device 010: ID 04da:2374 Panasonic (Matsushita) Lumix Camera (PTP mode)
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               3.00
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0         9
  idVendor           0x04da Panasonic (Matsushita)
  idProduct          0x2374 Lumix Camera (PTP mode)
  bcdDevice            1.00
  iManufacturer           1 Panasonic
  iProduct                2 DC-G9
  iSerial                 3 0000000000000000000XER1809100032
  bNumConfigurations      2
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           57
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0x80
      (Bus Powered)
    MaxPower              224mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           3
      bInterfaceClass         6 Imaging
      bInterfaceSubClass      1 Still Image Capture
      bInterfaceProtocol      1 Picture Transfer Protocol (PIMA 15470)
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x01  EP 1 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0400  1x 1024 bytes
        bInterval               0
        bMaxBurst               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0400  1x 1024 bytes
        bInterval               0
        bMaxBurst               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               5
        bMaxBurst               0
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           57
    bNumInterfaces          1
    bConfigurationValue     2
    iConfiguration          0 
    bmAttributes         0xc0
      Self Powered
    MaxPower                2mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           3
      bInterfaceClass         6 Imaging
      bInterfaceSubClass      1 Still Image Capture
      bInterfaceProtocol      1 Picture Transfer Protocol (PIMA 15470)
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x01  EP 1 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0400  1x 1024 bytes
        bInterval               0
        bMaxBurst               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0400  1x 1024 bytes
        bInterval               0
        bMaxBurst               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               5
        bMaxBurst               0
Binary Object Store Descriptor:
  bLength                 5
  bDescriptorType        15
  wTotalLength           22
  bNumDeviceCaps          2
  USB 2.0 Extension Device Capability:
    bLength                 7
    bDescriptorType        16
    bDevCapabilityType      2
    bmAttributes   0x00000000
      (Missing must-be-set LPM bit!)
  SuperSpeed USB Device Capability:
    bLength                10
    bDescriptorType        16
    bDevCapabilityType      3
    bmAttributes         0x00
    wSpeedsSupported   0x000e
      Device can operate at Full Speed (12Mbps)
      Device can operate at High Speed (480Mbps)
      Device can operate at SuperSpeed (5Gbps)
    bFunctionalitySupport   1
      Lowest fully-functional device speed is Full Speed (12Mbps)
    bU1DevExitLat          10 micro seconds
    bU2DevExitLat        1023 micro seconds
Device Status:     0x0000
  (Bus Powered)

0 个答案:

没有答案