<?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/OucnaBasda</link>
		<description>From Rxtx</description>
		<language>en</language>
		<generator>MediaWiki 1.15.4</generator>
		<lastBuildDate>Sun, 21 Jun 2026 13:30:12 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;OucnaBasda:&amp;#32;libora&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;ornoricoug&lt;br /&gt;
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]:&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>Sat, 09 Feb 2008 05:53:32 GMT</pubDate>			<dc:creator>OucnaBasda</dc:creator>			<comments>http://rxtx.qbang.org/wiki/index.php/Talk:Discovering_available_comm_ports</comments>		</item>
	</channel>
</rss>