Я не получаю серийный номер с использованием библиотеки libusb в C ++ - PullRequest
0 голосов
/ 31 января 2020

Я включил заголовок libusb и получаю идентификатор поставщика, идентификатор продукта и все остальное. Но я не смог получить серийный номер. Я уже установил winusb и все необходимые файлы заголовков. И да, если я заменяю des c .iSerialNumber на des c .iManufacturer, я получаю информацию о производителе правильно.

void print_dev_details(libusb_device *dev)
{
    libusb_device_descriptor desc;
    unsigned char dev_detail[200];
    libusb_device_handle *devHandle = NULL;
    libusb_error descriptorStatus = (libusb_error)libusb_get_device_descriptor(dev, &desc);
if(descriptorStatus != LIBUSB_SUCCESS )
    {
        cout<<"Some Error Occur";
        return;
    }
    else
    {
        int device_open_status = libusb_open(dev, &devHandle); 
   if(device_open_status < 0)
        {
            cout<<"Error in opening Device::Status is:: ";
            return;
        }

        if(devHandle != NULL)
        {
            cout<<"Device open success and DeviceHandler is valid";
        }
        else{
            cout<<"Device not able to open";
            return;
        }
        cout<<"\nDevice Class: "<<(int)desc.bDeviceClass;
        cout<<"\nDevice Sub Class: "<<(int)desc.bDeviceSubClass;
        cout<<"\nVendor ID: "<<std::hex<<desc.idVendor;
        cout<<"\nProduct Id: "<<std::hex<<desc.idProduct;

            int returnStatus = 0;
            memset(dev_detail, 0, sizeof(dev_detail));
            // Here I'm printing the Serial Number
            cout<<"\niSerialNumber index: "<<(int)desc.iSerialNumber<<endl;
            returnStatus = libusb_get_string_descriptor_ascii(devHandle, 
            desc.iSerialNumber, dev_detail, sizeof(dev_detail));
            if(returnStatus < 0)
                cout<<"\nError to get iSerialNumber. Status : "<<std::dec<<returnStatus;
            else
                printf("\nSerial Number: %s",dev_detail);

}
libusb_close(devHandle);
}  
 int main()
    {
       libusb_device **devs;
       libusb_context *ctx = NULL;
       ssize_t cnt = 0;
       int r;
       r = libusb_init(&ctx);
       if(r < 0)
       {
           cout<<"Error";
           return(0);
       }
       else
       {
        cnt = libusb_get_device_list(ctx, &devs);
        cout<<"\nNumber of devices are: "<<cnt;
        if(cnt == 0)
            return -1;

        for(int i=0;i<cnt;i++)
        {
            cout<<"\n\n\nDevice Number: "<<i+1<<endl;
            print_dev_details(devs[i]);
        }
        libusb_free_device_list(devs,cnt);
        libusb_exit(ctx);
       }
       return 1;
    }

Вывод

Device Number: 4 
Device_open_status:: Device open success and DeviceHandler is valid
Device Class: 0 
Device Sub Class: 0
Vendor ID: c72 
Product Id: d 
iSerialNumber index: 0 Error to get iSerialNumber. Status : -2
...