ikayaki.squid
Class SerialIO

java.lang.Object
  extended by ikayaki.squid.SerialIO
All Implemented Interfaces:
EventListener, javax.comm.SerialPortEventListener

public class SerialIO
extends Object
implements javax.comm.SerialPortEventListener

This class represents hardware layer to serial port communications.

Author:
Aki Sysmäläinen, Aki Korpua

Nested Class Summary
private static class SerialIO.LogEvent
          Logwriter event type
 
Field Summary
private static DateFormat dateFormat
           
private static boolean DEBUG
           
private  InputStream is
          Inputstream of this port
private  EventListenerList listenerList
          Listeners for this port.
private  BufferedWriter logWriter
          Logwriter buffer
private  boolean logWriterTriedCreate
          Have we tried to create the log writer?
private static Vector<SerialIO> openPorts
          All opened serial ports
private  OutputStream os
          Outputstream of this port
private  String portName
          Name of this port
private  javax.comm.SerialPort sPort
          This serial port
 
Constructor Summary
private SerialIO(SerialParameters parameters)
          Creates an instance of SerialIO which represents one serial port.
 
Method Summary
 void addSerialIOListener(SerialIOListener l)
          Adds a MeasurementListener to the project.
static void closeAllPorts()
          Closes all open serialports and their streams
 void closePort()
          Closes this serial port and it's streams
private  void debug(SerialIO.LogEvent e, String message)
          Debug logger.
private  void fireSerialIOEvent(String message)
          Notifies all listeners that have registered for MeasurementEvents.
 String getPortName()
           
static SerialIO openPort(SerialParameters parameters)
           
private  String padn(int n)
          Zero-paddes a number if it's <10.
 void removeSerialIOListener(SerialIOListener l)
          Removes a MeasurementListener from the project.
 void serialEvent(javax.comm.SerialPortEvent event)
          This method is run when a serial message is received from serial port.
 void writeMessage(String message)
          Writes an ASCII format message to serial port.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG

private static final boolean DEBUG
See Also:
Constant Field Values

dateFormat

private static final DateFormat dateFormat

openPorts

private static Vector<SerialIO> openPorts
All opened serial ports


listenerList

private EventListenerList listenerList
Listeners for this port.


sPort

private javax.comm.SerialPort sPort
This serial port


os

private OutputStream os
Outputstream of this port


is

private InputStream is
Inputstream of this port


portName

private String portName
Name of this port


logWriter

private BufferedWriter logWriter
Logwriter buffer


logWriterTriedCreate

private boolean logWriterTriedCreate
Have we tried to create the log writer? (Don't want to try again if it didn't work.)

Constructor Detail

SerialIO

private SerialIO(SerialParameters parameters)
          throws SerialIOException
Creates an instance of SerialIO which represents one serial port.

Parameters:
parameters - parameters for the serial port being opened.
Throws:
SerialIOException - if something goes wrong.
Method Detail

openPort

public static SerialIO openPort(SerialParameters parameters)
                         throws SerialIOException
Throws:
SerialIOException

writeMessage

public void writeMessage(String message)
                  throws SerialIOException
Writes an ASCII format message to serial port.

Parameters:
message - a message to be send
Throws:
SerialIOException - if exception occurs.

closePort

public void closePort()
Closes this serial port and it's streams


closeAllPorts

public static void closeAllPorts()
Closes all open serialports and their streams


serialEvent

public void serialEvent(javax.comm.SerialPortEvent event)
This method is run when a serial message is received from serial port. It generates a new SerialIOEvent.

Specified by:
serialEvent in interface javax.comm.SerialPortEventListener

addSerialIOListener

public void addSerialIOListener(SerialIOListener l)
Adds a MeasurementListener to the project.

Parameters:
l - the listener to be added.

removeSerialIOListener

public void removeSerialIOListener(SerialIOListener l)
Removes a MeasurementListener from the project.

Parameters:
l - the listener to be removed

fireSerialIOEvent

private void fireSerialIOEvent(String message)
Notifies all listeners that have registered for MeasurementEvents.

Parameters:
message -

debug

private void debug(SerialIO.LogEvent e,
                   String message)
Debug logger.

Parameters:
e - LogEvent type.
message - String to write; portname if e==SESSION_START.

padn

private String padn(int n)
Zero-paddes a number if it's <10.

Parameters:
n - int to pad.
Returns:
padded String.

getPortName

public String getPortName()