Package com.metratec.lib.connection
Class FileConnection
- java.lang.Object
-
- com.metratec.lib.connection.ICommConnection
-
- com.metratec.lib.connection.FileConnection
-
public class FileConnection extends ICommConnection
This is an implementation of ICommConnection that relies only on standard JRE file APIs. This is useful on embedded systems instead of Rs232Connection/UsbConnection since it does not require any JNI. Line settings can be set externally on the device node using `stty` before launching the program.
-
-
Field Summary
-
Fields inherited from class com.metratec.lib.connection.ICommConnection
CONNECTION_LOST, DEVICE_IN_USE, ETHERNET_TIMEOUT, ETHERNET_UNKNOWN_HOST, NO_DEVICES_FOUND, NO_LIBRARY_FOUND, NOT_AVAILABLE, NOT_INITIALISED, RECV_TIMEOUT, SERIAL_NO_ACCESS, SERIAL_NOT_INITIALISED, SERIAL_PARAMETER_NOT_SET, SERIAL_PORT_NOT_EXIST, SET_CONFIGURATION, UNHANDLED_ERROR, USB_SET_BAUDRATE, USB_SET_DATA_CHARACTERISTICS, USB_SET_FLOWCONTROL, USB_SET_TIMEOUTS, USER_ERRORCODE_01, USER_ERRORCODE_02, USER_ERRORCODE_03, USER_ERRORCODE_04, USER_ERRORCODE_05, WRONG_PARAMETER
-
-
Constructor Summary
Constructors Constructor Description FileConnection(java.lang.String portName)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
connect()
This method opens a connection.int
dataAvailable()
void
disconnect()
Closes the communication interfaceint
getConnectionTimeout()
java.util.Hashtable<java.lang.String,java.lang.Object>
getInfo()
Gets Device Informationsjava.io.InputStream
getInputStream()
java.io.OutputStream
getOutputStream()
int
getRecvTimeout()
boolean
isConnected()
int
recv()
Receives a single byte.void
send(byte[] senddata)
Sends data to the connected devicevoid
setConnectionTimeout(int timeout)
Sets the waiting time for the connectionvoid
setRecvTimeout(int timeout)
set the receive timeout for read data, if the timeout expires an CommConnectionException is raised with errorcode RECV_TIMEOUT, the connection is still validvoid
setSettings(java.util.Hashtable<java.lang.String,java.lang.String> settings)
Sets connections settings
-
-
-
Method Detail
-
connect
public void connect() throws CommConnectionException
This method opens a connection. Parameters are passed through the constructor- Specified by:
connect
in classICommConnection
- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- ETHERNET_UNKNOWN_HOST
- ETHERNET_TIMEOUT
- WRONG_PARAMETER
- for USB Connection:
- DEVICE_IN_USE
- NO_LIBRARY_FOUND
- NO_DEVICES_FOUND
- USB_SET_BAUDRATE
- USB_SET_DATA_CHARACTERISTICS
- USB_SET_FLOWCONTROL
- USB_SET_TIMEOUTS
- for RS232 Connection:
- NO_LIBRARY_FOUND
- SERIAL_PORT_NOT_EXIST
- SERIAL_PARAMETER_NOT_SET
- DEVICE_IN_USE
- SERIAL_NO_ACCESS
- SERIAL_NO_INITIALISE
- for TCP Connection:
-
disconnect
public void disconnect() throws CommConnectionException
Description copied from class:ICommConnection
Closes the communication interface- Specified by:
disconnect
in classICommConnection
- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- UNHANDLED_ERROR
- for USB Connection:
- UNHANDLED_ERROR
- for TCP Connection:
-
send
public void send(byte[] senddata) throws CommConnectionException
Description copied from class:ICommConnection
Sends data to the connected device- Specified by:
send
in classICommConnection
- Parameters:
senddata
- data/command send to the connected device- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- WRONG_PARAMETER
- UNHANDLED_ERROR
- NOT_INITIALISE
- for USB Connection:
- WRONG_PARAMETER
- UNHANDLED_ERROR
- NOT_INITIALISE
- for RS232 Connection:
- WRONG_PARAMETER
- CONNECTION_LOST
- UNHANDLED_ERROR
- NOT_INITIALISE
- for TCP Connection:
-
recv
public int recv() throws CommConnectionException
Description copied from class:ICommConnection
Receives a single byte.- Specified by:
recv
in classICommConnection
- Returns:
- byte or -1 if no Data available (timeout)
- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- CONNECTION_LOST
- UNHANDLED_ERROR
- NOT_INITIALISE
- for USB Connection:
- CONNECTION_LOST
- UNHANDLED_ERROR
- NOT_INITIALISE
- for RS232 Connection:
- CONNECTION_LOST
- UNHANDLED_ERROR
- NOT_INITIALISE
- for TCP Connection:
-
isConnected
public boolean isConnected()
- Specified by:
isConnected
in classICommConnection
- Returns:
- true if connected else false
-
getInfo
public java.util.Hashtable<java.lang.String,java.lang.Object> getInfo()
Description copied from class:ICommConnection
Gets Device Informations- Specified by:
getInfo
in classICommConnection
- Returns:
- Hashtable with the information
-
setSettings
public void setSettings(java.util.Hashtable<java.lang.String,java.lang.String> settings)
Description copied from class:ICommConnection
Sets connections settings- Specified by:
setSettings
in classICommConnection
- Parameters:
settings
- Hashtable with the special settings
-
getInputStream
public java.io.InputStream getInputStream()
- Specified by:
getInputStream
in classICommConnection
- Returns:
- the
InputStream
of this connection
-
getOutputStream
public java.io.OutputStream getOutputStream()
- Specified by:
getOutputStream
in classICommConnection
- Returns:
- the
OutputStream
of this connection
-
dataAvailable
public int dataAvailable() throws CommConnectionException
- Specified by:
dataAvailable
in classICommConnection
- Returns:
- number of available signs
- Throws:
CommConnectionException
- possible Errorcodes:- for all connections
- CONNECTION_LOST
- NOT_INITIALISE
- for all connections
-
setRecvTimeout
public void setRecvTimeout(int timeout)
Description copied from class:ICommConnection
set the receive timeout for read data, if the timeout expires an CommConnectionException is raised with errorcode RECV_TIMEOUT, the connection is still valid- Specified by:
setRecvTimeout
in classICommConnection
- Parameters:
timeout
- time in milliseconds
-
getRecvTimeout
public int getRecvTimeout()
- Specified by:
getRecvTimeout
in classICommConnection
- Returns:
- the receive timeout
-
setConnectionTimeout
public void setConnectionTimeout(int timeout)
Description copied from class:ICommConnection
Sets the waiting time for the connection- Specified by:
setConnectionTimeout
in classICommConnection
- Parameters:
timeout
- connection timeout
-
getConnectionTimeout
public int getConnectionTimeout()
- Specified by:
getConnectionTimeout
in classICommConnection
- Returns:
- the waiting time for the connection
-
-