Discovering comm ports

From Rxtx

(Difference between revisions)
Jump to: navigation, search
 
(20 intermediate revisions not shown)
Line 2: Line 2:
<pre>
<pre>
 +
import gnu.io.*;
     static void listPorts()
     static void listPorts()
     {
     {
-
         Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
+
         java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
         while ( portEnum.hasMoreElements() )  
         while ( portEnum.hasMoreElements() )  
         {
         {
-
             CommPortIdentifier portIdentifier = (CommPortIdentifier) portEnum.nextElement();
+
             CommPortIdentifier portIdentifier = portEnum.nextElement();
-
             System.out.println(portIdentifier.getName()   " - "   getPortTypeName(portIdentifier.getPortType()) );
+
             System.out.println(portIdentifier.getName() " - " getPortTypeName(portIdentifier.getPortType()) );
         }         
         }         
     }
     }
Line 31: Line 32:
     }
     }
</pre>
</pre>
 +
 +
Please note that on Ubuntu 11.04, the Arduino Uno and possibly others are recognised as /dev/ttyACMxx .
 +
The RXTX library only searches through /dev/ttySxx, so you need to make symlinks if your distro does the same,
 +
so for example ln -s /dev/ttyACM0 /dev/ttyS33 .
 +
 +
Besides that, you need to close the serial port after starting, to prevent Linux from making new devices, like /dev/ttyACM2.
 +
Do not forget to remove the lock file from /var/lock if you forgot to close the port.

Latest revision as of 00:35, 15 June 2011

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";
        }
    }

Please note that on Ubuntu 11.04, the Arduino Uno and possibly others are recognised as /dev/ttyACMxx . The RXTX library only searches through /dev/ttySxx, so you need to make symlinks if your distro does the same, so for example ln -s /dev/ttyACM0 /dev/ttyS33 .

Besides that, you need to close the serial port after starting, to prevent Linux from making new devices, like /dev/ttyACM2. Do not forget to remove the lock file from /var/lock if you forgot to close the port.

Personal tools