10

I have a serial to USB device and more than one of those can be connected to the computer. I need to query and retrieve a list of COM ports that the devices are connected to. In Windows Device Manager you can get the COM port + friendly name of devices that are connected at the present time. This list is dynamic.

Reading from the registry did not work because the information stored is stale and static, not dynamic.

Devcon (from Microsoft) does list the ports that devices are connected to, but it cannot be used in my app because it is not re-distributable.

Any ideas or preferably, a solution?

Community
  • 1
  • 1
likejudo
  • 3,396
  • 6
  • 52
  • 107
  • 1
    You know how to read and write COM ports? Check here: http://www.java-samples.com/showtutorial.php?tutorialid=11 (Makes use of javax.comm packages). Then you could simply iterate through all ports to find the ones you are looking for, couldn't you? – Sebastian Mar 15 '13 at 02:10
  • @Sebastian I don't know if you read the question carefully. "...retrieve a list of COM ports that the devices are connected to." – likejudo Mar 15 '13 at 15:48
  • And I gave you a good workaround – Sebastian Mar 16 '13 at 10:32
  • @Sebastian Let me explain: I don't want a list of all the COM ports. I can get that already. I want a list of the COM ports that are connected to the particular device. In Windows Device Manager you can get the COM port + friendly name of devices that are connected at the present time. This list is dynamic. Did I communicate the point clearly or is there something I am missing? – likejudo Mar 16 '13 at 14:18

4 Answers4

3

I had to solve a similar issue just some weeks ago. I came accross the Jawin-Project that provided everything you need to use WMI-Queries. It is already quite old but works like a charm if you follow the documentation. A nice German walkthrough can be found codegods blog.

For me, it did not solve all problems (I had some very specific things to find out about the target device), I finally created an own JNI (Java Native Interface) Class and DLL in C incorporating the windows API.

I hope this is what you searched for.

zero0
  • 847
  • 11
  • 26
  • thanks. what part of the windows API did you access to get the information about what devices are connected? – likejudo Mar 19 '13 at 16:19
  • JNA seems like it will be easier but is LGPL https://github.com/twall/jna. I am not sure if it can be used in a commercial product. – likejudo Mar 19 '13 at 16:27
  • In 2005, I looked at Jawin for another project and it was hard to use - it assumed knowledge of Windows API and internals. It was hard to get any response on the mailing lists and it seems to be abandoned. Finally I used NJawin, a Java-COM bridge by Vitaly Shelest which also seems to have been eventually abandoned. However for this project, 8 years later, I believe there must be better options. For me, JNI to call a DLL in C is the last resort only if better options fail. – likejudo Mar 22 '13 at 14:30
1

Did you try this jUSB API or This example ?

I hope this will help you.

Community
  • 1
  • 1
Patriks
  • 1,012
  • 1
  • 9
  • 29
  • Thanks for the links. I went to the jusb site. Seems like abandonware - no updates since 2001. The second link is for filesystems, which mine isn't. – likejudo Mar 15 '13 at 20:45
  • Seems that you are still interested in this, so I will try something my self then I will let you know.. :) – Patriks Mar 16 '13 at 04:55
1

Use JNA (https://github.com/twall/jna) and take a look at the following page. It might be a useful point to start from.

http://www.digitalinferno.com/wiki/Wiki.jsp?page=JNAPrintDevices

stefan222
  • 669
  • 1
  • 11
  • 30
0

I am looking at Java Native Access as it provides access to the windows API from pure Java code - no JNI to deal with. I was concerned about the LGPL license; whether it can be included in a commercial product but from what a few posts in the newsgroup say, it can be. After I try it out in the next few weeks, I shall post back here.

likejudo
  • 3,396
  • 6
  • 52
  • 107
  • did you managed to get it working with JNA? Could pls share your experience? I am trying to write a program that detect drives upon insert (hot-swap) and I was wondering if JNA could be of any help. tnx!! – Martijn Aug 29 '13 at 19:50
  • This works well on Windows https://www.digitalinferno.com/wiki/Wiki.jsp?page=JNAPrintDevices (don't know yet about what license) – ZiglioUK Jun 30 '15 at 04:08