调用 libusb_bulk_transfer 时如何修复“LIBUSB_ERROR_NOT_FOUND”错误

2023-12-31

我正在创建一个程序,使用 libusb 从 MIDI 控制器读取输入。如何正确调用libusb_bulk_transfer?目前我每次都会收到错误“LIBUSB_ERROR_NOT_FOUND”,并且我收到的数据是“P”。

我已将函数“libusb_bulk_transfer”替换为“libusb_interrupt_transfer”,但仍然收到相同的错误:LIBUSB_ERROR_NOT_FOUND

以下是我目前包含的库

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

下面是查找所有 USB 设备并调用导致我出现问题的函数的主函数: printDeviceUsbInput(devices[i]);据我所知,主要功能运行良好。我删除了错误检查以使代码更短

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;
}

下面是查找 MIDI 键盘设备并尝试打印出 MIDI 输入的函数。又名导致我出现问题的功能。我受到这段代码的启发:http://libusb.sourceforge.net/api-1.0/libusb_io.html http://libusb.sourceforge.net/api-1.0/libusb_io.html

我还删除了错误检查以使函数更短。

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));
        }
    }
}

我预计对 libusb_bulk_transfer 的调用将返回 0,并且每次我按下 MIDI 键盘上的某个键时,变量 data 的值都会发生变化。


我认为您需要分离任何内核驱动程序,然后声明该接口。

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");

我正在努力解决同样的错误,因此找到了你的问题,但我的问题似乎与无法在 OS-X Mojave 上声明接口有关

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

调用 libusb_bulk_transfer 时如何修复“LIBUSB_ERROR_NOT_FOUND”错误 的相关文章

随机推荐