Discovering available comm ports
From Rxtx
(Difference between revisions)
Line 1: | Line 1: | ||
- | + | This code snippet shows how to iretrive the available comms ports on your computer. A CommPort is available if it is not being used by another application: | |
+ | <b>JDK <= 1.4</b> | ||
+ | <pre> | ||
/** | /** | ||
* @return A HashSet containing the CommPortIdentifier for all serial ports that are not currently being used. | * @return A HashSet containing the CommPortIdentifier for all serial ports that are not currently being used. | ||
Line 28: | Line 30: | ||
return h; | return h; | ||
} | } | ||
+ | </pre> | ||
+ | |||
+ | <b>JDK >= 5.0</b> | ||
+ | <pre> | ||
+ | /** | ||
+ | * @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) { | ||
+ | if (log.isInfoEnabled()) { | ||
+ | log.info("Port, " + com.getName() + ", is in use."); | ||
+ | } | ||
+ | } catch (Exception e) { | ||
+ | if (log.isErrorEnabled()) { | ||
+ | log.error("Failed to open port " + com.getName(), e); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | return h; | ||
+ | } | ||
+ | </pre> |
Revision as of 17:04, 27 November 2006
This code snippet shows how to iretrive the available comms ports on your computer. A CommPort is available if it is not being used by another application:
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) { if (log.isInfoEnabled()) { log.info("Port, " + com.getName() + ", is in use."); } } catch (Exception e) { if (log.isErrorEnabled()) { log.error("Failed to open port " + com.getName(), e); } } } } 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) { if (log.isInfoEnabled()) { log.info("Port, " + com.getName() + ", is in use."); } } catch (Exception e) { if (log.isErrorEnabled()) { log.error("Failed to open port " + com.getName(), e); } } } } return h; }