Java Reference
In-Depth Information
Conversely, you can test for the presence of a port with a particular name, as
follows:
import javax.comm.*;
import java.util.*;
/** Look for COM# ports on the local machine. **/
public class PortTest
{
public static void main (String[] args) {
String port - name;
int i = 0;
while (true) {
i++;
port - name =" COM " +i;
try {
CommPortIdentifier port - id =
CommPortIdentifier.getPortIdentifier
(port - name);
System.out.println ("Port " + port - name +
" exists");
}
catch (NoSuchPortException e) {
System.out.println ("No port " + port - name);
break;
}
}
} // main
} // class PortTest
The output of this program might go as:
Port COM1 exists
Port COM2 exists
Port COM3 exists
Port COM4 exists
No port COM5
The following methods in CommPortIdentifier provide information about
the status of a port:
boolean isCurrentlyOwned() - indicates if another Java application owns the
port
String getCurrentOwner() -adescription of the Java application that owns a
port
 
Search WWH ::




Custom Search