libusb_open_device_with_vid_pid始终返回null

时间:2015-06-17 04:28:37

标签: c libusb

我是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

1 个答案:

答案 0 :(得分:0)

它可能出于任何原因发生。例如,您没有足够的权限打开设备,或者在打开设备时发生输入/输出错误。 from django.http import JsonResponse def get_data(request): maxX = request.GET['max_x'] minX = request.GET['min_x'] maxY = request.GET['max_y'] minY = request.GET['min_y'] extents = { 'max_x': maxX, 'min_x': minX, 'max_y': maxY, 'min_y': minY, } query = Query(max_x=maxX, min_x=minX, max_y=maxY, min_y=minY, username=username) query.save() return JsonResponse(extents) # this is if you want to use these json data in Javascript 没有为您提供错误代码,您可以将其用于诊断。这就是根本不使用libusb_open_device_with_vid_pid()的原因。