错误消息';接口未声明';来自libusb

错误消息';接口未声明';来自libusb,usb,libusb,libusb-1.0,Usb,Libusb,Libusb 1.0,我正在尝试使用,但收到以下错误消息: usbfs:进程24665(myprogram)在使用前未声明接口0 我真的不明白为什么,因为据我所知,我是根据图书馆里的描述来做的。这是我的密码: #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <libusb.h> int main(void) { int result; struct libu

我正在尝试使用,但收到以下错误消息:

usbfs:进程24665(myprogram)在使用前未声明接口0

我真的不明白为什么,因为据我所知,我是根据图书馆里的描述来做的。这是我的密码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

#include <libusb.h>

int main(void)
{
    int result;
    struct libusb_device_descriptor desc;
    libusb_device **list;
    libusb_device *my_device = NULL;

    result = libusb_init(NULL);
    libusb_set_debug(NULL, 3);

    ssize_t count = libusb_get_device_list(NULL, &list);
    for (int i = 0; i < count; i++) {
        libusb_device *device = list[i];
        result = libusb_get_device_descriptor(device, &desc);
        if((desc.idVendor == 0x03f0) && (desc.idProduct == 0x241d)) {
            my_device = device;
            break;
         }
    }

    if(my_device != NULL) {
        libusb_device_handle *handle;
        result = libusb_open(my_device, &handle);
        int kernelActive = libusb_kernel_driver_active(handle, 0);
        if(kernelActive == 1) {
            result = libusb_detach_kernel_driver(handle, 0);
        }
        result = libusb_claim_interface (handle, 0);
        result = libusb_control_transfer(handle,0x21,34,0x0003,0,NULL,0,0);
        result = libusb_release_interface (handle, 0);

        if(kernelActive == 1) {
          result = libusb_attach_kernel_driver(handle, 0);
        }
        libusb_close(handle);
    }
    libusb_free_device_list(list, 1);
    libusb_exit(NULL);
    return EXIT_SUCCESS;
}
#包括
#包括
#包括
#包括
内部主(空)
{
int结果;
结构libusb_设备描述符desc;
libusb_设备**列表;
libusb_device*my_device=NULL;
结果=libusb_init(NULL);
libusb_设置_调试(NULL,3);
ssize\u t count=libusb\u get\u device\u list(NULL,&list);
for(int i=0;i
正如您所看到的,我确实在传输之前声明了接口。(我也曾在其他USB设备上尝试过同样的代码,以防万一与此有关。)


我正在使用libusb-1.0.9,这是我能找到的最新版本。我正在(精确穿山甲)上运行这个程序。

您应该检查所有的结果值,然后您就可以很容易地找出哪里出了问题。只要检查所有结果值,如果它们返回您期望的值

核实:

  • libusb_open是否返回libusb_成功
  • libusb_内核_驱动程序_active是否返回0或1,而不是错误代码
  • libusb_内核_驱动程序是否返回libusb_成功
  • libusb_声明接口是否返回libusb_成功

尝试调用libusb\u set\u debug(上下文,其中\u to)
从libusb获取更多调试信息。消息的where_to是一个整数:

Level 0: no messages ever printed by the library (default)
Level 1: error messages are printed to stderr
Level 2: warning and error messages are printed to stderr
Level 3: informational messages are printed to stdout, warning and error messages are printed to stderr
这是一个很好的例子


我运行的代码中的错误消息看起来很好,但在内部它报告其他进程对它有独占声明,因此我无法使用它。

libusb-1.0也有同样的问题;我最初有这样一个序列:

libusb_init
libusb_open_device_with_vid_pid
libusb_reset_device
libusb_get_device
libusb_reset_device
libusb_set_configuration
libusb_claim_interface
libusb_set_interface_alt_setting 
libusb_get_device_descriptor
libusb_get_bus_number 
libusb_get_device_address
libusb_get_string_descriptor_ascii
if(libusb_kernel_driver_active.. ) 
  if(libusb_detach_kernel_driver.. ) 
libusb_bulk_transfer
...
。。。对于它,“接口未声明”是在第一次执行
libusb\u bulk\u transfer
时生成的(但不是后续的,上面没有显示),我通过进入
gdb
确认了这一点。(顺便说一句,该错误消息来自)

此页面:指的是对
libusb-0.1
的修复,该修复调用了相应的“detach_driver”功能;因此,我也开始在代码中移动“detach_driver”部分,最后这个序列似乎摆脱了“interface not claims”消息:

显然,如果首先分离驱动程序,然后声明接口,则不会生成错误。但这也是OP中的内容-因此我认为OP的诀窍是将
分离
,然后
设置配置
,然后
声明接口

希望这有帮助,

干杯

可能是权限问题吗?你试过用sudo运行你的程序吗?是的,我试过sudo。事实证明,一切似乎都正常,因此我假设消息只是糟糕的信息,而不是错误或警告。不过,我还是很想理解为什么我会得到这个信息。。这可能会有所帮助。libusb_detach_内核返回libusb_ERROR_OTHER。不是很有用的错误消息。您的权限正常吗?您是以root用户还是普通用户身份运行程序?如果您以普通用户身份运行程序,请确保您具有设备文件的rw访问权限。
libusb_init
libusb_open_device_with_vid_pid
if(libusb_kernel_driver_active.. ) 
  if(libusb_detach_kernel_driver.. ) 
libusb_reset_device
libusb_get_device
libusb_set_configuration
libusb_claim_interface
libusb_set_interface_alt_setting 
libusb_get_device_descriptor
libusb_get_bus_number
libusb_get_device_address
libusb_get_string_descriptor_ascii
libusb_bulk_transfer
...