2023-04-18

Cannot Open USB Device with vid-pid libusb-1.0 in C

I'm trying to learn USB Handling on Windows 10 using C and libusb-1.0. for starters I was able to initialize the library successfully using the libusb_init() function. I have verified the initialization with the return code as 0. Next I tried using the libusb_open_device_with_vid_pid() function and stored the return value of this function in the provided libusb_device_handle struct. For some reason the return value of this function is always NULL. I have tried printing the device list with the libusb_get_device_list() function and my Arduino Nano is being detected there with the correct VID and PID (Verified from Windows Device Manager).

Can anyone tell me what could be going wrong here?

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>

#include "include/libusb-1.0/libusb.h"

#define VID     0x1A86
#define PID     0x7523

int main(void)
{
    int status = 0;

    libusb_device_handle* dev = NULL;

    system("cls");

    printf("Initializing libusb...\n");

    status = libusb_init(NULL);

    if(status != 0)
    {
        printf("\nCannot Initialize libusb!!\n");
        
        return 0;
    }
    printf("\nlibusb Initialized Successfully...\n");

    dev = libusb_open_device_with_vid_pid(NULL, VID, PID);
    
    if(dev == NULL)
    {
        printf("\nError!! Could Not Find USB Device!\n");
        libusb_exit(NULL);
        return 0;
    }
    printf("\nUSB Device Found...\n");

    libusb_exit(NULL);

    return 0;
}

Thanks in Advance!



No comments:

Post a Comment