public static LibraryAdapter open ( final Logger logger , final String portId , final int baudrate , final int idleTimeout ) throws KNXException { Throwable t = null ; // check for Java ME Embedded platform and available serial communication port, // protocol support for communication ports is optional if ( CommConnectionAdapter . isAvailable ( ) ) { logger . debug ( "open Java ME serial port connection (CommConnection) for {}" , portId ) ; try { return new CommConnectionAdapter ( logger , portId , baudrate ) ; } catch ( final KNXException e ) { t = e ; } } // check internal support for serial port access // protocol support available for Win 32/64 platforms // (so we provide serial port access at least on platforms with ETS) if ( SerialComAdapter . isAvailable ( ) ) { logger . debug ( "open Calimero native serial port connection (serialcom) for {}" , portId ) ; SerialComAdapter conn = null ; try { conn = new SerialComAdapter ( logger , portId ) ; conn . setBaudRate ( baudrate ) ; //final int idleTimeout = idleTimeout(conn.getBaudRate()); conn . setTimeouts ( new SerialComAdapter . Timeouts ( idleTimeout , 0 , 250 , 0 , 0 ) ) ; conn . setParity ( SerialComAdapter . PARITY_EVEN ) ; conn . setControl ( SerialComAdapter . STOPBITS , SerialComAdapter . ONE_STOPBIT ) ; conn . setControl ( SerialComAdapter . DATABITS , 8 ) ; conn . setControl ( SerialComAdapter . FLOWCTRL , SerialComAdapter . FLOWCTRL_NONE ) ; logger . debug ( "setup serial port: baudrate " + conn . getBaudRate ( ) + ", even parity, " + conn . getControl ( SerialComAdapter . DATABITS ) + " databits, " + conn . getControl ( SerialComAdapter . STOPBITS ) + " stopbits, timeouts: " + conn . getTimeouts ( ) ) ; return conn ; } catch ( final IOException e ) { if ( conn != null ) conn . close ( ) ; t = e ; } } try { final Class < ? > c = Class . forName ( "tuwien.auto.calimero.serial.RxtxAdapter" ) ; logger . debug ( "using rxtx library for serial port access" ) ; final Class < ? extends LibraryAdapter > adapter = LibraryAdapter . class ; return adapter . cast ( c . getConstructors ( ) [ 0 ] . newInstance ( new Object [ ] { logger , portId , Integer . valueOf ( baudrate ) } ) ) ; } catch ( final ClassNotFoundException e ) { logger . warn ( "no rxtx library adapter found" ) ; } catch ( final Exception | NoClassDefFoundError e ) { t = e instanceof InvocationTargetException ? e . getCause ( ) : e ; } if ( t instanceof KNXException ) throw ( KNXException ) t ; if ( t != null ) throw new KNXException ( "failed to open serial port " + portId , t ) ; throw new KNXException ( "no serial adapter available to open " + portId ) ; }
Factory method to open a serial connection using one of the available library adapters .