public class RS485ReadWriteServer extends AbstractSerialDriver implements ExitCleaning
Applications should only use the writeCommand(java.lang.String) method to write
commands to the serial line and read in replies (the reply is returned as a
string from the method call). This method locks on the serial port until the
reply is received. Please use this method with the correct number of
reply-bytes specified whenever possible. Calling this method with a too-small
number of reply-bytes specified can cause premature read aborts (though they
should not happen frequently). A generic methods, private, is provided that
reads to the serial line, namely writeString(java.lang.String). This method does
not lock on the serial port and should therefore be used with
extreme care. The suggested approach is to not use them at all, but if
it is really necessary, always synchronize on the #lock object prior
to calling either of this methods.
The serial I/O-streams are buffered by the underlying implementation, but are nevertheless software-wise wrapped into buffered streams. The read-in of the command always sets the CommPort.enableReceiveThreshold of the comm extension package to the number of bytes awaited for the command. Additionally, if the driver does not support receive-thresholds, a software implementation of a receive threshold is implemented.
If the KEY_CLOSE key points to true, the serial port is closed on
exit via a shutdown-hook.
In version 1.2 (20-01-03), a counter for failed reading was introduced. The
KEY_FAILURES points to a maximum number of failures allowed within a
KEY_FAILTIME to consider the server working probably. If this number
is exceeded (within the stated time), the serial port is closed and opened
again after the KEY_REINIT time.
| Modifier and Type | Class and Description |
|---|---|
static class |
RS485ReadWriteServer.Checksum
This innner class reports the checksum to the given command line
argument.
|
static class |
RS485ReadWriteServer.Command
Sends a single command to the server stated on the command line.
|
static class |
RS485ReadWriteServer.Info
Tries to cat info on the specified port to system.out.
|
protected class |
RS485ReadWriteServer.ReadWrite
A cancelable thread that does the following things on run.
|
static class |
RS485ReadWriteServer.Scan
Scans for valid serial devices and reports all found to system.out.
|
AbstractSerialDriver.Available| Modifier and Type | Field and Description |
|---|---|
private boolean |
checksumming
True if checksumming is enabled.
|
private static RS485ReadWriteServer.ReadWrite |
commandthread
The server's read/write thread for command issuing.
|
private byte[] |
crbytes
The byte array representation of the CR.
|
private static int |
DEFCHECKCHAR
The default number of checksum bytes (2).
|
private static boolean |
DEFCHECKSUM
The default value for checksummed communication (false).
|
private static boolean |
DEFCLOSE
True if the serial port should be closed on java-VM exits.
|
private static String |
DEFCR
The default carriage return character.
|
private static boolean |
DEFECHO
True if the command issued is echoed on the result read.
|
private static String |
DEFINVALID
The default invalid-command marker.
|
private static int |
DEFMAXBUFFER
The default value for the read buffer size.
|
private static int |
DEFMAXBUSY
The default number of retries of non-busy queries to the serial .
|
private static long |
DEFQUERYTIME
The default query time.
|
private static long |
DEFRESPONDTIME
The default respond time, 1 sec.
|
private static long |
DEFTIMEBUSY
The default time-out between non-busy queries to the serial server.
|
static String |
KEY_CHECKCHAR
The key whether to enable checksumming.
|
static String |
KEY_CHECKSUM
The key whether to enable checksumming.
|
static String |
KEY_CLOSE
The key for closing the serial line on exit with a shutdown hook.
|
static String |
KEY_CR
The key to the terminal character(s) written after a string.
|
static String |
KEY_ECHO
The key to echo-on.
|
static String |
KEY_FAILTIME
The key to the time span until failures are considered invalid.
|
static String |
KEY_FAILURES
The key for the maximum number of failures allowed.
|
static String |
KEY_INVALID
The key linked to the starting sequence of invalid commands.
|
static String |
KEY_MAXBUFFER
The key to the buffer size of the read buffer.
|
static String |
KEY_MAXBUSY
The maximum retries for non-busy queries to the serial server.
|
static String |
KEY_QUERYTIME
The key for the inputstream.available queries (ms).
|
static String |
KEY_REINIT
The key to the time between closing and re-opening the serial port.
|
static String |
KEY_RESPONDTIME
The key for the response-reading timeout (ms).
|
static String |
KEY_STARTUP
The key to a comma-separated list of commands to send on open.
|
static String |
KEY_TIMEBUSY
The time-out between non-busy queries to the serial server.
|
private byte[] |
readbytes
The byte array representation of the input.
|
inport, KEY_BAUDRATE, KEY_CLOSETIMEOUT, KEY_DATABIT, KEY_FLOWCONTROL, KEY_INBUFFER, KEY_NOTIFYCOOLDOWN, KEY_OUTBUFFER, KEY_PARITYBIT, KEY_PORTNAME, KEY_RECEIVEFRAMING, KEY_RECEIVETHRESHOLD, KEY_RECEIVETIMEOUT, KEY_STOPBIT, KEY_TIMEOUT, LOCK, outport, STATICLOCKKEY_DRIVERNAMEKEY_LISTSEPARATOR, KEY_MAPKEYVALUECHAR, KEY_MAPSEPARATORCONFIG, KEY_CLASS| Constructor and Description |
|---|
RS485ReadWriteServer(Map prop)
Constructs a new RS485 server.
|
| Modifier and Type | Method and Description |
|---|---|
void |
cancelCommand()
Cancels the command-read thread.
|
String |
deferCommand(String cmd)
Writes a command to the serial port and reads in the resonse.
|
protected String |
endCommand()
For subclasses only: Read the result of a previous command on clean the
line.
|
String |
ensureCommand(String cmd,
int retries)
For very faulty serial lines, this method should ensure that a command is
sent.
|
void |
exit()
The finalizer of the RS485 server.
|
protected static byte[] |
getCheckSum(byte[] send,
int len)
Calculates a checksum over a number of bytes.
|
boolean |
getCheckSumming()
Returns if checksumming is enabled.
|
private String |
interpretString(String raw)
Interprets a string read in from the serial line.
|
boolean |
isReadAlive()
Queries the server if it is busy writing a command.
|
boolean |
isReading()
Queries the server if it is busy writing a command.
|
boolean |
open()
Opens the serial port and additionally sets the baud rate, the data bit,
stop bit, and parity bit.
|
String |
readString()
If we have bytes on input, read them, otherwise return null.
|
void |
setCheckSumming(boolean newcheck)
Sets the checksumming option.
|
protected void |
startCommand(String cmd)
For subclasses only: Start the command thread and return.
|
String |
writeCommand(String cmd)
Writes a command to the serial port and reads in the resonse.
|
String |
writeString(String mess)
Writes the argumental string to the output stream.
|
close, createPortEventListener, createPortEventListener, deregisterSoleEventListener, disableEvent, enableEvent, getDriverName, init, isOpen, registerSoleEventListener, resetOriginalParameterscreateDriver, equals, hashCodeaugment, augment, augment, augment, augment, clone, defaultBoolean, defaultChar, defaultDouble, defaultFloat, defaultInt, defaultLong, defaultObject, defaultObject, defaultProperties, defaultProperty, getAsBoolean, getAsChar, getAsDouble, getAsEnums, getAsFloat, getAsInt, getAsList, getAsLong, getAsMap, getAsMap, getAsObject, getAsObject, getProperties, getProperty, has, initProperties, isNew, parseObject, parseObject, reload, reload, removeProperty, requires, rescanned, setObject, setProperties, setProperty, stringProperties, toStringpublic static final String KEY_MAXBUFFER
public static final String KEY_CR
public static final String KEY_CHECKSUM
public static final String KEY_CHECKCHAR
public static final String KEY_INVALID
public static final String KEY_STARTUP
public static final String KEY_QUERYTIME
public static final String KEY_RESPONDTIME
public static final String KEY_CLOSE
public static final String KEY_FAILURES
public static final String KEY_FAILTIME
public static final String KEY_REINIT
public static final String KEY_ECHO
public static final String KEY_MAXBUSY
public static final String KEY_TIMEBUSY
private static final int DEFMAXBUFFER
private static final String DEFCR
private static final boolean DEFCHECKSUM
private static final int DEFCHECKCHAR
private static final String DEFINVALID
private static final long DEFQUERYTIME
private static final long DEFRESPONDTIME
private static final boolean DEFCLOSE
private static final boolean DEFECHO
private static final int DEFMAXBUSY
private static final long DEFTIMEBUSY
private boolean checksumming
private final byte[] readbytes
private final byte[] crbytes
private static RS485ReadWriteServer.ReadWrite commandthread
public RS485ReadWriteServer(Map prop)
AbstractSerialDriver.init() method is called, as specified in the Driver
interface.public boolean open()
throws IOException
#resetClose method. If successful input and output
streams available at the port are opened.open in interface Driveropen in class AbstractSerialDriverIOExceptionpublic void exit()
AbstractSerialDriver.close().exit in interface ExitCleaningpublic void setCheckSumming(boolean newcheck)
public boolean getCheckSumming()
public String writeCommand(String cmd) throws IOException
RS485ReadWriteServer.ReadWrite. This thread is also responsible for avoiding
deadlocking. If the timer runs out or the writing of the command fails
for any reason null is returned and should be treated
accordingly in the calling method. This method cannot be used on serial
devices that do not return a reply on commands sent.
writeCommand in class AbstractSerialDrivercmd - The command to writecount - The number of bytes to read as a command reply. Zero for
unspecified numbers.IOExceptionpublic String deferCommand(String cmd) throws IOException
KEY_INVALID
the string read in is returned. The difference to the
writeCommand(java.lang.String) is that this method defers command sending if the
server is currently writing. This is the more gentle way of writing a
command. As from version 1.1, the defer command waits at max.
KEY_MAXBUSY times KEY_TIMEBUSY if the serial server is
busy.IOExceptionpublic String ensureCommand(String cmd, int retries) throws IOException
deferCommand(java.lang.String) until this method
returns non-null. If deferring commands fails for the stated maximum
number of retries, a warning is issued to syslog.retries - If zero, try forever.IOExceptionprotected void startCommand(String cmd)
protected String endCommand()
public void cancelCommand()
public boolean isReading()
RS485ReadWriteServer.ReadWrite thread stored in it's
private commandthread field. If this reference is valid and
return true, true is returned.public boolean isReadAlive()
RS485ReadWriteServer.ReadWrite thread stored in it's
private commandthread field. If this reference is valid and
return true, true is returned.public String writeString(String mess) throws IOException
KEY_CR character(s).
Note that this method calls flush on the output stream after writing the String as a series of bytes to the port.
This method does not synchronize on the serial port! External
synchronization by locking on the #lock object is necessary, if
this method is used outside of the writeCommand(java.lang.String) method.
writeString in class AbstractSerialDriverIOExceptionpublic String readString() throws IOException
readString in class AbstractSerialDriverIOExceptionprivate String interpretString(String raw) throws IOException
IOExceptionprotected static final byte[] getCheckSum(byte[] send,
int len)