Friday, January 07, 2011

HOWTO : get a list of available ports, Java

If you want to ahve a list over all ports, the code down here can help alot
protected void list() {
// get list of ports available on this particular computer,
// by calling static method in CommPortIdentifier.
Enumeration portList = CommPortIdentifier.getPortIdentifiers();

// Process the list.
while (portList.hasMoreElements()) {
CommPortIdentifier cpi = (CommPortIdentifier) portList.nextElement();
System.out.print("Port " + cpi.getName() + " ");
if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println("is a Serial Port: " + cpi);
} else if (cpi.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
System.out.println("is a Parallel Port: " + cpi);
} else {
System.out.println("is an Unknown Port: " + cpi);
}
}
}

Source : java2s.com, modified as javax.comm does not exist anymore on Windows and RxTx should be used

No comments: