| AVR Z-LINKŪ |

# SerialStream.java

Go to the documentation of this file.

    00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
    00002 package serialInterface;
    00003
    00004 import java.io.DataInputStream;
    00005 import java.io.IOException;
    00006 import java.io.PrintStream;
    00007
    00008 import javax.comm.SerialPort;
    00009 import javax.comm.SerialPortEvent;
    00010 import javax.comm.SerialPortEventListener;
    00011 import javax.swing.event.EventListenerList;
    00029 public class SerialStream implements SerialPortEventListener{
    00030
    00031         private SerialPort serialPort;
    00032         private DataInputStream inputStream;
    00033         private PrintStream outputStream;
    00034
    00035         private boolean receptionDone;
    00036         private boolean streamEventEnabled;
    00037         private String  receivedOnStream;
    00038         private EventListenerList SerialStreamListeners = new EventListenerList( );
    00039
    00040
    00053         public SerialStream( SerialPort portToAppendStream ) throws SerialStreamException{
    00054
    00055                 //Open IO streams.
    00056                 try{
    00057
    00058                         inputStream = new DataInputStream( portToAppendStream.getInputStream( ) );
    00059                         outputStream = new PrintStream( portToAppendStream.getOutputStream( ), true );
    00060                 }catch( IOException ioe ){
    00061
    00062                         ioe.printStackTrace( );
    00063
    00064                         throw new SerialStreamException( "Not possible to open IO Streams." );
    00065                 }
    00066
    00067                 //Add this object instance as SerialPortEventListener.
    00068                 if( !SerialPortUtilities.addSerialEventListener( portToAppendStream, this) ){
    00069                         throw new SerialStreamException( "Not possible to listen to chosen serial port." );
    00070                 }
    00071
    00072                 //Ready to receive new responses and disable serial stream event generation.
    00073                 receptionDone = false;
    00074                 streamEventEnabled = false;
    00075         }
    00076
    00084         public void sendData( String data ){
    00085
    00086                 receptionDone = false;
    00087                 outputStream.println( data );
    00088         }
    00089
    00098         public String getData( ){
    00099
    00100                 if( receptionDone ){
    00101
    00102                         return receivedOnStream;
    00103                 }
    00104
    00105                 //Reception is not finished.
    00106                 else{
    00107                         return null;
    00108                 }
    00109         }
    00110
    00128         public String sendDataBlocking( String data ){
    00129
    00130                 this.sendData( data );
    00131
    00132 //              Wait until the response is termianted or an error occurs reading.
    00133                 while( !receptionDone );
    00134
    00135                 return receivedOnStream;
    00136         }
    00137
    00138
    00147         public SerialPort getSerialPortUsed( ){
    00148                 return serialPort;
    00149         }
    00150
    00151
    00157         public void enableSerialStreamEvents( ){
    00158                 streamEventEnabled = true;
    00159         }
    00160
    00161
    00167         public void disableSerialStreamEvents( ){
    00168                 streamEventEnabled = false;
    00169         }
    00170
    00171
    00177         public void resetReceiver( ){
    00178
    00179                 receptionDone = false;
    00180         }
    00181
    00195         public void serialEvent( SerialPortEvent spe ) {
    00196
    00197                 switch( spe.getEventType( ) ){
    00198
    00199                 //HW handshake not used.
    00200             case SerialPortEvent.BI:
    00201             case SerialPortEvent.OE:
    00202             case SerialPortEvent.FE:
    00203             case SerialPortEvent.PE:
    00204             case SerialPortEvent.CD:
    00205             case SerialPortEvent.CTS:
    00206             case SerialPortEvent.DSR:
    00207             case SerialPortEvent.RI:
    00208             case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
    00209                 break;
    00210
    00211             //Only react on new data available.
    00212             case SerialPortEvent.DATA_AVAILABLE:
    00213
    00214                 StringBuffer readBuffer = new StringBuffer( );
    00215                 int c;
    00216
    00217                 try{
    00218
    00219                         for( ; ( c = inputStream.read( ) ) != '\n'; ){
    00220                                 if( c!= '\r' )  readBuffer.append( ( char ) c);
    00221                                 }
    00222
    00223                         //Close stream.
    00224                         inputStream.close( );
    00225                         receptionDone = true;
    00226                         }catch( IOException ie ){
    00227
    00228                                 //Handle if we could not read from the serial port.
    00229                                 ie.printStackTrace( );
    00230                                 receptionDone = true;
    00231                                 receivedOnStream = "ERROR: Could not read from stream.";
    00232
    00233 //                              Send a serial stream event to possible listeners if enabled.
    00234                                 if( streamEventEnabled ){
    00235
    00236                                         fireSearialStreamEvent( new SerialStreamEvent( receivedOnStream ) );
    00237                                 }
    00238
    00239                                 return;
    00240                         }
    00241
    00242                         //Add response to the receivedOnStream string.
    00243                         receivedOnStream = readBuffer.toString( );
    00244
    00245                         //Send a serial stream event to possible listeners if enabled.
    00246                         if( streamEventEnabled ){
    00247                                 fireSearialStreamEvent( new SerialStreamEvent( receivedOnStream ) );
    00248                         }
    00249
    00250                 break;
    00251                 }
    00252         }
    00253
    00262         public void addSerialStreamEventListener( SerialStreamListener ssListener ){
    00263                 SerialStreamListeners.add( SerialStreamListener.class, ssListener );
    00264         }
    00265
    00274         public void removeSerialStreamListener( SerialStreamListener ssListener ){
    00275                 SerialStreamListeners.remove( SerialStreamListener.class, ssListener );
    00276         }
    00277
    00286         protected void fireSearialStreamEvent( SerialStreamEvent SerialStreamEvent ){
    00287
    00288                 Object[ ] listeners = SerialStreamListeners.getListenerList( );
    00289                 int numberOfListeners = listeners.length;
    00290
    00291                 for( int i = 0; i < numberOfListeners; i+=2 ){
    00292
    00293                         if( listeners[ i ] == SerialStreamListener.class ){
    00294                                 ( (SerialStreamListener)listeners[ i + 1 ] ).SerialStreamEvent( SerialStreamEvent );
    00295                         }
    00296                 }
    00297         }
    00298 }

@DOC_TITLE@