private void handleDataAvailable ( ) { InputStream inputStream = serialPort . getInputStream ( ) ; try { while ( inputStream . available ( ) > 0 ) { byte inputByte = ( byte ) inputStream . read ( ) ; if ( inputByte == - 1 ) { log . error ( "Reached end of stream trying to read serial port." ) ; stop ( ) ; return ; } Message message = configuration . getCommandParser ( ) . handleInputStream ( inputByte , inputStream ) ; if ( message != null ) { log . debug ( "Routing message {}" , message . getClass ( ) . getSimpleName ( ) ) ; routeMessage ( message ) ; } } } catch ( IOException e ) { log . error ( "IO Error reading from serial port. Closing connection." ) ; e . printStackTrace ( ) ; stop ( ) ; } }
Handles the SerialPort input stream and builds a message object if a detected CommandByte is discovered over the communications stream .