io
Class AbstractSerialDriver

java.lang.Object
  extended by util.PropertyContainer
      extended by io.AbstractDriver
          extended by io.AbstractSerialDriver
All Implemented Interfaces:
Driver, Cloneable, Initializable, PropertyBearing
Direct Known Subclasses:
MotorController, RS485ReadWriteServer, RS485Server, SerialDriver

public abstract class AbstractSerialDriver
extends AbstractDriver

A base class for any driver that wants to support serial access. The base method provides open/close capabilities, but does not define how a string or command is writtn to the serial line, nor how we read a response.


Nested Class Summary
protected  class AbstractSerialDriver.Available
          To catch the asynchroneous output, we use a serial event listener.
 
Nested classes/interfaces inherited from class util.PropertyContainer
PropertyContainer.URLResource
 
Field Summary
private static int DEFBAUDRATE
          The default baudrate to use.
private static long DEFCLOSETIMEOUT
          The default time out.
private static int DEFDATABIT
          The default databits to use.
private static int DEFFLOWCONTROL
          The default flow control.
private static int DEFINBUFFER
          The default value for the buffer size of the output-stream.
private static long DEFNOTIFYCOOLDOWN
          The default notify cool down time out.
private static int DEFOUTBUFFER
          The default value for the buffer size of the output-stream.
private static int DEFPARITYBIT
          The default parity bit to use.
private static String DEFPORTNAME
          The default name of the communications port.
private static int DEFRECEIVETHRESHOLD
          The default receiving threshold, zero is disabled.
private static int DEFRECEIVETIMEOUT
          The default receiving timeout, zero is disabled.
private static int DEFSTOPBIT
          The default stopbits to use.
private static int DEFTIMEOUT
          The default time out.
protected  InputStream inport
          The stream to the input from the serial line.
private  boolean isOpen
          Gets true once we successfully opened the serial port.
static String KEY_BAUDRATE
          The key to the baud rate.
static String KEY_CLOSETIMEOUT
          The key to the close timeout in ms.
static String KEY_DATABIT
          The key to the number of data bits.
static String KEY_FLOWCONTROL
          The key to the flow control integer mask.
static String KEY_INBUFFER
          The key to the buffer size of the input stream.
static String KEY_NOTIFYCOOLDOWN
          The key to the notify-read timeout in ms.
static String KEY_OUTBUFFER
          The key to the buffer size of the output stream.
static String KEY_PARITYBIT
          The key to the parity bit.
static String KEY_PORTNAME
          Serial communication port the unit attached to.
static String KEY_RECEIVEFRAMING
          The key to the framing byte.
static String KEY_RECEIVETHRESHOLD
          The key to the receive threshold byte number.
static String KEY_RECEIVETIMEOUT
          The key to the receive timeout ms number.
static String KEY_STOPBIT
          The key to the number of stop bits.
static String KEY_TIMEOUT
          The key to the timeout in ms.
private  int orgbaud
          The original baud rate of the serial port.
private  int orgdata
          The original data bit number of the serial connection.
private  int orgparity
          The original parity bit number of the serial connection.
private  int orgstop
          The original stop bit number of the serial connection.
protected  OutputStream outport
          The stream to the output of the serial line.
private  gnu.io.SerialPort serial
          The instance of the communication port for issuing command.
protected static Object staticlock
          A locking Object to prevent concurrent access to the serial port.
 
Fields inherited from class io.AbstractDriver
KEY_DRIVERNAME
 
Fields inherited from class util.PropertyContainer
KEY_LISTSEPARATOR, KEY_LOCALECOUNTRY, KEY_LOCALELANGUAGE, KEY_MAPKEYVALUECHAR, KEY_MAPSEPARATOR, KEY_NOINITONCREATE, KEY_RESOURCEBUNDLES
 
Fields inherited from interface util.PropertyBearing
CONFIG, KEY_CLASS, KEY_URLRESOURCES, KEY_URLUSECONFIG, KEY_URLUSECURRENT, KEY_URLUSEHOME
 
Constructor Summary
protected AbstractSerialDriver(Map info)
          Constructor.
 
Method Summary
 boolean close()
          Closes the serial port.
 gnu.io.SerialPortEventListener createPortEventListener(int count, Object notify)
          Creates a serial port event listener that listens to this input port.
 gnu.io.SerialPortEventListener createPortEventListener(Object notify)
          Creates a serial port event listener that listens to this input port.
 void deregisterSoleEventListener()
          Deregisters a serial event listener to the underlying serial port.
 void disableEvent(int type)
          Disables the specified event for passing to the sole event listener registered on this serial port.
 void enableEvent(int type)
          Enables the specified event for passing to the sole event listener registered on this serial port.
 String getDriverName()
          Returns the name of the server.
 boolean isOpen()
          Queries if we are already open.
 boolean open()
          Initializing the communication port.
abstract  String readString()
          Reads a string from the serial line.
 void registerSoleEventListener(gnu.io.SerialPortEventListener sel)
          Registers a serial event listener to the underlying serial port.
 boolean resetOriginalParameters()
          Resets the original serial port settings.
private  void setEvent(int type, boolean val)
          Sets the specific event type's notification property to the given boolean value.
abstract  String writeCommand(String cmd)
          Writes a command to the serial line and retrieves the coresponding reply.
abstract  String writeString(String cmd)
          Writes a single string to the serial line without waiting/reading of a response.
 
Methods inherited from class io.AbstractDriver
createDriver, equals, hashCode
 
Methods inherited from class util.PropertyContainer
augment, augment, augment, clone, createFrom, createFrom, createFrom, defaultBoolean, defaultChar, defaultDouble, defaultFloat, defaultInt, defaultLong, defaultObject, defaultObject, defaultProperties, defaultProperty, getAsBoolean, getAsChar, getAsDouble, getAsEnums, getAsFloat, getAsInt, getAsList, getAsLong, getAsMap, getAsMap, getAsObject, getAsObject, getLocalClassLoader, getLocalized, getLocalized, getLocalizedString, getLocalizedString, getProperties, getPropertiesToKey, getPropertiesToKey, getProperty, getResource, getResourceAsStream, getResourceFromKey, getResources, has, init, isNew, keyCreate, keyCreate, parseObject, reload, reload, removeProperty, rescanned, setObject, setProperties, setProperty, stringProperties, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

KEY_PORTNAME

public static final String KEY_PORTNAME
Serial communication port the unit attached to.

See Also:
Constant Field Values

KEY_TIMEOUT

public static final String KEY_TIMEOUT
The key to the timeout in ms.

See Also:
Constant Field Values

KEY_CLOSETIMEOUT

public static final String KEY_CLOSETIMEOUT
The key to the close timeout in ms.

See Also:
Constant Field Values

KEY_NOTIFYCOOLDOWN

public static final String KEY_NOTIFYCOOLDOWN
The key to the notify-read timeout in ms.

See Also:
Constant Field Values

KEY_BAUDRATE

public static final String KEY_BAUDRATE
The key to the baud rate.

See Also:
Constant Field Values

KEY_DATABIT

public static final String KEY_DATABIT
The key to the number of data bits.

See Also:
Constant Field Values

KEY_STOPBIT

public static final String KEY_STOPBIT
The key to the number of stop bits.

See Also:
Constant Field Values

KEY_PARITYBIT

public static final String KEY_PARITYBIT
The key to the parity bit.

See Also:
Constant Field Values

KEY_FLOWCONTROL

public static final String KEY_FLOWCONTROL
The key to the flow control integer mask.

See Also:
Constant Field Values

KEY_RECEIVEFRAMING

public static final String KEY_RECEIVEFRAMING
The key to the framing byte.

See Also:
Constant Field Values

KEY_RECEIVETHRESHOLD

public static final String KEY_RECEIVETHRESHOLD
The key to the receive threshold byte number.

See Also:
Constant Field Values

KEY_RECEIVETIMEOUT

public static final String KEY_RECEIVETIMEOUT
The key to the receive timeout ms number.

See Also:
Constant Field Values

KEY_OUTBUFFER

public static final String KEY_OUTBUFFER
The key to the buffer size of the output stream.

See Also:
Constant Field Values

KEY_INBUFFER

public static final String KEY_INBUFFER
The key to the buffer size of the input stream.

See Also:
Constant Field Values

DEFPORTNAME

private static final String DEFPORTNAME
The default name of the communications port.

See Also:
Constant Field Values

DEFNOTIFYCOOLDOWN

private static final long DEFNOTIFYCOOLDOWN
The default notify cool down time out.

See Also:
Constant Field Values

DEFTIMEOUT

private static final int DEFTIMEOUT
The default time out.

See Also:
Constant Field Values

DEFCLOSETIMEOUT

private static final long DEFCLOSETIMEOUT
The default time out.

See Also:
Constant Field Values

DEFBAUDRATE

private static final int DEFBAUDRATE
The default baudrate to use.

See Also:
Constant Field Values

DEFDATABIT

private static final int DEFDATABIT
The default databits to use.

See Also:
Constant Field Values

DEFSTOPBIT

private static final int DEFSTOPBIT
The default stopbits to use.

See Also:
Constant Field Values

DEFPARITYBIT

private static final int DEFPARITYBIT
The default parity bit to use.

See Also:
Constant Field Values

DEFFLOWCONTROL

private static final int DEFFLOWCONTROL
The default flow control.

See Also:
Constant Field Values

DEFRECEIVETHRESHOLD

private static final int DEFRECEIVETHRESHOLD
The default receiving threshold, zero is disabled.

See Also:
Constant Field Values

DEFRECEIVETIMEOUT

private static final int DEFRECEIVETIMEOUT
The default receiving timeout, zero is disabled.

See Also:
Constant Field Values

DEFOUTBUFFER

private static final int DEFOUTBUFFER
The default value for the buffer size of the output-stream.

See Also:
Constant Field Values

DEFINBUFFER

private static final int DEFINBUFFER
The default value for the buffer size of the output-stream.

See Also:
Constant Field Values

staticlock

protected static Object staticlock
A locking Object to prevent concurrent access to the serial port.


outport

protected OutputStream outport
The stream to the output of the serial line.


inport

protected InputStream inport
The stream to the input from the serial line.


serial

private gnu.io.SerialPort serial
The instance of the communication port for issuing command.


isOpen

private boolean isOpen
Gets true once we successfully opened the serial port.


orgbaud

private int orgbaud
The original baud rate of the serial port.


orgdata

private int orgdata
The original data bit number of the serial connection.


orgstop

private int orgstop
The original stop bit number of the serial connection.


orgparity

private int orgparity
The original parity bit number of the serial connection.

Constructor Detail

AbstractSerialDriver

protected AbstractSerialDriver(Map info)
Constructor. Opening of the serial port is delayed until init. Here only properties are read.

Method Detail

open

public boolean open()
             throws IOException
Initializing the communication port. Gets the port identifier, opens the port and the I/O streams and send start-up commands.

Returns:
True if initialization was successful.
Throws:
IOException

getDriverName

public String getDriverName()
Returns the name of the server. Queries the AbstractDriver.KEY_DRIVERNAME property.

Specified by:
getDriverName in interface Driver
Overrides:
getDriverName in class AbstractDriver

isOpen

public boolean isOpen()
Queries if we are already open. To avoid multiple init calls.

Returns:
True, if a previous initialization was successful

resetOriginalParameters

public boolean resetOriginalParameters()
Resets the original serial port settings.

Returns:
True if successful

close

public boolean close()
              throws IOException
Closes the serial port. Additionally, the I/O-streams are closed.

Returns:
True if clean-up was successful.
Throws:
IOException

registerSoleEventListener

public void registerSoleEventListener(gnu.io.SerialPortEventListener sel)
Registers a serial event listener to the underlying serial port. The user must additionally call the enableEvent(int) method after registering an event listener to actually pass events to the listener.

Note that the current implementation of the comm-API allows only a single event listener to be registered. Note also that after closing and re-opening of a serial port the listener has to be registered again.


deregisterSoleEventListener

public void deregisterSoleEventListener()
Deregisters a serial event listener to the underlying serial port. The user must call this method prior to adding a new serial port event listener to the underlying serial port.

Note that the current implementation of the comm-API allows only a single event listener to be registered. Note also that after closing the serial port the listener is deregistered automatically.


enableEvent

public void enableEvent(int type)
Enables the specified event for passing to the sole event listener registered on this serial port. Note that the event-coding integers are not single-bit integers therefore enabling different event s for a listener requires subsequent calls to this method.

Note that it is implementation depended whether some events are available at all.

See Also:
disableEvent(int)

disableEvent

public void disableEvent(int type)
Disables the specified event for passing to the sole event listener registered on this serial port. Note that the event-coding integers are not single-bit integers therefore enabling different event s for a listener requires subsequent calls to this method.

Note that it is implementation depended whether some events are available at all.

See Also:
enableEvent(int)

setEvent

private void setEvent(int type,
                      boolean val)
Sets the specific event type's notification property to the given boolean value.


writeCommand

public abstract String writeCommand(String cmd)
                             throws IOException
Writes a command to the serial line and retrieves the coresponding reply. Subclasses may throw an UnsupportedOperationException. A null return means failure.

Throws:
IOException

writeString

public abstract String writeString(String cmd)
                            throws IOException
Writes a single string to the serial line without waiting/reading of a response. Subclasses may throw an UnsupportedOperationException.

Returns:
The string as it was truely sent to the line, including LF etc.
Throws:
IOException

readString

public abstract String readString()
                           throws IOException
Reads a string from the serial line. Subclasses may throw an UnsupportedOperationException.

Throws:
IOException

createPortEventListener

public gnu.io.SerialPortEventListener createPortEventListener(Object notify)
Creates a serial port event listener that listens to this input port. After creation, the event type must be enabled on the serial line.

Parameters:
notify - An object that should be notified on the event.

createPortEventListener

public gnu.io.SerialPortEventListener createPortEventListener(int count,
                                                              Object notify)
Creates a serial port event listener that listens to this input port. After creation, the event type must be enabled on the serial line.

Parameters:
count - The minimum number of bytes that have to be available.
notify - An object that should be notified on the event.