[Rxtx] how to identify connected port

Brian Schlining bschlining at gmail.com
Wed Nov 17 10:06:25 MST 2010


> or better, to know wich port is avaliable ?


/**
 * @return    A HashSet containing the CommPortIdentifier for all serial
ports that are not currently being used.
 */
    public static HashSet<CommPortIdentifier> getAvailableSerialPorts() {
        HashSet<CommPortIdentifier> h = new HashSet<CommPortIdentifier>();
        Enumeration thePorts = CommPortIdentifier.getPortIdentifiers();

        while (thePorts.hasMoreElements()) {
            CommPortIdentifier com = (CommPortIdentifier)
thePorts.nextElement();

            switch (com.getPortType()) {
            case CommPortIdentifier.PORT_SERIAL:
                try {
                    CommPort thePort = com.open("CommUtil", 50);

                    thePort.close();
                    log.debug("Serial Port, " + com.getName() + ", is
available");
                    h.add(com);
                }
                catch (PortInUseException e) {
                    if (log.isInfoEnabled()) {
                        log.debug("Serial port, " + com.getName() + ", is in
use.");
                    }
                }
                catch (Exception e) {
                    if (log.isErrorEnabled()) {
                        log.error("Failed to open serial port " +
com.getName(), e);
                    }
                }
            }
        }

        return h;
    }
-- 
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Brian Schlining
bschlining at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qbang.org/pipermail/rxtx/attachments/20101117/c57cd355/attachment-0638.htm>


More information about the Rxtx mailing list