当我尝试用libusb raspberry pi 3发送usb数据时LIBUSB_ERROR_TIMEOUT

问题描述 投票:1回答:1

我正在研究一个项目,我需要通过USB端口与指纹传感器(gt-511c1r)进行通信(Uart对我不起作用)。我正在使用libusb库尝试使用附加的简单代码进行通信。 显然一切正常,直到我尝试将数据发送到传感器,然后发生LIBUSB_ERROR_TIMEOUT错误。 我正在使用带有更新固件的raspberry pi 3。我还将lsusb -v命令的输出附加到设备。 还用SO ubuntu在PC上测试代码,我有同样的问题,那么它会是代码问题吗? 任何可以帮我解决问题的帮助或线索我都会感谢你。问候

    //codigo de prueba
    #include <stdio.h>
    #include <libusb-1.0/libusb.h>
    #include <stdint.h>
    #include <string.h>


    /*--------------------------------------------------------------------*/
    int main(int argc, char*argv[])
    {
       int res = 0;  /* return codes from libusb functions */
       libusb_device_handle* handle = 0;  /* handle for USB device */
       int kernelDriverDetached = 0;  /* Set to 1 if kernel driver detached*/
       int numBytes                 = 0;  /* Actual bytes transferred. */
       uint8_t buffer[64];                /* 64 byte transfer buffer */
       int ep_out = 0x02;
       int ep_in = 0x81;

       /* Initialise libusb. */
       res = libusb_init(0);
       if (res != 0)
       {
          fprintf(stderr, "Error initialising libusb.\n");
          return 1;
       }

       /* Get the first device with the matching Vendor ID and Product ID.If
       * intending to allow multiple demo boards to be connected at once,you
       * will need to use libusb_get_device_list() instead. Refer to the libusb
       * documentation for details. */
       handle = libusb_open_device_with_vid_pid(0, 0x04d9, 0x8008);
       if (!handle)
      {
         fprintf(stderr, "Unable to open device.\n");
         return 1;
      }

       /* Check whether a kernel driver is attached to interface #0. If so, we'll
      * need to detach it.*/
     if (libusb_kernel_driver_active(handle, 0))
     {
        res = libusb_detach_kernel_driver(handle, 0);
        if (res == 0)
        {
           kernelDriverDetached = 1;
        }
        else
        {
           fprintf(stderr, "Error detaching kernel driver.\n");
           return 1;
        }
   }

    /* Claim interface #0. */
    res = libusb_claim_interface(handle, 0);
    if (res != 0)
    {
       fprintf(stderr, "Error claiming interface.\n");
       return 1;
    }

    memset(buffer, 0, 12);
    buffer[0] = 0x55;
    buffer[1] = 0xAA;
    buffer[2] = 0x01;
    buffer[3] = 0x00;
    buffer[4] = 0x00;
    buffer[5] = 0x00;
    buffer[6] = 0x00;
    buffer[7] = 0x00;
    buffer[8] = 0x01;
    buffer[9] = 0x00;
    buffer[10] = 0x01;
    buffer[11] = 0x01;

    res = libusb_bulk_transfer(handle, ep_out, buffer, 12, &numBytes, 100);
    if (res == 0)
    {
       printf("%d bytes transmitted successfully.\n", numBytes);
    }
    else
    {
      fprintf(stderr, "Error during send message: %s\n",libusb_error_name(res));
    }

     memset(buffer, 0, 12);

     res = libusb_bulk_transfer(handle, ep_in, buffer, 12, &numBytes, 100);
     if (res == 0)
     {
        printf("%d bytes receibed successfully.\n", numBytes);
     }
     else
     {
        fprintf(stderr, "Error during receibe response: 
        %s\n",libusb_error_name(res));
     }



    /* Release interface #0. */
    res = libusb_release_interface(handle, 0);
    if (0 != res)
    {
       fprintf(stderr, "Error releasing interface.\n");
    }

    /* If we detached a kernel driver from interface #0 earlier, we'll now
    * need to attach it again.  */
    if (kernelDriverDetached)
    {
      libusb_attach_kernel_driver(handle, 0);
    }

    /* Shutdown libusb. */
    libusb_exit(0);

    return 0;
   }

这里是lsusb -v输出

     Bus 001 Device 013: ID 04d9:8008 Holtek Semiconductor, Inc. 
     Device Descriptor:
     bLength                18
     bDescriptorType         1
     bcdUSB               2.00
     bDeviceClass            0 (Defined at Interface level)
     bDeviceSubClass         0 
     bDeviceProtocol         0 
     bMaxPacketSize0        64
     idVendor           0x04d9 Holtek Semiconductor, Inc.
     idProduct          0x8008 
     bcdDevice            1.00
     iManufacturer           1 FINGER
     iProduct                2 USB-MASS STORAGE
     iSerial                 3 000000000001
     bNumConfigurations      1
      Configuration Descriptor:
      bLength                 9
      bDescriptorType         2
      wTotalLength           32
      bNumInterfaces          1
      bConfigurationValue     1
      iConfiguration          0 
      bmAttributes         0xc0
      Self Powered
      MaxPower              100mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass         8 Mass Storage
      bInterfaceSubClass      6 SCSI
      bInterfaceProtocol     80 Bulk-Only
      iInterface              0 
    Endpoint Descriptor:
      bLength                 7
      bDescriptorType         5
      bEndpointAddress     0x81  EP 1 IN
      bmAttributes            2
      Transfer Type            Bulk
      Synch Type               None
      Usage Type               Data
      wMaxPacketSize     0x0040  1x 64 bytes
      bInterval               0
  Endpoint Descriptor:
    bLength                 7
    bDescriptorType         5
    bEndpointAddress     0x02  EP 2 OUT
    bmAttributes            2
      Transfer Type            Bulk
      Synch Type               None
      Usage Type               Data
    wMaxPacketSize     0x0040  1x 64 bytes
    bInterval               0
    Device Status:     0x0001
    Self Powered
c linux raspberry-pi
1个回答
1
投票

问题解决了。所有代码本身都很好,问题是传感器不明白我发送给他的是什么因为我没有根据传感器的usb协议使用发送正确的包。

© www.soinside.com 2019 - 2024. All rights reserved.