net.wimpi.modbus.io
Class ModbusTCPTransaction

java.lang.Object
  extended by net.wimpi.modbus.io.ModbusTCPTransaction
All Implemented Interfaces:
ModbusTransaction

public class ModbusTCPTransaction
extends Object
implements ModbusTransaction

Class implementing the ModbusTransaction interface.

Version:
1.2 (@date@)
Author:
Dieter Wimberger

Field Summary
private static AtomicCounter c_TransactionID
           
private  TCPMasterConnection m_Connection
           
private  ModbusTransport m_IO
           
private  boolean m_Reconnecting
           
private  ModbusRequest m_Request
           
private  ModbusResponse m_Response
           
private  int m_Retries
           
private  Mutex m_TransactionLock
           
private  boolean m_ValidityCheck
           
 
Constructor Summary
ModbusTCPTransaction()
          Constructs a new ModbusTCPTransaction instance.
ModbusTCPTransaction(ModbusRequest request)
          Constructs a new ModbusTCPTransaction instance with a given ModbusRequest to be send when the transaction is executed.
ModbusTCPTransaction(TCPMasterConnection con)
          Constructs a new ModbusTCPTransaction instance with a given TCPMasterConnection to be used for transactions.
 
Method Summary
private  void assertExecutable()
          Asserts if this ModbusTCPTransaction is executable.
protected  void checkValidity()
          Checks the validity of the transaction, by checking if the values of the response correspond to the values of the request.
 void execute()
          Executes this ModbusTransaction.
 ModbusRequest getRequest()
          Returns the ModbusRequest instance associated with this ModbusTransaction.
 ModbusResponse getResponse()
          Returns the ModbusResponse instance associated with this ModbusTransaction.
 int getRetries()
          Returns the amount of retries for opening the connection for executing the transaction.
 int getTransactionID()
          Returns the actual transaction identifier of this ModbusTransaction.
 boolean isCheckingValidity()
          Tests whether the validity of a transaction will be checked.
 boolean isReconnecting()
          Tests if the connection will be openend and closed for each execution.
 void setCheckingValidity(boolean b)
          Sets the flag that controls whether the validity of a transaction will be checked.
 void setConnection(TCPMasterConnection con)
          Sets the connection on which this ModbusTransaction should be executed.
 void setReconnecting(boolean b)
          Sets the flag that controls whether a connection is openend and closed for each execution or not.
 void setRequest(ModbusRequest req)
          Sets the ModbusRequest for this ModbusTransaction.
 void setRetries(int num)
          Set the amount of retries for opening the connection for executing the transaction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

c_TransactionID

private static AtomicCounter c_TransactionID

m_Connection

private TCPMasterConnection m_Connection

m_IO

private ModbusTransport m_IO

m_Request

private ModbusRequest m_Request

m_Response

private ModbusResponse m_Response

m_ValidityCheck

private boolean m_ValidityCheck

m_Reconnecting

private boolean m_Reconnecting

m_Retries

private int m_Retries

m_TransactionLock

private Mutex m_TransactionLock
Constructor Detail

ModbusTCPTransaction

public ModbusTCPTransaction()
Constructs a new ModbusTCPTransaction instance.


ModbusTCPTransaction

public ModbusTCPTransaction(ModbusRequest request)
Constructs a new ModbusTCPTransaction instance with a given ModbusRequest to be send when the transaction is executed.

Parameters:
request - a ModbusRequest instance.

ModbusTCPTransaction

public ModbusTCPTransaction(TCPMasterConnection con)
Constructs a new ModbusTCPTransaction instance with a given TCPMasterConnection to be used for transactions.

Parameters:
con - a TCPMasterConnection instance.
Method Detail

setConnection

public void setConnection(TCPMasterConnection con)
Sets the connection on which this ModbusTransaction should be executed.

An implementation should be able to handle open and closed connections.

Parameters:
con - a TCPMasterConnection.

setRequest

public void setRequest(ModbusRequest req)
Description copied from interface: ModbusTransaction
Sets the ModbusRequest for this ModbusTransaction.

The related ModbusResponse is acquired from the passed in ModbusRequest instance.

Specified by:
setRequest in interface ModbusTransaction
Parameters:
req - a ModbusRequest.

getRequest

public ModbusRequest getRequest()
Description copied from interface: ModbusTransaction
Returns the ModbusRequest instance associated with this ModbusTransaction.

Specified by:
getRequest in interface ModbusTransaction
Returns:
the associated ModbusRequest instance.

getResponse

public ModbusResponse getResponse()
Description copied from interface: ModbusTransaction
Returns the ModbusResponse instance associated with this ModbusTransaction.

Specified by:
getResponse in interface ModbusTransaction
Returns:
the associated ModbusRequest instance.

getTransactionID

public int getTransactionID()
Description copied from interface: ModbusTransaction
Returns the actual transaction identifier of this ModbusTransaction. The identifier is a 2-byte (short) non negative integer value valid in the range of 0-65535.

Specified by:
getTransactionID in interface ModbusTransaction
Returns:
the actual transaction identifier as int.

setCheckingValidity

public void setCheckingValidity(boolean b)
Description copied from interface: ModbusTransaction
Sets the flag that controls whether the validity of a transaction will be checked.

Specified by:
setCheckingValidity in interface ModbusTransaction
Parameters:
b - true if checking validity, false otherwise.

isCheckingValidity

public boolean isCheckingValidity()
Description copied from interface: ModbusTransaction
Tests whether the validity of a transaction will be checked.

Specified by:
isCheckingValidity in interface ModbusTransaction
Returns:
true if checking validity, false otherwise.

setReconnecting

public void setReconnecting(boolean b)
Sets the flag that controls whether a connection is openend and closed for each execution or not.

Parameters:
b - true if reconnecting, false otherwise.

isReconnecting

public boolean isReconnecting()
Tests if the connection will be openend and closed for each execution.

Returns:
true if reconnecting, false otherwise.

getRetries

public int getRetries()
Description copied from interface: ModbusTransaction
Returns the amount of retries for opening the connection for executing the transaction.

Specified by:
getRetries in interface ModbusTransaction
Returns:
the amount of retries as int.

setRetries

public void setRetries(int num)
Description copied from interface: ModbusTransaction
Set the amount of retries for opening the connection for executing the transaction.

Specified by:
setRetries in interface ModbusTransaction
Parameters:
num - the amount of retries as int.

execute

public void execute()
             throws ModbusIOException,
                    ModbusSlaveException,
                    ModbusException
Description copied from interface: ModbusTransaction
Executes this ModbusTransaction. Locks the ModbusTransport for sending the ModbusRequest and reading the related ModbusResponse. If reconnecting is activated the connection will be opened for the transaction and closed afterwards.

Specified by:
execute in interface ModbusTransaction
Throws:
ModbusException - if an I/O error occurs, or the response is a modbus protocol exception.
ModbusIOException
ModbusSlaveException

assertExecutable

private void assertExecutable()
                       throws ModbusException
Asserts if this ModbusTCPTransaction is executable.

Throws:
ModbusException - if the transaction cannot be asserted as executable.

checkValidity

protected void checkValidity()
                      throws ModbusException
Checks the validity of the transaction, by checking if the values of the response correspond to the values of the request. Use an override to provide some checks, this method will only return.

Throws:
ModbusException - if this transaction has not been valid.


Copyright © 2012. All Rights Reserved.