Menu

Opening two devices with same Id

Help
Welpa
2007-09-11
2012-12-06
  • Welpa

    Welpa - 2007-09-11

    Is there a way to open a specific device when we
    have two (or more) devices connected with the same
    Vendor and Product Id's?

    The USB.getDevice method only specifies the Id's
    so it can't distinguish two identical devices, can it?

     
    • andi

      andi - 2007-09-13

      No you can't. At the moment the device is identified by the vendor and product ids only. The question is, which information should be taken into account to distinguish two identical devices.
      The device descriptors will look the same. So I guess the device number or the filename can be used.

       
    • decrouez marion

      decrouez marion - 2008-06-03

      Hi,
      We can have the device number or the filename if we use the Usb_Device Class. Is it possible to change an usb_device into a device so we could read data from it?

      Thanks

       
    • andi

      andi - 2008-06-08

      Hi

      I have extended the Device class with a new constructor which allows to pass the system specific filename. So you can have multiple devices with the same vendor and product id. The code still needs to be checked. Have a look at the repository in the next few weeks.

       
    • Anonymous

      Anonymous - 2009-05-12

      Hi all,
      i've read this discussion and i've seen that multiple devices should be handled using the device filename.
      Am i wrong or the device filename is unique only when related to the same bus?

      Here's the output of my enumeration routine:

      Bus 005 Device 001: ID 1d6b/0001
      Bus 004 Device 002: ID 0a5f/0098 <------------
      Bus 004 Device 001: ID 1d6b/0001
      Bus 003 Device 002: ID 0a5f/0098 <------------
      Bus 003 Device 001: ID 1d6b/0001
      Bus 002 Device 003: ID 04b3/3018
      Bus 002 Device 004: ID 06cb/0009
      Bus 002 Device 002: ID 04b3/3016
      Bus 002 Device 001: ID 1d6b/0001
      Bus 001 Device 001: ID 1d6b/0002

      The number beside device is the device filename and as you can see it is the same for 2 devices attached to different buses.
      If what i say is correct it is needed to take also the bus in account when addressing a device.

      The output is obtained with this routine:

      public Vector scan(boolean verbose) {
              Vector v = new Vector();
              try {
                  Usb_Bus root = USB.getBus();
                  if(root == null) {
                      return v;
                  }
                  Usb_Bus curBus = root;
                  while(curBus != null) {
                      Usb_Device device = curBus.getDevices();
                      while(device != null) {
                          System.err.println(String.format("Bus %s Device %s: ID %04x/%04x", curBus.getDirname(), device.getFilename(), device.getDescriptor().getIdVendor(), device.getDescriptor().getIdProduct()));
                          v.add(String.format("%04x/%04x", device.getDescriptor().getIdVendor(), device.getDescriptor().getIdProduct()));
                          device = device.getNext();
                      }
                      curBus = curBus.getNext();
                  }
              } catch(USBException ex) {
                  System.err.println(ex.toString());
              }
              return v;
          }

       
    • Anonymous

      Anonymous - 2009-05-12

      I wish to add another observation, the following function in the USB class seems broken to me:

      private static Device getRegisteredDevice(short idVendor, short idProduct,
                  String filename) {
              for (Iterator<Device> iter = devices.iterator(); iter.hasNext();) {
                  Device dev = iter.next();
                  if (filename != null && dev.getFilename() != null
                          && filename.compareTo(dev.getFilename()) == 0
                          && dev.getIdVendor() == idVendor
                          && dev.getIdProduct() == idProduct) {
                      return dev;
                  } else if (dev.getIdVendor() == idVendor
                          && dev.getIdProduct() == idProduct) {
                      return dev;
                  }
              }
              return null;
          }

      Assuming that filename is unique (and i think it's not) and that i call this function with all the parameters set, this function searches for the device with pid, vid and filename i specify.
      Let's say i have 2 devices, both are cached in the USB class:

      VID: 1 PID: 1 FILENAME: 5 briefly (1,1,5)
      VID: 1 PID: 1 FILENAME: 7 briefly (1,1,7)

      I do a getDevice for 1,1,7

      the first if fails because 1,1,5 != 1,1,7
      but then there's the "else if" that matches only vid and pid so 1,1 == 1,1 and the device returned is 1,1,5 instead of 1,1,7

       
      • andi

        andi - 2009-05-13

        Hi

        Filename not unique:
        The filename value is platform dependant. On Windows I get something like \\.\libusb0-0001--0x046d-0xc016. This includes the bus number and is unique as far as I know. As this is not the case in your example I think it is required to add another parameter for the bus name.

        Function getRegisteredDevice:
        That is definitely wrong. When the filename is set but does not match it should return null. Thanks for pointing that out.

        I will update the code and post a message when done.

         
        • Anonymous

          Anonymous - 2009-05-13

          yeah sorry, i forgot to mention i'm woking on linux

           
    • andi

      andi - 2009-05-17

      There's an updated jar available which fixes the problems: https://sourceforge.net/project/showfiles.php?group_id=188245

       
      • Anonymous

        Anonymous - 2009-05-20

        seems to work under linux.
        i will test it more extensively on both linux and windows during the next weeks.
        thank you

         
    • Anonymous

      Anonymous - 2009-09-08

      Hi,
      i tested the dll extensively under windows and i found a major problem.
      I had to drive 3 printers from 3 software instances, each software instance had is own folder with jars and dll,

      I found that running the 3 instances togheter gives a lot of problems in opening the device (busy) and several other communication problems that render the library unusable.

      To solve the problem i had to run the 3 instances on 3 different PCs. No issues in this situation. Only few minor opening problems after switching on the printer (i had to try to open the device 2 times because the first time the device is not seen).

      I suspect that the dll is not completely thread safe or that some device handler get screwed when more that one device is open at a time.

      I'm not a windows dll expert and i tought that having 3 copies of it in 3 different folders would avoid those problems but it did not work.

       
  • andi

    andi - 2009-09-17

    Hi

    The DLL is calling libusb-win32 functions synchronously. So the question is if libusb-win32 is thread safe -- not the wrapper.
    I'm also not a DLL expert but  states: 1. Synchronize access to all global variables - 2. Link only with libraries that are thread safe

    The global variables in the DLL are: 1. Bus structure retrieved by usb\_get_busses() - 2. References to Java classes

    All read and write calls pass all data to the underlying library (libusb-win32 or libus).

    So I can't see a reason why the wrapper would cause a problem with writing/reading from multiple threads.

    I think you should find out if libusb-win32 is thread safe (mailing list of libusb-win32) or if some synchronization at the user code level has to be done.

    Spandi

      : http://msdn.microsoft.com/en-us/library/ms682592%28VS.85%29.aspx

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.