Crea sito


org.zu.ardulink.connection.serial
Class SerialConnection

java.lang.Object
  extended by org.zu.ardulink.connection.serial.AbstractSerialConnection
      extended by org.zu.ardulink.connection.serial.SerialConnection
All Implemented Interfaces:
Connection

public class SerialConnection
extends AbstractSerialConnection
implements Connection

Ardulink - return to homepage

v0.6.1 Magnum PI

Used to simplify communication over a Serial port. Using the RXTX-library (rxtx.qbang.org), one connection per instance of this class can be handled. In addition to handling a connection, information about the available Serial ports can be received using this class. 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 AbstractSerialConnection.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.

Author:
Raphael Blatter ([email protected]), heavily using code examples from the RXTX-website (rxtx.qbang.org)

Luciano Zu Ardulink has added Connection implementation interface to allow multiple connections.

Luciano Zu Ardulink has heavily refactored this class (its original name was gnu.io.net.Network)




Field Summary
 
Fields inherited from class org.zu.ardulink.connection.serial.AbstractSerialConnection
DEFAULT_CONNECTION_NAME
 
Constructor Summary
SerialConnection()
          See super constructor
SerialConnection(ConnectionContact contact)
          Just as #SerialConnection(int, ConnectionContact, int), but with a default AbstractSerialConnection.divider of 255 and a default AbstractSerialConnection.id of 0.
SerialConnection(String id)
          See super constructor
SerialConnection(String id, ConnectionContact contact)
          Just as #SerialConnection(int, ConnectionContact, int), but with a default AbstractSerialConnection.divider of 255.
SerialConnection(String id, ConnectionContact contact, int divider)
           
SerialConnection(String id, int divider)
          See super constructor
 
Method Summary
 boolean connect(Object... params)
           
 boolean connect(String portName)
          Just as connect(String, int), but using 115200 bps as a default speed of the connection.
 boolean connect(String portName, int speed)
          Opening a connection to the specified Serial port, using the specified speed.
 boolean disconnect()
          Simple function closing the connection held by this instance of SerialConnection.
 List<String> getPortList()
          This method is used to get a list of all the available Serial ports (note: only Serial ports are considered).
 
Methods inherited from class org.zu.ardulink.connection.serial.AbstractSerialConnection
getContact, getId, isConnected, setConnectionContact, startReader, stopReader, writeLog, writeSerial, writeSerial
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.zu.ardulink.connection.Connection
isConnected, setConnectionContact, writeSerial, writeSerial
 

Constructor Detail

SerialConnection

public SerialConnection(String id,
                        ConnectionContact contact,
                        int divider)
Parameters:
id - int identifying the specific instance of the SerialConnection-class. While having only a single instance, AbstractSerialConnection.id is irrelevant. However, having more than one open connection (using more than one instance of SerialConnection), AbstractSerialConnection.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 AbstractSerialConnection.divider could be identified.

SerialConnection

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

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

SerialConnection

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

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

SerialConnection

public SerialConnection()
See super constructor


SerialConnection

public SerialConnection(String id,
                        int divider)
See super constructor


SerialConnection

public SerialConnection(String id)
See super constructor

Method Detail

getPortList

public 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
Specified by:
getPortList in class AbstractSerialConnection
Returns:
A List containing Strings showing all available Serial ports.

connect

public boolean connect(String portName)
Just as connect(String, int), but using 115200 bps as a default speed of the connection.

Parameters:
portName - The name of the port the connection should be opened to (see getPortList()).
Returns:
true if the connection has been opened successfully, false otherwise.
See Also:
connect(String, int)

connect

public boolean connect(String portName,
                       int speed)
Opening a connection to the specified Serial port, using the specified speed. After opening the port, messages can be sent using AbstractSerialConnection.writeSerial(String) and received data will be packed into packets (see AbstractSerialConnection.divider) and forwarded using org.zu.ardulink.connection.ConnectionContact#parseInput(int, int, int[]).

Parameters:
portName - The name of the port the connection should be opened to (see getPortList()).
speed - The desired speed of the connection in bps.
Returns:
true if the connection has been opened successfully, false otherwise.

disconnect

public boolean disconnect()
Simple function closing the connection held by this instance of SerialConnection. It also ends the Thread AbstractSerialConnection.reader.

Specified by:
disconnect in interface Connection
Specified by:
disconnect in class AbstractSerialConnection
Returns:
true if the connection could be closed, false otherwise.

connect

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


Copyright © 2015. All rights reserved.