stella.io
Class RS485ReadWriteServer.ReadWrite

java.lang.Object
  extended by java.lang.Thread
      extended by stella.io.RS485ReadWriteServer.ReadWrite
All Implemented Interfaces:
Runnable
Enclosing class:
RS485ReadWriteServer

protected class RS485ReadWriteServer.ReadWrite
extends Thread

A cancelable thread that does the following things on run.

To allow this thread to exit from hanging reads, other objects may interfere and set its cancel flag to true. This allows the thread to die prematurely. In this case, the serial line is cleared of any remaining bytes. These bytes are copied to the getResult() method. The correct execution of the thread can be queired with the isCancel() method.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
private  boolean busy
          The bsuy flag indicating that a read/write cycle is in progress.
private  boolean cancel
          The cancel flag.
private  String command
          The command to write to the serial line.
private  String result
          The result read-in from the serial line.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
protected RS485ReadWriteServer.ReadWrite(String cmd)
          Constructs a new, self-starting read/write thread.
 
Method Summary
 String getResult()
          Returns the result of the read associated with this thread.
 boolean isBusy()
          Returns the status of the busy flag.
 boolean isCancel()
          Returns the status of the cancel flag.
 void run()
          The run command.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

cancel

private boolean cancel
The cancel flag.


command

private String command
The command to write to the serial line.


result

private String result
The result read-in from the serial line.


busy

private boolean busy
The bsuy flag indicating that a read/write cycle is in progress.

Constructor Detail

RS485ReadWriteServer.ReadWrite

protected RS485ReadWriteServer.ReadWrite(String cmd)
Constructs a new, self-starting read/write thread. The command and the number of bytes must be supplied at the construction phase. Before issuing the start command, the busy flag is raised. This will only be reset after the command reply has been retrieved.

Method Detail

run

public void run()
The run command. It synchronizes on the enclosing class's lock object, writes the command to the serial line and blocks reading in until the expected number of bytes is available. Can be canceled from objects that have excess to this thread's protected cancel variable.

Specified by:
run in interface Runnable
Overrides:
run in class Thread

getResult

public String getResult()
Returns the result of the read associated with this thread. If no read has occured, like when canceled on waiting for bytes, this method returns null.

If this method is called while the reading thread is still alive, we join on the read thread for a maximum of RS485ReadWriteServer.KEY_RESPONDTIME ms. If the thread is still alife after that period, the read thread is canceld, and we join again on the thread until it really dies.


isCancel

public boolean isCancel()
Returns the status of the cancel flag. It may only become true, if the getResult() method received a time-out during waiting for the retrieval of the data. If this method is called prior to the getResult() method it will always return false.


isBusy

public boolean isBusy()
Returns the status of the busy flag. This thread is busy as long as the read/write cycle has not been finished with the retrieval of the command reply.