Java Reference
In-Depth Information
getPortIdentifers() provides a list of CommPortIdentifier objects for
each port, serial and parallel, on the platform:
Enumeration portList =
CommPortIdentifier.getPortIdentifiers ();
This enumeration lists the instances of CommPortIdentifier , one for each
port. The methods of this class provide information about the particular port such
as its name and type via getName() and getPortType() , respectively, as the
PortList example below illustrates:
import javax.comm.*;
import java.util.*;
/** List all the ports available on the local machine. **/
public class PortList
{
public static void main (String[] args) {
Enumeration port - list = CommPortIdentifier.getPortIdentifiers ();
while (port - list.hasMoreElements ()) {
CommPortIdentifier port - id = (CommPortIdentifier) port - list.nextElement ();
if (port - id.getPortType () == CommPortIdentifier.PORT - SERIAL) {
System.out.println ("Serial port: " + port - id.getName ());
}
else if (port - id.getPortType () == CommPortIdentifier.PORT - PARALLEL) {
System.out.println ("Parallel port: " +port - id.getName ());
}
else
System.out.println ("Other port: " + port - id.getName ());
}
} // main
} // class PortList
We used the constants PORT - SERIAL and PORT - PARALLEL from the
CommPortIdentifier class to test for the port type. On a desktop machine
with four serial ports and two parallel ports, the output would go as:
Serial port: COM1
Serial port: COM2
Serial port: COM3
Serial port: COM4
Parallel port: LPT1
Parallel port: LPT2
 
Search WWH ::




Custom Search