Discovering comm ports
From Rxtx
(Difference between revisions)
(Enumeration -> Enumeration<CommPortIdentifier>) |
m (show rerequired import) |
||
Line 2: | Line 2: | ||
<pre> | <pre> | ||
+ | import gnu.io.*; | ||
static void listPorts() | static void listPorts() | ||
{ | { |
Revision as of 13:36, 6 May 2010
This code snippet shows how to find out the available comms ports on your computer.:
import gnu.io.*; static void listPorts() { java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers(); while ( portEnum.hasMoreElements() ) { CommPortIdentifier portIdentifier = portEnum.nextElement(); System.out.println(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()) ); } } static String getPortTypeName ( int portType ) { switch ( portType ) { case CommPortIdentifier.PORT_I2C: return "I2C"; case CommPortIdentifier.PORT_PARALLEL: return "Parallel"; case CommPortIdentifier.PORT_RAW: return "Raw"; case CommPortIdentifier.PORT_RS485: return "RS485"; case CommPortIdentifier.PORT_SERIAL: return "Serial"; default: return "unknown type"; } }