3

I'm writing a java program, and right now I have a setup-file which contains a COM port number. which has to be changed if the device changes COM port number.

This is not very user friendly. Therefore I want to be able to get a list of COM port ID's and let the user select the right device by its ID. I've tried googling, but without much success.

By ID I mean if you check the Device Manager: "COM Port ID (COM<#>)". Check the with red marked text seen in the following picture:

enter image description here

I have tried the following libraries:

  • javax.comm - CommPortIdentifier,getPortIdentifiers();
  • jssc - SerialPortList.getPortNames();

But I have been unable to find out if it is possible to get the COM port ID, as the two above methods just return the number of the COM Port. Does anyone know of a way to get the COM port IDs?

Zeliax
  • 4,987
  • 10
  • 51
  • 79
  • I'm currently not at the PC at which I have the code on, but as far as I remember it just returned "COM#". I want to get the name of the item connected to that port. – Zeliax Jun 07 '16 at 13:26
  • you can try to read this info from the [registry](http://stackoverflow.com/questions/6362775/getting-device-driver-information-related-to-a-com-port) - but this will of course only work on Windows. – TmTron Jun 10 '16 at 13:13
  • It's quite strange, JSSC works like a charm. Feel free to download this code, https://github.com/enriquezrene/javafx-arduino I show the COM ports and the user select it, and then I use it for work I hope it help you – Rene Enriquez Jun 10 '16 at 15:42

1 Answers1

2

I used rxtxcomm.jar and rxtxSerial.dll to communicate with an Arduino. This snippet should get you the available ports:

@SuppressWarnings("unchecked")
Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();

while (portEnum.hasMoreElements()) {
    CommPortIdentifier currPortId = portEnum.nextElement();
    System.out.println(currPortId.getName() + " - " + currPortId.getCurrentOwner());
}

Here's an article with some further details: https://blog.henrypoon.com/blog/2010/12/25/installing-rxtx-for-serial-communication-with-java/

maccaroo
  • 819
  • 2
  • 12
  • 22
  • The problem is that I don't connect to the device using the code, but I use a secondary API to do complete the connection and transfer the data required. – Zeliax Jun 20 '16 at 07:21