public class Ports { public static List<CommPortIdentifier> listCommPorts() { List<CommPortIdentifier> listOfPorts = new ArrayList<>(25); Enumeration ports = CommPortIdentifier.getPortIdentifiers(); while (ports.hasMoreElements()) { CommPortIdentifier curPort = (CommPortIdentifier) ports.nextElement(); //get only serial ports if (curPort.getPortType() == CommPortIdentifier.PORT_SERIAL) { listOfPorts.add(curPort); } } return listOfPorts; } }
@Override @SuppressWarnings( "unchecked" ) public Object[] getItems() { final Enumeration<CommPortIdentifier> portIdentifiers = CommPortIdentifier.getPortIdentifiers(); final List<String> portList = new ArrayList<String>(); while ( portIdentifiers.hasMoreElements() ) { CommPortIdentifier portId = portIdentifiers.nextElement(); if ( portId.getPortType() == CommPortIdentifier.PORT_SERIAL ) { portList.add( portId.getName() ); } } return portList.toArray( new String[portList.size()] ); } } );
@Override @SuppressWarnings( "unchecked" ) public Object[] getItems() { final Enumeration<CommPortIdentifier> portIdentifiers = CommPortIdentifier.getPortIdentifiers(); final List<String> portList = new ArrayList<String>(); while ( portIdentifiers.hasMoreElements() ) { CommPortIdentifier portId = portIdentifiers.nextElement(); if ( portId.getPortType() == CommPortIdentifier.PORT_SERIAL ) { portList.add( portId.getName() ); } } return portList.toArray( new String[portList.size()] ); } } );
static public void init(String[] args) { m_TestPortName = "cu.usbserial-FTOXM3NX"; if (args.length > 0) m_TestPortName = args[0]; Enumeration e = CommPortIdentifier.getPortIdentifiers(); boolean found = false; String last = null; while (e.hasMoreElements()) { CommPortIdentifier portid = (CommPortIdentifier) e.nextElement(); if (portid.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portid.getName().equals(m_TestPortName)) found = true; last = portid.getName(); } } if (!found) m_TestPortName = last; }