<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://rxtx.qbang.org/wiki/skins/common/feed.css?207"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>Rxtx - User contributions [en]</title>
		<link>http://rxtx.qbang.org/wiki/index.php/Special:Contributions/Ericmarshall</link>
		<description>From Rxtx</description>
		<language>en</language>
		<generator>MediaWiki 1.15.4</generator>
		<lastBuildDate>Sun, 28 Jun 2026 10:10:07 GMT</lastBuildDate>
		<item>
			<title>Discovering available comm ports</title>
			<link>http://rxtx.qbang.org/wiki/index.php/Discovering_available_comm_ports</link>
			<description>&lt;p&gt;Ericmarshall:&amp;#32;simplified code&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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. Note the differrence 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]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;JDK &amp;lt;= 1.4&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**&lt;br /&gt;
     * @return    A HashSet containing the CommPortIdentifier for all serial ports that are not currently being used.&lt;br /&gt;
     */&lt;br /&gt;
    public static HashSet getAvailableSerialPorts() {&lt;br /&gt;
        HashSet h = new HashSet();&lt;br /&gt;
        Enumeration thePorts = CommPortIdentifier.getPortIdentifiers();&lt;br /&gt;
        while (thePorts.hasMoreElements()) {&lt;br /&gt;
            CommPortIdentifier com = (CommPortIdentifier) thePorts.nextElement();&lt;br /&gt;
            switch (com.getPortType()) {&lt;br /&gt;
            case CommPortIdentifier.PORT_SERIAL:&lt;br /&gt;
                try {&lt;br /&gt;
                    CommPort thePort = com.open(&amp;quot;CommUtil&amp;quot;, 50);&lt;br /&gt;
                    thePort.close();&lt;br /&gt;
                    h.add(com);&lt;br /&gt;
                } catch (PortInUseException e) {&lt;br /&gt;
                    System.out.println(&amp;quot;Port, &amp;quot; + com.getName() + &amp;quot;, is in use.&amp;quot;);&lt;br /&gt;
                } catch (Exception e) {&lt;br /&gt;
                    System.err.println(&amp;quot;Failed to open port &amp;quot; + com.getName());&lt;br /&gt;
                    e.printStackTrace();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        return h;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;JDK &amp;gt;= 5.0&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   /**&lt;br /&gt;
     * @return    A HashSet containing the CommPortIdentifier for all serial ports that are not currently being used.&lt;br /&gt;
     */&lt;br /&gt;
    public static HashSet&amp;lt;CommPortIdentifier&amp;gt; getAvailableSerialPorts() {&lt;br /&gt;
        HashSet&amp;lt;CommPortIdentifier&amp;gt; h = new HashSet&amp;lt;CommPortIdentifier&amp;gt;();&lt;br /&gt;
        Enumeration thePorts = CommPortIdentifier.getPortIdentifiers();&lt;br /&gt;
        while (thePorts.hasMoreElements()) {&lt;br /&gt;
            CommPortIdentifier com = (CommPortIdentifier) thePorts.nextElement();&lt;br /&gt;
            switch (com.getPortType()) {&lt;br /&gt;
            case CommPortIdentifier.PORT_SERIAL:&lt;br /&gt;
                try {&lt;br /&gt;
                    CommPort thePort = com.open(&amp;quot;CommUtil&amp;quot;, 50);&lt;br /&gt;
                    thePort.close();&lt;br /&gt;
                    h.add(com);&lt;br /&gt;
                } catch (PortInUseException e) {&lt;br /&gt;
                    System.out.println(&amp;quot;Port, &amp;quot; + com.getName() + &amp;quot;, is in use.&amp;quot;);&lt;br /&gt;
                } catch (Exception e) {&lt;br /&gt;
                    System.err.println(&amp;quot;Failed to open port &amp;quot; + com.getName());&lt;br /&gt;
                    e.printStackTrace();&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        return h;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</description>
			<pubDate>Wed, 19 Sep 2007 21:01:45 GMT</pubDate>			<dc:creator>Ericmarshall</dc:creator>			<comments>http://rxtx.qbang.org/wiki/index.php/Talk:Discovering_available_comm_ports</comments>		</item>
		<item>
			<title>Discovering comm ports</title>
			<link>http://rxtx.qbang.org/wiki/index.php/Discovering_comm_ports</link>
			<description>&lt;p&gt;Ericmarshall:&amp;#32;added missing + signs&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This code snippet shows how to find out the available comms ports on your computer.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    static void listPorts()&lt;br /&gt;
    {&lt;br /&gt;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();&lt;br /&gt;
        while ( portEnum.hasMoreElements() ) &lt;br /&gt;
        {&lt;br /&gt;
            CommPortIdentifier portIdentifier = (CommPortIdentifier) portEnum.nextElement();&lt;br /&gt;
            System.out.println(portIdentifier.getName() + &amp;quot; - &amp;quot; + getPortTypeName(portIdentifier.getPortType()) );&lt;br /&gt;
        }        &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    static String getPortTypeName ( int portType )&lt;br /&gt;
    {&lt;br /&gt;
        switch ( portType )&lt;br /&gt;
        {&lt;br /&gt;
            case CommPortIdentifier.PORT_I2C:&lt;br /&gt;
                return &amp;quot;I2C&amp;quot;;&lt;br /&gt;
            case CommPortIdentifier.PORT_PARALLEL:&lt;br /&gt;
                return &amp;quot;Parallel&amp;quot;;&lt;br /&gt;
            case CommPortIdentifier.PORT_RAW:&lt;br /&gt;
                return &amp;quot;Raw&amp;quot;;&lt;br /&gt;
            case CommPortIdentifier.PORT_RS485:&lt;br /&gt;
                return &amp;quot;RS485&amp;quot;;&lt;br /&gt;
            case CommPortIdentifier.PORT_SERIAL:&lt;br /&gt;
                return &amp;quot;Serial&amp;quot;;&lt;br /&gt;
            default:&lt;br /&gt;
                return &amp;quot;unknown type&amp;quot;;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</description>
			<pubDate>Wed, 19 Sep 2007 20:52:13 GMT</pubDate>			<dc:creator>Ericmarshall</dc:creator>			<comments>http://rxtx.qbang.org/wiki/index.php/Talk:Discovering_comm_ports</comments>		</item>
		<item>
			<title>Discovering comm ports</title>
			<link>http://rxtx.qbang.org/wiki/index.php/Discovering_comm_ports</link>
			<description>&lt;p&gt;Ericmarshall:&amp;#32;revert vandalism&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This code snippet shows how to find out the available comms ports on your computer.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    static void listPorts()&lt;br /&gt;
    {&lt;br /&gt;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();&lt;br /&gt;
        while ( portEnum.hasMoreElements() ) &lt;br /&gt;
        {&lt;br /&gt;
            CommPortIdentifier portIdentifier = (CommPortIdentifier) portEnum.nextElement();&lt;br /&gt;
            System.out.println(portIdentifier.getName()   &amp;quot; - &amp;quot;   getPortTypeName(portIdentifier.getPortType()) );&lt;br /&gt;
        }        &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    static String getPortTypeName ( int portType )&lt;br /&gt;
    {&lt;br /&gt;
        switch ( portType )&lt;br /&gt;
        {&lt;br /&gt;
            case CommPortIdentifier.PORT_I2C:&lt;br /&gt;
                return &amp;quot;I2C&amp;quot;;&lt;br /&gt;
            case CommPortIdentifier.PORT_PARALLEL:&lt;br /&gt;
                return &amp;quot;Parallel&amp;quot;;&lt;br /&gt;
            case CommPortIdentifier.PORT_RAW:&lt;br /&gt;
                return &amp;quot;Raw&amp;quot;;&lt;br /&gt;
            case CommPortIdentifier.PORT_RS485:&lt;br /&gt;
                return &amp;quot;RS485&amp;quot;;&lt;br /&gt;
            case CommPortIdentifier.PORT_SERIAL:&lt;br /&gt;
                return &amp;quot;Serial&amp;quot;;&lt;br /&gt;
            default:&lt;br /&gt;
                return &amp;quot;unknown type&amp;quot;;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</description>
			<pubDate>Wed, 19 Sep 2007 20:36:19 GMT</pubDate>			<dc:creator>Ericmarshall</dc:creator>			<comments>http://rxtx.qbang.org/wiki/index.php/Talk:Discovering_comm_ports</comments>		</item>
	</channel>
</rss>