Skip to content

Get SerialNumber without opening device #546

Open

Description

@hrueger

Hi,
I'm trying to migrate from usb-detection. I have lot of devices with the same VID and PID and I need to differenciate them by their serial number. Using usb-detection I could get the serialNumber directly in the callback:

usbDetect.on("add", (newDevice) => console.log(newDevice.serialNumber));`

If I read the docs correctly, I need to open the USB Device in order to read its serial number, right? This feeld wrong, because there might be another process trying to open that device, too. I don't want to just claim every usb device with that VID and PID...
Did usb-detection open them in the background?

This is probably similar to #328, but I can't reopen that one.

Activity

  1. thegecko commented on Sep 22, 2022

    @thegecko
    Member

    I need to open the USB Device in order to read its serial number, right?

    Yes, the serial number string is not available in the usb descriptors, so you need to read it.

    Other things which may help:

  2. hrueger commented on Sep 22, 2022

    @hrueger
    Author

    Wow, thanks for that quick answer 🎉

    turn it into a WebUSB device first which will populate this (may be too heavy)

    does this internally open the device?

    I wonder if usb-detection also opened the device... Maybe @Julusian knows that? (sorry for pinging you ;-) )

    use (or look at) the findBySerialNumber function

    thanks, I see that it also opens the device. I'll try that.

  3. hrueger commented on Sep 22, 2022

    @hrueger
    Author

    I just did a quick test using the code of the findBySerialNumber function.
    However, I get

    Error: LIBUSB_ERROR_NOT_SUPPORTED
        at Device.ExtendedDevice.open (A:\_Source\GLUE\node_modules\usb\tsc\usb\device.ts:68:14)
        at Discoverer.getSerialNumber (A:\_Source\GLUE\packages\makehaus-lib\src\control\Discovery.ts:108:24)
        at EventEmitter.<anonymous> (A:\_Source\GLUE\packages\makehaus-lib\src\control\Discovery.ts:64:39)
        at EventEmitter.emit (node:events:390:28)
        at EventEmitter.emitHotplugEvents (A:\_Source\GLUE\node_modules\usb\tsc\usb\index.ts:63:17)
        at EventEmitter.emit (node:events:390:28) {
      errno: -12
    }
    

    If I read #182 correctly, that means that there's a wrong driver. Unfortunately, I can't require users to use Zadig to change the driver, just that I can read the serial number...
    I only need to find the serial number of the device. The physical usb device provides a virtual com port which is used by serialport.

  4. thegecko commented on Sep 22, 2022

    @thegecko
    Member

    does this internally open the device?

    Yes

    I wonder if usb-detection also opened the device...

    It will (or Windows will when you plug it in)

    This library uses libusb to be compatible across operating systems. Windows requires a driver for usb devices and can load winusb drivers for common devices (including HID devices AFAIK).

    On windows you can also try:

    https://github.com/node-usb/node-usb#usbuseusbdkbackend

  5. Julusian commented on Sep 22, 2022

    @Julusian
    Contributor

    It looks like usb-detection is using os native methods to fetch the serialnumber for each device.

    Parsing the unique device id on windows: https://github.com/MadLittleMods/node-usb-detection/blob/master/src/detection_win.cpp#L442-L467
    Linux exposes it as part of a set of properties https://github.com/MadLittleMods/node-usb-detection/blob/master/src/detection_linux.cpp#L153-L155
    Mac has an api to call for it https://github.com/MadLittleMods/node-usb-detection/blob/master/src/detection_mac.cpp#L189

    So it appears that it is possible on some platforms to retrieve the serialnumber without opening the device. But perhaps libusb isnt exposing a way to access this information?

  6. Julusian commented on Sep 22, 2022

    @Julusian
    Contributor

    Looking upstream, it sounds like there is no technical reason it isnt possible, and there has been some interest in supporting it, but nobody has provided a complete implementation yet.

    libusb/libusb#866
    libusb/libusb#875

  7. hrueger commented on Sep 22, 2022

    @hrueger
    Author

    Thanks for your hints. I'm gonna stay with @Julusian's patch of the usb-detection lib for now.
    Maybe we can leave this open until some implements it upstream?

  8. changed the title [-]Migrating from `usb-detection`: How to get Serial Number[/-] [+]Get SerialNumber without opening device[/+] on Sep 22, 2022
  9. mildsunrise commented on Sep 22, 2022

    @mildsunrise
    Collaborator

    hi, just wanted to say

    This feeld wrong, because there might be another process trying to open that device, too

    there's nothing wrong with multiple processes accessing a device at the same time. as long as you don't do stuff such as resetting the device, changing to another configuration, or claiming an interface, you shouldn't interfere with other processes.

    your use case in particular (opening a set of usb devices to get their serial number) is something I've definitely seen done in other libraries

  10. hrueger commented on Sep 23, 2022

    @hrueger
    Author

    Interesting. I thought that (at least on Windows) USB devices would be opened in exclusive mode (or was that only with hid devices? I can't remember.)
    Even if that did not cause trouble, I'm still stuck with the LIBUSB_NOT_SUPPORTED error.

  11. thegecko commented on Oct 31, 2022

    @thegecko
    Member

    I would be happy to expose/utilise functionality to get the usb serial number on windows if someone were to port it from usb-detection.

    e.g., the findBySerialNumber function could use native Windows code on windows and libusb otherwise. This feels like it would really harden the library and avoid a lot of libusb crashes finding devices on Windows. @Julusian WDYT?

  12. hrueger commented on Nov 1, 2022

    @hrueger
    Author

    on windows

    What would speak agains exposing that functionality on MacOS and Linux, too?

  13. thegecko commented on Nov 1, 2022

    @thegecko
    Member

    What would speak agains exposing that functionality on MacOS and Linux, too?

    I believe on those platforms you would be recreating how libusb does it.

  14. self-assigned this
    on Feb 26, 2023
  15. ItsHarper commented on Mar 22, 2023

    @ItsHarper

    What would speak agains exposing that functionality on MacOS and Linux, too?

    I believe on those platforms you would be recreating how libusb does it.

    Does this mean that on macOS and Linux you can retrieve the serial number without calling open()?

  16. Julusian commented on Mar 22, 2023

    @Julusian
    Contributor

    For those watching, there is movement on this upstream libusb/libusb#1258.
    I am keeping an eye on it in case I can help either there or here

    I would be happy to expose/utilise functionality to get the usb serial number on windows if someone were to port it from usb-detection.

    e.g., the findBySerialNumber function could use native Windows code on windows and libusb otherwise. This feels like it would really harden the library and avoid a lot of libusb crashes finding devices on Windows. @Julusian WDYT?

    I would treat anything from node-usb-detection as questionable, as there are probably newer apis that will make the task simpler (as was the case for hotplug detection), but it could be done if someone is motivated to do it.

  17. ItsHarper commented on Jan 23, 2024

    @ItsHarper

    The libusb PR got closed and its branch deleted* :(

    *Of course you can still view the changes at https://github.com/libusb/libusb/pull/1258/files

  18. qwertychouskie commented on Oct 1, 2024

    @qwertychouskie

    libusb/libusb#1532 is the current version being worked on.

  19. ItsHarper commented on Aug 28, 2025

    @ItsHarper

    After just shy of 3 years, the day has finally come that libusb supports this on its main branch: libusb/libusb@e894c3e

    I no longer even have much reason to care haha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions