无法使用libusb接收数据

时间:2016-02-22 10:14:44

标签: c embedded

我想从设备发送和接收数据,反之亦然。我正在发送字符串,但无法完全接收它。示例:发送的字符串为Hello,输出为:

Received:H
Error in read! e = -4 and received = 5
#include <string.h>
    #include<stdio.h>
    #include <stdlib.h>
    #include <libusb-1.0/libusb.h>

    #define BULK_EP_OUT     0x01
    #define BULK_EP_IN      0x81

    /*find these values using lsusb -v*/

    uint16_t VENDOR = 0x0483;
    uint16_t PRODUCT = 0x5740;
    int main(void)
    {
        int ret = 1;    //int type result
       struct libusb_device **usb_dev;
    struct libusb_device_descriptor desc;
        struct libusb_device_handle *handle = NULL;
     struct device_handle_expected;
    //struct libusb_device_handle device_expected_handle = NULL;
        struct libusb_device *dev, *dev_expected; 
        char *my_string, *my_string1;
        int e = 0, config;
        char found = 0;
        int transferred = 0;
        int received = 0;
        int length = 0;
        int i=0;
        int count;

        /*struct libusb_device *dev;
        struct libusb_device **devs;
        struct dev_expected;*/

        // Initialize libusb
        ret = libusb_init(NULL);
        if(ret < 0)
        {
            printf("\nFailed to initialise libusb\n");
            return 1;
        }
        else
            printf("\nInit successful!\n");

        // Get a list of USB devices
        count = libusb_get_device_list(NULL, &usb_dev);
        if (count < 0)
        {
            printf("\nThere are no USB devices on the bus\n");
            return -1;
        }
        printf("\nToally we have %d devices\n", count);

        while ((dev = usb_dev[i++]) != NULL)
        {    

            ret = libusb_get_device_descriptor(dev, &desc);
            if (ret < 0)
                {
                printf("Failed to get device descriptor\n");
                libusb_free_device_list(dev, 1);
                break;
            }

            e = libusb_open(dev, &handle);
            if (e < 0)
            {
                printf("Error opening device\n");
                libusb_free_device_list(dev, 1);
                libusb_close(handle);
                break;
            }

            if(desc.idVendor == 0x0483 && desc.idProduct == 0x5740)
            {
               found = 1;
            break;
            }
        }//end of while
        if(found == 0)
        {
            printf("\nDevice NOT found\n");
            libusb_free_device_list(usb_dev, 1);
            libusb_close(handle);
            return 1;
        }
        else
        {
            printf("\nDevice found");
           // dev_expected = dev;
            //device_handle_expected = handle;
        }

        e = libusb_get_configuration(handle, &config);
        if(e!=0)
        {
            printf("\n***Error in libusb_get_configuration\n");
            libusb_free_device_list(usb_dev, 1);
            libusb_close(handle);
            return -1;
        }
        printf("\nConfigured value: %d", config);

        if(config != 1)
        {
            libusb_set_configuration(handle, 1);
            if(e!=0)
            {
                printf("Error in libusb_set_configuration\n");
                libusb_free_device_list(usb_dev, 1);
                libusb_close(handle);
                return -1;
            }
            else
                printf("\nDevice is in configured state!");
        }



        if(libusb_kernel_driver_active(handle, 0) == 1)
        {
            printf("\nKernel Driver Active");
            if(libusb_detach_kernel_driver(handle, 0) == 0)
                printf("\nKernel Driver Detached!");
            else
            {
                printf("\nCouldn't detach kernel driver!\n");
                libusb_free_device_list(usb_dev, 1);
                libusb_close(handle);
                return -1;
            }
        }

        e = libusb_claim_interface(handle, 0);
        if(e < 0)
        {
            printf("\nCannot Claim Interface");
            libusb_free_device_list(usb_dev, 1);
            libusb_close(handle);
            return -1;
        }
        else
            printf("\nClaimed Interface\n");

        int nbytes = 64;
        my_string = (char *) malloc(nbytes + 1);
        my_string1 = (char *) malloc(nbytes + 1);

        memset(my_string, '\0', 64);//The C library function void (an unsigned char) to the first n characters of the string pointed to, by the argument str.
        memset(my_string1, '\0', 64);

        strcpy(my_string, "Hello");
        length = strlen(my_string);

        printf("\nTo be sent: %s", my_string);

        e = libusb_bulk_transfer(handle, BULK_EP_OUT, my_string, length, &transferred, 0);
        if(e == 0 && transferred == length)
        {
            printf("\nWrite successful!");
            printf("\nSent %d bytes with string: %s\n", transferred, my_string);
        }
        else
            printf("\nError in write! e = %d and transferred = %d\n", e, transferred);

       // sleep(3);
       i = 0;

        for(i = 0; i <= length; i++)
        {
            e = libusb_bulk_transfer(handle, BULK_EP_IN, my_string1,length, &received, 0);  //64: Max Packet Length
            if(e == 0 && received == length)
            {
                printf("\nReceived:");
                printf("%c", my_string1[i]); 
             sleep(5);
            }
            else
            {
                printf("\nError in read! e = %d and received = %d bytes\n", e, received);
                return -1;
            }
        }

        libusb_release_interface(handle, 0);
    libusb_free_device_list(usb_dev, 1);

        libusb_close(handle);
        libusb_exit(NULL);

        printf("\n");
        return 0;
    }

1 个答案:

答案 0 :(得分:1)

很明确,传输中的批量将立即传输整个缓冲区(如果您正在进行高速操作,则最多64个或512个 - 字节),而不是一次传输一个字节。

您正在迭代预期缓冲区的大小并为每个字节执行批量处理,并且只打印出第一个字节。

去除读取的for循环并将printf()更改为printf(“%s \ n”,my_string1);