Discovering comm ports

From Rxtx

(Difference between revisions)
Jump to: navigation, search
(Reverting to last version not containing links to sulei-ka.ifrance.com)
Line 4: Line 4:
     static void listPorts()
     static void listPorts()
     {
     {
-
         Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
+
         java.util.Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
         while ( portEnum.hasMoreElements() )  
         while ( portEnum.hasMoreElements() )  
         {
         {
             CommPortIdentifier portIdentifier = (CommPortIdentifier) portEnum.nextElement();
             CommPortIdentifier portIdentifier = (CommPortIdentifier) portEnum.nextElement();
-
             System.out.println(portIdentifier.getName()   " - "   getPortTypeName(portIdentifier.getPortType()) );
+
             System.out.println(portIdentifier.getName() " - " getPortTypeName(portIdentifier.getPortType()) );
         }         
         }         
     }
     }

Revision as of 22:22, 13 November 2007

This code snippet shows how to find out the available comms ports on your computer.:

    static void listPorts()
    {
        java.util.Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
        while ( portEnum.hasMoreElements() ) 
        {
            CommPortIdentifier portIdentifier = (CommPortIdentifier) 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";
        }
    }
Personal tools