List available port in Java

| Sunday, December 27, 2009
import javax.comm.*;
import java.util.Enumeration;

public class ListPorts {

   public static void main(String args[]) {
       Enumeration ports = CommPortIdentifier.getPortIdentifiers();
       while (ports.hasMoreElements()) {
           CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
           String type;
           switch (port.getPortType()) {
               case CommPortIdentifier.PORT_PARALLEL:
                   type = "Parallel";
                   break;
               case CommPortIdentifier.PORT_SERIAL:
                   type = "Serial";
                   break;
               default:
                    type = "Unknown";
                   break;
           }
           System.out.println(port.getName() + ": " + type);
       }
   }
}

0 comments:

Post a Comment