Discovering available comm ports

From Rxtx

(Difference between revisions)
Jump to: navigation, search
(Reverting to last version not containing links to keepthefight.50webs.com/description/immagini-folletti.htm)
m (Just fixed the examples so the will actually compile.)
Line 18: Line 18:
                     h.add(com);
                     h.add(com);
                 } catch (PortInUseException e) {
                 } catch (PortInUseException e) {
-
                     System.out.println("Port, "   com.getName()   ", is in use.");
+
                     System.out.println("Port, " + com.getName() ", is in use.");
                 } catch (Exception e) {
                 } catch (Exception e) {
                     System.err.println("Failed to open port "  com.getName());
                     System.err.println("Failed to open port "  com.getName());
Line 46: Line 46:
                     h.add(com);
                     h.add(com);
                 } catch (PortInUseException e) {
                 } catch (PortInUseException e) {
-
                     System.out.println("Port, "   com.getName()   ", is in use.");
+
                     System.out.println("Port, " + com.getName() + ", is in use.");
                 } catch (Exception e) {
                 } catch (Exception e) {
                     System.err.println("Failed to open port "  com.getName());
                     System.err.println("Failed to open port "  com.getName());

Revision as of 07:18, 28 November 2007

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