1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import java.util.Enumeration;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
public class PortsEnumerator {
public static void main(String[] args) throws NoSuchPortException
{
Enumeration ports = CommPortIdentifier.getPortIdentifiers();
// CommPortIdentifier portId =
// CommPortIdentifier.getPortIdentifier("/dev/ttyUSB0");
// System.out.println(portId.getName());
System.out.println(ports.hasMoreElements());
int i = 1;
while (ports.hasMoreElements())
{
CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
System.out.println("Port n°"+i++);
System.out.println("\tNom\t:\t"+port.getName());
String type = null;
if (port.getPortType() == CommPortIdentifier.PORT_SERIAL) type = "Serie";
else type = "Parallèle";
System.out.println("\tType\t:\t"+type);
String etat = null;
if (port.isCurrentlyOwned()) etat = "Possédé par "+port.getCurrentOwner();
else etat = "Libre";
System.out.println("\tEtat\t:\t"+etat+"\n");
}
}
} |
Partager