Enumerate Hardware Ports in Java
Enumerate hardware ports (e.g. serial) using Java.
Download: PortList.java | 1.4KB
/* PortList - example of enumerating hardware ports on a system using Java. Created to aid debugging Linux where the Arduino IDE (Java) cannot see a USB Serial device for 5 minutes after first plugging in. Compiled on Ubuntu 64bit with "javac PortList.java" after installing 64bit RXTX Library: sudo cp lib/RXTXcomm.jar /usr/lib/jvm/java-6-sun/jre/lib/ext/ sudo cp lib/librxtxSerial.so /usr/lib/jvm/java-6-sun/jre/lib/ext/ Version: 2009.11.16-01 */ import java.io.*; import java.util.*; import gnu.io.*; class PortList { static void listPorts() { Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); while ( portEnum.hasMoreElements() ) { CommPortIdentifier portIdentifier = (CommPortIdentifier) portEnum.nextElement(); System.out.println( portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()) ); } } static String getPortTypeName ( int portType ) { switch ( portType ) { case CommPortIdentifier.PORT_I2C: return "I2C"; case CommPortIdentifier.PORT_PARALLEL: return "Parallel"; case CommPortIdentifier.PORT_RAW: return "Raw"; case CommPortIdentifier.PORT_RS485: return "RS485"; case CommPortIdentifier.PORT_SERIAL: return "Serial"; default: return "unknown type"; } } public static void main(String args[]) { System.out.println("Getting list of ports"); listPorts(); System.out.println("Finished"); } }
