net.sourceforge.jmodbus
Class ModbusSlave

java.lang.Object
  |
  +--net.sourceforge.jmodbus.Modbus
        |
        +--net.sourceforge.jmodbus.ModbusSlave
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
ModbusASCIISlave, ModbusRTUSlave, ModbusTCPSlave

public class ModbusSlave
extends Modbus
implements java.lang.Runnable

Class to represent a Modbus Slave device. This is a base class that will be extended by classes representing the different transports. This class contains the knowledge that is required to generate a modbus request and parse the response. It also implements Runnable so it can be wrapped up into a Thread to run independantly from other threads that might on any of the modbus registers or coils.


Fields inherited from class net.sourceforge.jmodbus.Modbus
ADDRESS_MAX, COIL_OFF, COIL_ON, debug, EXCEPTION_MODIFIER, ILLEGAL_DATA_ADDRESS, ILLEGAL_DATA_VALUE, ILLEGAL_FUNCTION, ILLEGAL_RESPONSE_LENGTH, MAX_MESSAGE_LENGTH, READ_COILS, READ_EXCEPTION_STATUS, READ_INPUT_DISCRETES, READ_INPUT_REGISTERS, READ_MULTIPLE_REGISTERS, transport, UINT16_MAX, UINT16_MIN, UINT8_MAX, UINT8_MIN, WRITE_COIL, WRITE_MULTIPLE_REGISTERS, WRITE_SINGLE_REGISTER
 
Constructor Summary
ModbusSlave(ModbusTransport transport, int slaveAddress)
          Class constructor.
 
Method Summary
 boolean processRequest()
          Function to process a single request.
 void run()
          The run method for the slave object.
 void setInputCoils(ModbusCoilBank coils)
          Set the reference to the Input Coil Bank.
 void setInputRegisters(ModbusRegisterBank regs)
          Set the reference to the Input Register Bank.
 void setOutputCoils(ModbusCoilBank coils)
          Set the reference to the Output Coil Bank.
 void setOutputRegisters(ModbusRegisterBank regs)
          Set the reference to the Output Register Bank.
 
Methods inherited from class net.sourceforge.jmodbus.Modbus
receiveFrame, sendFrame
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ModbusSlave

public ModbusSlave(ModbusTransport transport,
                   int slaveAddress)
Class constructor. Accepts a ModbusTransport object that is passed to the master.
Parameters:
transport - The ModbusTransport object that should be used by this Modbus object to send and recieve Modbus frames.
slaveAddress - The slave address (or unit indetifier) of this slave device.
Method Detail

setInputRegisters

public void setInputRegisters(ModbusRegisterBank regs)
Set the reference to the Input Register Bank. This also tells the slave device that it is allowed to process function relating to input registers.
Parameters:
regs - The ModbusRegisterBank object that represents the input registers.

setOutputRegisters

public void setOutputRegisters(ModbusRegisterBank regs)
Set the reference to the Output Register Bank. This also tells the slave device that it is allowed to process function relating to output registers.
Parameters:
regs - The ModbusRegisterBank object that represents the output registers.

setInputCoils

public void setInputCoils(ModbusCoilBank coils)
Set the reference to the Input Coil Bank. This also tells the slave device that it is allowed to process function relating to input coils.
Parameters:
regs - The ModbusCoilBank object that represents the input coils.

setOutputCoils

public void setOutputCoils(ModbusCoilBank coils)
Set the reference to the Output Coil Bank. This also tells the slave device that it is allowed to process function relating to output coils.
Parameters:
regs - The ModbusCoilBank object that represents the output coils.

run

public void run()
The run method for the slave object. This will cause the object to continually scan for input requests and generate responses, untill an unrecoverable error occurs, when it will exit. To do this it uses the processRequest function and just loops untill it exits unsucessfully.
Specified by:
run in interface java.lang.Runnable

processRequest

public boolean processRequest()
Function to process a single request. This function will block untill a request is recieved and then process it. If the request can be processed then the function return sucessfully. If the request can not be processed then it will return false. If a modbus exception occurs and an appropriate response is generated, this is considered a sucess.