Java - get COM port id

3.9k Views Asked by At

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?

1

There are 1 best solutions below

1
maccaroo On

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/