Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

embedded linux - How could I get USB serial number using libusb in C code?

I'm trying to get the usb device serial number from a C code application in a embedded Linux platform (petalinux) using libusb.

I've seen a few similar questions which helped me to get some direction but unfortunately I didn't manage to solve it yet.

First I'll show what I'm trying to read, this is the dmesg of the device when is plugged in:

[    9.632965] usb 1-1.4: New USB device found, idVendor=0403, idProduct=de37
[    9.639837] usb 1-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[    9.647146] usb 1-1.4: Product: PI CAM
[    9.651153] usb 1-1.4: Manufacturer: MANUF
[    9.655326] usb 1-1.4: SerialNumber: 20102130
[    9.688519] uvcvideo: Found UVC 1.00 device PI IMAGER (0403:de37)
[    9.695084] uvcvideo 1-1.4:1.0: Entity type for entity Processing 3 was not initialized!
[    9.703177] uvcvideo 1-1.4:1.0: Entity type for entity Camera 1 was not initialized!
[    9.711098] input: PI CAM: PI CAM as /devices/platform/amba/ff9d0000.usb0/fe200000.dwc3/xhci-hcd.0.auto/usb1/1-1/1-1.4/1-1.4:1.0/input/input0

What I'm trying to read is the serial number: 20102130

I've tried the next code:

    libusb_device*** list=NULL;
    libusb_context *context = NULL;
    libusb_init(&context);

        unsigned char serial[200];
        int status = libusb_get_string_descriptor_ascii(device,desc.iSerialNumber, serial,200);
        if(status < 0)
        {
            printf("Index: %d Error: %d
",desc.iSerialNumber, status);
        }else{
            printf("Themal camera serial: %s
",serial);
        }

But it gives a -1 error, it prints: "Index: 3 Error: -1" . If I remove the error checking I've got the serial: bA6` which it doesn't make any sense...

Does anyone know how could I retrieve/read that serial number from my application?

Thanks in advance.

question from:https://stackoverflow.com/questions/65617648/how-could-i-get-usb-serial-number-using-libusb-in-c-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...