How to fix 'LIBUSB_ERROR_NOT_FOUND' error when calling libusb_bulk_transfer

女生的网名这么多〃 提交于 2021-02-20 03:40:20

问题


I'm creating a program that reads in input from a midi controller using libusb. How do I properly call libusb_bulk_transfer? Currently I'm receiving the error "LIBUSB_ERROR_NOT_FOUND" every time, and the data that I receive is "P".

I've swapped out the function 'libusb_bulk_transfer' with 'libusb_interrupt_transfer' but I still receive the same error: LIBUSB_ERROR_NOT_FOUND

Below are the libraries that I currently have included

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

Below is main function that finds all of the usb devices and calls the function that is causing me issues: printDeviceUsbInput(devices[i]); For all I know, the main function is working fine. I removed the error checking to make the code shorter

int main(int argc, char *argv[])
{
    libusb_device **devices;
    libusb_context *context = NULL;

    size_t list;
    size_t i;
    int returnValue;

    returnValue = libusb_init(&context);

    list = libusb_get_device_list(context, &devices);

    printf("There are %zu devices found \n\n", list);
    for (i = 0; i < list; i++)
    {
        printDeviceUsbInput(devices[i]);
        //printDevices(devices[i]);
    }

    libusb_free_device_list(devices, 1);
    libusb_exit(context);
    return 0;
}

Below is the function that finds the midi keyboard device and attempts to print out the midi input. AKA the function that is causing me problems. I was inspired by this code: http://libusb.sourceforge.net/api-1.0/libusb_io.html

I also removed the error checking to make the function shorter.

void printDeviceUsbInput(libusb_device *device)
{

    struct libusb_device_descriptor deviceDescriptor;

    int returnValue;

    returnValue = libusb_get_device_descriptor(device, &deviceDescriptor);

    if(deviceDescriptor.idProduct == 49)
    {
        printf("Keyboard found\n\n");
        unsigned char data[4];
        int actual_length;
        libusb_device_handle *deviceHandle;
        returnValue = libusb_open(device, &deviceHandle);

        while(1)
        {
            returnValue = libusb_bulk_transfer(deviceHandle, LIBUSB_ENDPOINT_IN,data, sizeof(data), &actual_length, 0);
            printf("Data: %s\n\n", data);
            printf("returnValue: %s\n\n", libusb_error_name(returnValue));
        }
    }
}

I expect that the call to libusb_bulk_transfer will return 0, and that the value of the variable data will change every-time I press a key on the midi keyboard.


回答1:


I think you need detach any kernel driver and then claim the interface.

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(devs, 1);
        libusb_close(handle);
        return -1;
    }
}

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

I'm struggling with the same error, hence finding your question, but my problem seems to be related to not being able to claim the interface on OS-X Mojave



来源:https://stackoverflow.com/questions/56729045/how-to-fix-libusb-error-not-found-error-when-calling-libusb-bulk-transfer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!