com.alarexgroup.m2mplf.adapters.serial
Class SerialAdapter

java.lang.Object
  extended by com.alarexgroup.m2mplf.adapters.serial.SerialAdapter
public final class SerialAdapter
extends java.lang.Object

Adapter provides the serial communication


Example:

import com.alarexgroup.m2mplf.adapters.serial.SerialAdapter;
import com.alarexgroup.m2mplf.adapters.serial.SerialAdapterListener;

public class MyM2Mapp extends M2Mlet implements SerialAdapterListener{
.
.
.



    serialAdapter = SerialAdapter.getSerialAdapter(this,1);
    serialAdapter.openPort("comm:com0;baudrate=4800",DATA_SEPARATOR_CR_LF);
    serialAdapter.writeData("Hello, I'am Siemens module!\r\n");

public void onSerialDataReceived(byte[] receivedData, int adapterId) {
     serialAdapter.writeData(receivedData);
}

or

    serialAdapter = SerialAdapter.getSerialAdapter(this,1);
    serialAdapter.openPort("comm:com0;baudrate=4800");
    serialAdapter.writeData("Hello, I'am Siemens module!\r\n");

public void onSerialDataReceivedInStream(InputStream receivedData, int adapterId) {
     int nums;
     try {
          nums = receivedData.available();
          byte[] b = new byte[nums];
          receivedData.read(b);
          serialAdapter.writeData(b);
     } catch (IOException ex) {
          ex.printStackTrace();
     }

}

Parameters

Parameters Default: Description
baudrate Platform independent Data speed
bitsperchar 8 Character encoding (7 or 8 bits for character.
stopbits 1 Number of stop bits(1 or 2)
parity none Parity: odd, even, or none.
blocking on If blocking is set to on, it waits for filling up the whole buffer of this device.
autocts on If autocts is set to on, it waits for CTS signal before data writingm.
autorts on If autorts is set to on, RTS signals incomming data.
In case of off, RTS is still turned on.

[SIEMENS WM ]
TC65 has following restrictions:
baudrate: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200, 230400, 460800
stopbits: only 1 is supported
parity: 7N1 is not supported

Format of the parameter string is as follows:

<comm_connection_string> ::= "comm:"<port_id>[<options_list>] ;
<port_id> ::= string of characters and numbers
<options_list> ::= *(<baud_rate_string>| <bitsperchar>| <stopbits>| <parity>| <blocking>| <autocts>| <autorts>) ;
; in case of duplicite parameters
; the last one will be used
<baud_rate_string> ::= ";baudrate="<baud_rate>
<baud_rate> ::= string of digits
<bitsperchar> ::= ";bitsperchar="<bit_value>
<bit_value> ::= "7" | "8"
<stopbits> ::= ";stopbits="<stop_value>
<stop_value> ::= "1" | "2"
<parity> ::= ";parity="<parity_value>
<parity_value> ::= "even" | "odd" | "none"
<blocking> ::= ";blocking="<on_off>
<autocts> ::= ";autocts="<on_off>
<autorts> ::= ";autorts="<on_off>
<on_off> ::= "on" | "off"

Field Summary
static byte[] DATA_SEPARATOR_CR
          Data block closing sequence: CR - 13
static byte[] DATA_SEPARATOR_CR_LF
          Data block closing sequence: CRLF - 13,10 = 0D,0A in HEX
static byte[] DATA_SEPARATOR_ESCAPE
          Data block closing sequence: ESC - 27
static byte[] DATA_SEPARATOR_ZERO_BYTE
          Data block closing sequence: 0
static int longSleep
           
static int shortSleep
           
static int timeForSleep
           
 
Constructor Summary
SerialAdapter()
           
 
Method Summary
 void close()
          Terminates this adapter's work
 boolean closePort()
          Method stops serail communication.You can reestablish the connection with openPort methods.
 java.io.InputStream getInputStream()
          Method returns opened InputStream for data receiving from serial port.
 java.io.OutputStream getOutputStream()
          Method returns opened OutputStream used for sending data to serial port.
static SerialAdapter getSerialAdapter(SerialAdapterListener listener, int adapterId)
          Method creates adapter and returns referrence.
 boolean openPort(java.lang.String parameters)
          Opens communications port with given parameters.
 boolean openPort(java.lang.String parameters, byte[] dataSeparator)
          Opens communications port with given parameters.
 boolean openPort(java.lang.String parameters, byte[] dataSeparator, long timeOut)
          Opens communications port with given parameters.
 void setPriority(int priority)
          Priority setting of SerialAdapter's internal thread
 boolean writeData(byte[] data)
          Writes a byte array to serial port
 boolean writeData(byte[] data, int offset, int length)
          Writes length of bytes from a byte array to serial port starting at offset
 boolean writeData(java.io.InputStream ist, int length)
          Method writes content of the inputstream to serial port.
 boolean writeData(int onebyte)
          Writes a single byte to serial port
 boolean writeData(java.lang.String data)
          Writes a string to serial port
 byte[] writeDataAndReadResponse(byte[] data)
          Method writes data to serial port and returns response.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DATA_SEPARATOR_CR_LF

public static final byte[] DATA_SEPARATOR_CR_LF
Data block closing sequence: CRLF - 13,10 = 0D,0A in HEX

DATA_SEPARATOR_ZERO_BYTE

public static final byte[] DATA_SEPARATOR_ZERO_BYTE
Data block closing sequence: 0

DATA_SEPARATOR_CR

public static final byte[] DATA_SEPARATOR_CR
Data block closing sequence: CR - 13

DATA_SEPARATOR_ESCAPE

public static final byte[] DATA_SEPARATOR_ESCAPE
Data block closing sequence: ESC - 27

shortSleep

public static int shortSleep

longSleep

public static int longSleep

timeForSleep

public static int timeForSleep
Constructor Detail

SerialAdapter

public SerialAdapter()
Method Detail

getSerialAdapter

public static SerialAdapter getSerialAdapter(SerialAdapterListener listener,
                                             int adapterId)
Method creates adapter and returns referrence. There is only single adapter, future calls return just the instance referrence.

Parameters:
adapterId - id of this adapteru. Lets you use multiple serial ports.
listener -
Returns:
Reference on serial adapter
See Also:
SerialAdapter, SerialAdapterListener

openPort

public boolean openPort(java.lang.String parameters,
                        byte[] dataSeparator)
Opens communications port with given parameters.
Method SerialAdapterListener.onSerialDataReceived(byte[], int) is called by adapter if there was a received data with closing sequence. See DATA_SEPARATOR_CR_LF and more.

Parameters:
dataSeparator - Mark for deliberating the end of the incomming data
This mark can be random character sequence. Often used sequences are: CRLF or CR, e.g. in GPS receiver devices etc.
After receiving the mark the mehod SerialAdapterListener.onSerialDataReceived(byte[], int) is called for hanling the incomming data.
parameters - Parameters of opened serial prot
Example: "comm:com0;baudrate=4800"
Returns:
true - port was succesfully opened
false - port can not be opened

openPort

public boolean openPort(java.lang.String parameters,
                        byte[] dataSeparator,
                        long timeOut)
Opens communications port with given parameters.
Method SerialAdapterListener.onSerialDataReceived(byte[], int) is called by adapter if there was a received data with closing sequence. See DATA_SEPARATOR_CR_LF and more.

Parameters:
dataSeparator - Mark for deliberating the end of the incomming data
This mark can be random character sequence. Often used sequences are: CRLF or CR, e.g. in GPS receiver devices etc.
After receiving the mark the mehod SerialAdapterListener.onSerialDataReceived(byte[], int) is called for hanling the incomming data.
parameters - Parameters of opened serial prot
Example: "comm:com0;baudrate=4800"
timeOut - timeout in miliseconds. If there were a data received but not closde by a closing char sequence, they will be processed as if they were closed after the timeout expires.
Returns:
true - port was succesfully opened
false - port can not be opened

openPort

public boolean openPort(java.lang.String parameters)
Opens communications port with given parameters.
Method SerialAdapterListener.onSerialDataReceived(byte[], int) is called by adapter if there was received at least a single byte.

Parameters:
parameters - Parameters of opened serial prot
Example: "comm:com0;baudrate=4800"
Returns:
true - port was succesfully opened
false - port can not be opened

close

public void close()
Terminates this adapter's work

closePort

public boolean closePort()
Method stops serail communication.You can reestablish the connection with openPort methods.

Returns:
success

setPriority

public void setPriority(int priority)
Priority setting of SerialAdapter's internal thread

Parameters:
priority - Thread priority

getOutputStream

public java.io.OutputStream getOutputStream()
Method returns opened OutputStream used for sending data to serial port.

Returns:
OutputStream

getInputStream

public java.io.InputStream getInputStream()
Method returns opened InputStream for data receiving from serial port.

Returns:
InputStream
Since:
1.3

writeData

public boolean writeData(byte[] data)
Writes a byte array to serial port

Parameters:
data - byte array
Returns:
true in case of success

writeData

public boolean writeData(byte[] data,
                         int offset,
                         int length)
Writes length of bytes from a byte array to serial port starting at offset

Parameters:
offset - position, from which the data will be sent inclusive
length - length of sent data
data - source byte array
Returns:
true if success

writeData

public boolean writeData(java.lang.String data)
Writes a string to serial port

Parameters:
data - text
Returns:
true if success

writeData

public boolean writeData(int onebyte)
Writes a single byte to serial port

Parameters:
onebyte -
Returns:
TRUE - success

writeData

public boolean writeData(java.io.InputStream ist,
                         int length)
Method writes content of the inputstream to serial port.

Parameters:
ist - InputStream with data for serial writing
length - Number of bytes to write
Returns:
True - success

writeDataAndReadResponse

public byte[] writeDataAndReadResponse(byte[] data)
Method writes data to serial port and returns response.

Parameters:
data - Dta for writing
Returns:
response