Discovering available comm ports

From Rxtx

(Difference between revisions)
Jump to: navigation, search
m (http://zelzelqa.is-the-boss.com/news-microsoft-reader-2009-01-01.html)
Line 1: Line 1:
-
[http://zelzelqa.is-the-boss.com/news-microsoft-reader-2009-01-01.html microsoft reader activation active x] [http://saererg.0lx.net/20081109-animal-intercourse.htm animal intercourse videos] [http://cnavieltz.strefa.pl/comment-1316.htm agnus of god movie] [http://caraines.qsh.eu/cnalar.htm prime video stores] [http://varpasz.is-the-boss.com/article576.htm mv torrents]
 
-
[http://roladarh.strefa.pl/news-1919.html new realises movies] [http://vihencbr.0lx.net/sony-psp-movie.html sony psp movie image converter 2.1] [http://acsitzar.0lx.net/news-eureekas-castle-2008-11-11.html eureekas castle videos] [http://ettaelt.strefa.pl/news-1304.html kitty fox movies] [http://naceceli.qsh.eu/page439.html regal movie]
 
-
rocalmo
 
-
trlidel
 
This code snippet shows how to retrieve the available comms ports on your computer. A CommPort is available if it is not being used by another application. Note the difference between the two examples is that the version for JDK 5.0 up uses [http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html generics]:
This code snippet shows how to retrieve the available comms ports on your computer. A CommPort is available if it is not being used by another application. Note the difference between the two examples is that the version for JDK 5.0 up uses [http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html generics]:

Revision as of 18:31, 8 January 2009

This code snippet shows how to retrieve the available comms ports on your computer. A CommPort is available if it is not being used by another application. Note the difference between the two examples is that the version for JDK 5.0 up uses generics:

JDK <= 1.4

   /**
     * @return    A HashSet containing the CommPortIdentifier for all serial ports that are not currently being used.
     */
    public static HashSet getAvailableSerialPorts() {
        HashSet h = new HashSet();
        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();
                    h.add(com);
                } catch (PortInUseException e) {
                    System.out.println("Port, "  + com.getName() +  ", is in use.");
                } catch (Exception e) {
                    System.err.println("Failed to open port " + com.getName());
                    e.printStackTrace();
                }
            }
        }
        return h;
    }

JDK >= 5.0

   /**
     * @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();
                    h.add(com);
                } catch (PortInUseException e) {
                    System.out.println("Port, "  + com.getName() + ", is in use.");
                } catch (Exception e) {
                    System.err.println("Failed to open port " +  com.getName());
                    e.printStackTrace();
                }
            }
        }
        return h;
    }
Personal tools