Querying Available COM Ports : COM Port « Development Class « Java






Querying Available COM Ports

 

// Install the Java Comm API first. if there is no necessary file, say Dll files, the API 
// won't work.

import java.util.Enumeration;

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: /// Shouldn't happen
        type = "Unknown";
        break;
      }
      System.out.println(port.getName() + ": " + type);
    }
  }
}

           
         
  








Related examples in the same category

1.Printing a FilePrinting a File
2.List the ports
3.Open a serial port using Java CommunicationsOpen a serial port using Java Communications
4.Subclasses CommPortOpen and adds send/expect handling for dealing with Hayes-type modems
5.Read from a Serial port, notifying when data arrives
6.Choose a port, any port!
7.Read from multiple Serial ports, notifying when data arrives on any
8.Demonstrate the port conflict resolution mechanism
9.Port Finder
10.Port Reader
11.Port Sniffer
12.Port Writer
13.This program tries to do I/O in each direction using a separate ThreadThis program tries to do I/O in each direction using a separate Thread