gpt4 book ai didi

c - libusb_open_device_with_vid_pid 总是返回 null

转载 作者:太空宇宙 更新时间:2023-11-04 04:35:34 26 4
gpt4 key购买 nike

我是 Centos 用户。我尝试使用 libusb-1.0.19 写入我的 USB 闪存盘。但似乎“libusb_open_device_with_vid_pid”总是返回 NULL 值。为什么会这样?,我的程序在 ROOT 权限下运行,并且设备连接到我的 PC 并且程序可以获取它的供应商 ID 和产品 ID。

这是我的代码!!!

#include <cassert>
#include <cstdio>
#include <libusb-1.0/libusb.h>

#define VENDOR_ID 0x090c
#define PRODUCT_ID 0x1000


int main()
{
libusb_context *context = NULL ;
libusb_device_handle *dev_handle = NULL ;
libusb_device **devs ;
int rc = 0 ;
ssize_t count ; //holding number of devices in list

//----------------------------------------------------------------------------
// Initialize the library
//----------------------------------------------------------------------------
rc = libusb_init(&context);
assert(rc == 0);

//----------------------------------------------------------------------------
// Enable debug
//----------------------------------------------------------------------------
#ifndef NDEBUG
libusb_set_debug(context, LIBUSB_LOG_LEVEL_WARNING);
#endif
//----------------------------------------------------------------------------
// Get device list
//----------------------------------------------------------------------------
count = libusb_get_device_list(context, &devs);
assert(count > 0);


for (size_t idx = 0; idx < count; ++idx) {
libusb_device *device = devs[idx];
libusb_device_descriptor desc = {0};

rc = libusb_get_device_descriptor(device, &desc);
assert(rc == 0);

printf("Vendor:Device = %04x:%04x\n", desc.idVendor, desc.idProduct);
}
//----------------------------------------------------------------------------
// open usb device by vendor ID and Product ID
//----------------------------------------------------------------------------
dev_handle = libusb_open_device_with_vid_pid(context,VENDOR_ID,PRODUCT_ID);
assert(dev_handle == NULL);

//----------------------------------------------------------------------------
// Free device list
//----------------------------------------------------------------------------
libusb_free_device_list(devs, 1); //free the list, unref the devices in it

//----------------------------------------------------------------------------
// Write data to device
//----------------------------------------------------------------------------
unsigned char *data = new unsigned char[5]; //data to write
int actual;
data[0]='h';
data[1]='e';
data[2]='l';
data[3]='l';
data[4]='o';

/*Check if kenel driver attached*/
if(libusb_kernel_driver_active(dev_handle, 0))
{
rc = libusb_detach_kernel_driver(dev_handle, 0); // detach driver
assert(rc == 0);
}
rc = libusb_claim_interface(dev_handle, 0);
assert(rc < 0);

rc = libusb_bulk_transfer(dev_handle, (64 | LIBUSB_ENDPOINT_OUT), data, 4, &actual, 0);
assert (rc != 0 || actual != 5);

rc = libusb_release_interface(dev_handle, 0);
assert(rc != 0);

printf("Wrote \"hello\" to usb device\n");
//----------------------------------------------------------------------------
// close dev_handle
//----------------------------------------------------------------------------
libusb_close(dev_handle);

//----------------------------------------------------------------------------
// exit
//----------------------------------------------------------------------------
libusb_exit(context);

return 0;
}

这是我得到的结果

Vendor:Device = 1d6b:0002
Vendor:Device = 1d6b:0002
Vendor:Device = 090c:1000
Vendor:Device = 1d6b:0002
Vendor:Device = 1d6b:0003
Vendor:Device = 1d6b:0002
Vendor:Device = 1d6b:0003
Vendor:Device = 8087:0024
Vendor:Device = 2109:0811
Vendor:Device = 0458:003a
Vendor:Device = 04d9:1503
test: test.c:49: int main(): Assertion `dev_handle == __null' failed.
Aborted (core dumped)

这是 libusb 返回的错误信息。

libusb:error [submit_bulk_transfer] submiturb failed error -1 errno=22

最佳答案

它可能出于任何原因发生。例如,您没有足够的权限打开设备或打开设备时出现输入/输出错误。 libusb_open_device_with_vid_pid() 不会给你一个错误代码,你可以用它来进行诊断。这就是根本不使用 libusb_open_device_with_vid_pid() 的原因。

关于c - libusb_open_device_with_vid_pid 总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30882348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com