<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://rxtx.qbang.org/wiki/skins/common/feed.css?207"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://rxtx.qbang.org/wiki/index.php?feed=atom&amp;target=OucnaBasda&amp;title=Special%3AContributions</id>
		<title>Rxtx - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://rxtx.qbang.org/wiki/index.php?feed=atom&amp;target=OucnaBasda&amp;title=Special%3AContributions"/>
		<link rel="alternate" type="text/html" href="http://rxtx.qbang.org/wiki/index.php/Special:Contributions/OucnaBasda"/>
		<updated>2026-06-21T14:54:19Z</updated>
		<subtitle>From Rxtx</subtitle>
		<generator>MediaWiki 1.15.4</generator>

	<entry>
		<id>http://rxtx.qbang.org/wiki/index.php/Discovering_available_comm_ports</id>
		<title>Discovering available comm ports</title>
		<link rel="alternate" type="text/html" href="http://rxtx.qbang.org/wiki/index.php/Discovering_available_comm_ports"/>
				<updated>2008-02-09T05:53:32Z</updated>
		
		<summary type="html">&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;</summary>
		<author><name>OucnaBasda</name></author>	</entry>

	</feed>