org.zu.ardulink.connection.serial
Class AbstractSerialConnection

java.lang.Object
  extended by org.zu.ardulink.connection.serial.AbstractSerialConnection
All Implemented Interfaces:
Connection
Direct Known Subclasses:
BluetoothConnection, SerialConnection

public abstract class AbstractSerialConnection
extends Object
implements Connection

Ardulink - return to homepage

v0.6.1 Magnum PI

Used to simplify communication. One connection per instance of this class can be handled. A separate Thread is started to handle messages that are being received over the Serial interface. This class also makes packages out of a stream of bytes received, using a divider, and sending these packages as an array of ints (each between 0 and 255) to a function implemented by a class implementing the org.zu.ardulink.connection.ConnectionContact-interface.


Field Summary
static String DEFAULT_CONNECTION_NAME
           
 
Constructor Summary
AbstractSerialConnection()
          Just as (@link AbstractSerialConnection(ConnectionContact), but with null contact.
AbstractSerialConnection(ConnectionContact contact)
          Just as #AbstractSerialConnection(int, ConnectionContact, int), but with a default divider of 255 and a default id of DEFAULT_CONNECTION_NAME.
AbstractSerialConnection(String id)
          Just as (@link #AbstractSerialConnection(int, ConnectionContact, int), but with null contact and default divider.
AbstractSerialConnection(String id, ConnectionContact contact)
          Just as #AbstractSerialConnection(int, ConnectionContact, int), but with a default divider of 255.
AbstractSerialConnection(String id, ConnectionContact contact, int divider)
           
AbstractSerialConnection(String id, int divider)
          Just as (@link #AbstractSerialConnection(int, ConnectionContact, int), but with null contact.
 
Method Summary
abstract  boolean connect(Object... params)
           
abstract  boolean disconnect()
           
 ConnectionContact getContact()
           
 String getId()
           
abstract  List<String> getPortList()
          This method is used to get a list of all the available Serial ports (note: only Serial ports are considered).
 boolean isConnected()
           
 void setConnectionContact(ConnectionContact connectionContact)
           
 void startReader()
           
 void stopReader()
           
 void writeLog(String text)
           
 boolean writeSerial(int numBytes, int[] message)
          If a connection is open, an int between 0 and 255 (except the divider) can be sent over the Serial port using this function.
 boolean writeSerial(String message)
          This method is included as a legacy.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CONNECTION_NAME

public static final String DEFAULT_CONNECTION_NAME
See Also:
Constant Field Values
Constructor Detail

AbstractSerialConnection

public AbstractSerialConnection(String id,
                                ConnectionContact contact,
                                int divider)
Parameters:
id - int identifying the specific instance of the SerialConnection-class. While having only a single instance, id is irrelevant. However, having more than one open connection (using more than one instance of SerialConnection), id helps identifying which Serial connection a message or a log entry came from.
contact - Link to the instance of the class implementing org.zu.ardulink.connection.ConnectionContact.
divider - A small int representing the number to be used to distinguish between two consecutive packages. It can take a value between 0 and 255. Note that data is only sent to org.zu.ardulink.connection.ConnectionContact#parseInput(int, int, int[]) once the following divider could be identified.

AbstractSerialConnection

public AbstractSerialConnection(String id,
                                ConnectionContact contact)
Just as #AbstractSerialConnection(int, ConnectionContact, int), but with a default divider of 255.

See Also:
#SerialConnection(int, ConnectionContact, int)

AbstractSerialConnection

public AbstractSerialConnection(ConnectionContact contact)
Just as #AbstractSerialConnection(int, ConnectionContact, int), but with a default divider of 255 and a default id of DEFAULT_CONNECTION_NAME. This constructor may mainly be used if only one Serial connection is needed at any time.

See Also:
#SerialConnection(int, ConnectionContact, int)

AbstractSerialConnection

public AbstractSerialConnection()
Just as (@link AbstractSerialConnection(ConnectionContact), but with null contact. The first method to use should be setConnectionContact(ConnectionContact)


AbstractSerialConnection

public AbstractSerialConnection(String id,
                                int divider)
Just as (@link #AbstractSerialConnection(int, ConnectionContact, int), but with null contact. The first method to use should be setConnectionContact(ConnectionContact)


AbstractSerialConnection

public AbstractSerialConnection(String id)
Just as (@link #AbstractSerialConnection(int, ConnectionContact, int), but with null contact and default divider. The first method to use should be setConnectionContact(ConnectionContact)

Method Detail

getPortList

public abstract List<String> getPortList()
This method is used to get a list of all the available Serial ports (note: only Serial ports are considered). Any one of the elements contained in the returned List can be used as a parameter in #connect(String) or #connect(String, int) to open a Serial connection.

Specified by:
getPortList in interface Connection
Returns:
A List containing Strings showing all available Serial ports.

startReader

public void startReader()

stopReader

public void stopReader()

writeSerial

public boolean writeSerial(String message)
This method is included as a legacy. Depending on the other side of the Serial port, it might be easier to send using a String. Note: this method does not add the divider to the end. If a connection is open, a String can be sent over the Serial port using this function. If no connection is available, false is returned and a message is sent using org.zu.ardulink.connection.ConnectionContact#writeLog(int, String).

Specified by:
writeSerial in interface Connection
Parameters:
message - The String to be sent over the Serial connection.
Returns:
true if the message could be sent, false otherwise.

writeSerial

public boolean writeSerial(int numBytes,
                           int[] message)
If a connection is open, an int between 0 and 255 (except the divider) can be sent over the Serial port using this function. The message will be finished by sending the divider. If no connection is available, false is returned and a message is sent using org.zu.ardulink.connection.ConnectionContact#writeLog(int, String).

Specified by:
writeSerial in interface Connection
Parameters:
numBytes - The number of bytes to send over the Serial port.
message - [] The array ofints to be sent over the Serial connection (between 0 and 256).
Returns:
true if the message could be sent, false otherwise or if one of the numbers is equal to the #divider .

connect

public abstract boolean connect(Object... params)
Specified by:
connect in interface Connection

disconnect

public abstract boolean disconnect()
Specified by:
disconnect in interface Connection

setConnectionContact

public void setConnectionContact(ConnectionContact connectionContact)
Specified by:
setConnectionContact in interface Connection

isConnected

public boolean isConnected()
Specified by:
isConnected in interface Connection
Returns:
Whether this instance of AbstractSerialConnection has currently an open connection of not.

getContact

public ConnectionContact getContact()

getId

public String getId()

writeLog

public void writeLog(String text)


Copyright © 2015. All rights reserved.