Class ModbusRegisterData

java.lang.Object
net.solarnetwork.node.io.modbus.ModbusRegisterData

public class ModbusRegisterData extends Object
Data for a Modbus register set.
Since:
4.2
Version:
1.0
Author:
matt
  • Constructor Details

    • ModbusRegisterData

      public ModbusRegisterData()
      Constructor.
    • ModbusRegisterData

      public ModbusRegisterData(BitSet coils, BitSet discretes, ModbusData holdings, ModbusData inputs)
      Constructor.

      If any argument is null a new instance will be created and used for that value.

      Parameters:
      coils - the coil register data
      discretes - the discrete register data
      holdings - the holding register data
      inputs - the input register data
    • ModbusRegisterData

      public ModbusRegisterData(ModbusRegisterData other)
      Copy constructor.

      This method provides a thread-safe way to get a copy of the current data.

      Parameters:
      other - the object to copy
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • copy

      public ModbusRegisterData copy()
      Create a copy of this object.

      This method provides a thread-safe way to get a copy of the current data.

      Returns:
      the new instance
      See Also:
    • isEmpty

      public boolean isEmpty()
      Test if the registers are all empty.
      Returns:
      true if no registers or bits have been set
    • hasRegisterData

      public boolean hasRegisterData()
      Test if there is holding or input register data.
      Returns:
      true if either holding or input registers have been set
    • getDataTimestamp

      public Instant getDataTimestamp(ModbusRegisterBlockType blockType)
      Get the data timestamp (last update time) for a given data block.
      Parameters:
      blockType - the block type
      Returns:
      the update timestamp, or null if never updated
    • expire

      public final void expire(ModbusRegisterBlockType blockType)
      Force a data timestamp to be expired.

      Calling this method will reset the data timestamp to zero, effectively expiring the data.

      Parameters:
      blockType - the type of register block to expire
    • isOlderThan

      public final boolean isOlderThan(ModbusRegisterBlockType blockType, long date)
      Test if a block of data was updated before a specific date.
      Parameters:
      blockType - the type of register block to compare the data timestamp against
      date - the date to compare
      Returns:
      true if the block's data timestamp is older than date
    • getWordOrder

      public ModbusWordOrder getWordOrder()
      Get the word order.
      Returns:
      the word order
    • setWordOrder

      public void setWordOrder(ModbusWordOrder order)
      Set the word order.
      Parameters:
      order - the word order
    • writeBit

      public void writeBit(ModbusRegisterBlockType blockType, int address, boolean value)
      Write a bit to a coil or discrete block type.
      Parameters:
      blockType - the block type, must be either Coil or Discrete
      address - the address to write to
      value - the bit value
      Throws:
      IllegalArgumentException - if blockType is not valid
    • writeBits

      public void writeBits(ModbusRegisterBlockType blockType, int address, int count, BitSet set)
      Write a set of bits to a coil or discrete block type.
      Parameters:
      blockType - the block type, must be either Coil or Discrete
      address - the address to write to
      count - the number of bits to update
      set - the bit values
      Throws:
      IllegalArgumentException - if blockType is not valid
    • writeValue

      public void writeValue(ModbusRegisterBlockType blockType, ModbusDataType dataType, int address, int count, Object value)
      Write a non-bit register value.
      Parameters:
      blockType - the block type, must be either Holding or Input
      dataType - the desired data type to write
      address - the address to write to
      count - the number of registers to write to, for variable-length data types
      value - the value to write
      Throws:
      IllegalArgumentException - if blockType is not valid
    • writeRegisters

      public void writeRegisters(ModbusRegisterBlockType blockType, int address, short[] dataValue)
      Write to a set of registers.
      Parameters:
      blockType - the block type to update
      address - the starting register address to write to
      dataValue - the register values to write
    • readCoils

      public BitSet readCoils(int address, int count)
      Read a set of coil values.

      The returned set starts at index 0, meaning the indexes are shifted down by address positions.

      Parameters:
      address - the starting register address
      count - the number of registers to read
      Returns:
      the result set
    • writeCoil

      public void writeCoil(int address, boolean value)
      Write a single coil register value.
      Parameters:
      address - the register address
      value - the value
    • writeCoils

      public void writeCoils(int address, int count, BitSet set)
      Write a set of coil register values.
      Parameters:
      address - the register address to start at
      count - the count of registers to read
      set - the set of coil values to write, starting from index 0
    • writeDiscrete

      public void writeDiscrete(int address, boolean value)
      Write a single discrete register value.
      Parameters:
      address - the register address
      value - the value
    • writeDiscretes

      public void writeDiscretes(int address, int count, BitSet set)
      Write a set of discrete register values.
      Parameters:
      address - the register address to start at
      count - the count of registers to read
      set - the set of coil values to write, starting from index 0
    • readDiscretes

      public BitSet readDiscretes(int address, int count)
      Read a set of discrete values.

      The returned set starts at index 0, meaning the indexes are shifted down by address positions.

      Parameters:
      address - the starting register address
      count - the number of registers to read
      Returns:
      the result set
    • readHoldings

      public byte[] readHoldings(int address, int count)
      Read holding register data into a byte array.
      Parameters:
      address - the starting address
      count - the number of registers
      Returns:
      the register data, of length count * 2
    • readInputs

      public byte[] readInputs(int address, int count)
      Read holding register data into a byte array.
      Parameters:
      address - the starting address
      count - the number of registers
      Returns:
      the register data, of length count * 2
    • readRegisters

      public <T> T readRegisters(ModbusRegisterBlockType blockType, Function<ModbusData,T> action)
      Perform a thread-safe read action on a register block.
      Type Parameters:
      T - the result type
      Parameters:
      blockType - the block type, must be Holding or Input
      action - the action to perform
      Returns:
      the action result
    • readBits

      public <T> T readBits(ModbusRegisterBlockType blockType, Function<BitSet,T> action)
      Perform a thread-safe read action on a register block.
      Type Parameters:
      T - the result type
      Parameters:
      blockType - the block type, must be Coil or Discrete
      action - the action to perform
      Returns:
      the action result
    • writeHolding

      public void writeHolding(int address, short value)
      Write a holding register value.
      Parameters:
      address - the register address to save
      value - the value to save
    • writeHoldings

      public void writeHoldings(int address, short[] values)
      Write holding register values.
      Parameters:
      address - the starting register address to save
      values - the values to save
    • writeInput

      public void writeInput(int address, short value)
      Write an input register value.
      Parameters:
      address - the register address to save
      value - the value to save
    • writeInputs

      public void writeInputs(int address, short[] values)
      Write input register values.
      Parameters:
      address - the starting register address to save
      values - the values to save
    • performRegisterUpdates

      public final ModbusData performRegisterUpdates(ModbusRegisterBlockType blockType, ModbusData.ModbusDataUpdateAction action) throws IOException
      Perform a set of updates to saved register data.
      Parameters:
      blockType - the register block type to perform updates on; must be Holding or Input
      action - the callback to perform the updates on
      Returns:
      this updated data
      Throws:
      IOException - if any communication error occurs
    • performBitUpdates

      public final void performBitUpdates(ModbusRegisterBlockType blockType, ModbusRegisterData.ModbusBitsUpdateAction action) throws IOException
      Perform a set of updates to saved register data.
      Parameters:
      blockType - the register block type to perform updates on; must be Coil or Discrete
      action - the callback to perform the updates on; return true to update the associated data timestamp
      Throws:
      IOException - if any communication error occurs
    • getCoils

      public BitSet getCoils()
      Get the coil register data.
      Returns:
      the coil registers, never null
    • getDiscretes

      public BitSet getDiscretes()
      Get the discrete register data.
      Returns:
      the discrete registers, never null
    • getInputs

      public ModbusData getInputs()
      Get the input register data.
      Returns:
      the input registers, never null
    • getHoldings

      public ModbusData getHoldings()
      Get the holding register data.
      Returns:
      the holding registers, never null