Class ModbusDataUtils

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

public final class ModbusDataUtils extends Object
Utilities for converting to/from Modbus 16-bit register values.

All Modbus register values are stored using short values, which in Java are always treated as 16-bit signed integers.

Since:
2.6
Version:
2.3
Author:
matt
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static BitSet
    bitSetForShortArray(short[] words)
    Convert an array of 16-bit register values into a bit set, using ModbusWordOrder.MostToLeastSignificant word order.
    static BitSet
    bitSetForShortArray(short[] words, ModbusWordOrder wordOrder)
    Convert an array of 16-bit register values into a bit set.
    static short[]
    encodeBytes(byte[] data)
    Encode an array of bytes into 16-bit raw Modbus register values.
    static short[]
    encodeBytes(byte[] data, ModbusWordOrder wordOrder)
    Encode an array of bytes into 16-bit raw Modbus register values.
    static short
    encodeFloat16(net.solarnetwork.util.Half value)
    Encode an IEEE-754 16-bit float value into a raw Modbus unsigned short register value.
    static short[]
    Encode an IEEE-754 32-bit float value into raw Modbus unsigned short register values.
    static short[]
    encodeFloat32(Float value, ModbusWordOrder wordOrder)
    Encode an IEEE-754 32-bit float value into raw Modbus unsigned short register values.
    static short[]
    Encode an IEEE-754 32-bit float value into raw Modbus unsigned short register values.
    static short[]
    Encode an IEEE-754 32-bit float value into raw Modbus unsigned short register values.
    static short[]
    Encode a 16-bit signed integer value into raw Modbus unsigned short register values.
    static short[]
    Encode a 32-bit signed integer value into raw Modbus unsigned short register values.
    static short[]
    encodeInt32(Integer value, ModbusWordOrder wordOrder)
    Encode a 32-bit signed integer value into raw Modbus unsigned short register values.
    static short[]
    Encode a 64-bit signed integer value into raw Modbus unsigned short register values.
    static short[]
    encodeInt64(Long value, ModbusWordOrder wordOrder)
    Encode a 64-bit signed integer value into raw Modbus unsigned short register values.
    static short[]
    encodeNumber(ModbusDataType dataType, Number number)
    Encode a number into raw Modbus register values.
    static short[]
    encodeNumber(ModbusDataType dataType, Number number, ModbusWordOrder wordOrder)
    Encode a number into raw Modbus register values.
    static short[]
    Encode a 16-bit unsigned integer value into raw Modbus unsigned short register values.
    static short[]
    Encode a 32-bit unsigned integer value into raw Modbus unsigned short register values.
    static short[]
    Encode a 32-bit unsigned integer value into raw Modbus unsigned short register values.
    static short[]
    Encode an 64-bit unsigned integer value into raw Modbus unsigned short register values.
    static short[]
    Encode an 64-bit unsigned integer value into raw Modbus unsigned short register values.
    static short[]
    Encode an unsigned integer value into raw Modbus unsigned short register values.
    static short[]
    Encode an unsigned integer value into raw Modbus unsigned short register values.
    static Integer[]
    integerArray(short[] array)
    Convert an array of shorts to Integer objects.
    static byte[]
    parseBytes(short[] words, int offset)
    Parse any number of Modbus register values as a series of bytes.
    static byte[]
    parseBytes(short[] words, int offset, ModbusWordOrder wordOrder)
    Parse any number of Modbus register values as a series of bytes.
    static net.solarnetwork.util.Half
    parseFloat16(short val)
    Parse an IEEE-754 16-bit float value from raw Modbus register values.
    static Float
    parseFloat32(short hi, short lo)
    Parse an IEEE-754 32-bit float value from raw Modbus register values.
    static Double
    parseFloat64(short h1, short h2, short l1, short l2)
    Parse an IEEE-754 64-bit floating point value from raw Modbus register values.
    static Short
    parseInt16(short lo)
    Parse a 16-bit signed integer value from a raw Modbus register value.
    static Integer
    parseInt32(short hi, short lo)
    Parse a 32-bit signed integer value from raw Modbus register values.
    static Long
    parseInt64(short h1, short h2, short l1, short l2)
    Parse a 64-bit signed integer value from raw Modbus register values.
    static Number
    parseNumber(ModbusDataType dataType, short[] words, int offset)
    Encode a number into raw Modbus register values.
    static Number
    parseNumber(ModbusDataType dataType, short[] words, int offset, ModbusWordOrder wordOrder)
    Encode a number into raw Modbus register values.
    static Integer
    Parse a 16-bit unsigned integer value from a raw Modbus register value.
    static Long
    parseUnsignedInt32(short hi, short lo)
    Parse a 32-bit unsigned integer value from raw Modbus register values.
    static BigInteger
    parseUnsignedInt64(short h1, short h2, short l1, short l2)
    Construct an 64-bit unsigned integer from raw Modbus register values.
    static BigInteger
    parseUnsignedInteger(short[] words, int offset)
    Parse any number of Modbus register values as a series of bytes into a BigInteger.
    static BigInteger
    parseUnsignedInteger(short[] words, int offset, ModbusWordOrder wordOrder)
    Parse any number of Modbus register values as a series of bytes into a BigInteger.
    static short[]
    shortArray(int[] array)
    Convert an array of ints to shorts.
    static short[]
    shortArrayForBitSet(BitSet bits, int count)
    Convert a bit set into an array of 16-bit register values, using ModbusWordOrder.MostToLeastSignificant word order.
    static short[]
    shortArrayForBitSet(BitSet bits, int count, ModbusWordOrder wordOrder)
    Convert a bit set into an array of 16-bit register values.
    static void
    swapWordOrder(short[] array)
    Swap the order of an array of register values.
    static float
    toFloat32(short hi, short lo)
    Parse an IEEE-754 32-bit float value from raw Modbus register values.
    static double
    toFloat64(short h1, short h2, short l1, short l2)
    Parse an IEEE-754 64-bit floating point value from raw Modbus register values.
    static short
    toInt16(short lo)
    Convert a 16-bit signed integer value from a raw Modbus register value.
    static int
    toInt32(short hi, short lo)
    Parse a 32-bit signed integer value from raw Modbus register values.
    static long
    toInt64(short h1, short h2, short l1, short l2)
    Parse a 64-bit signed integer value from raw Modbus register values.
    static int
    toUnsignedInt16(short lo)
    Convert a 16-bit unsigned integer value from a raw Modbus register value.
    static long
    toUnsignedInt32(short hi, short lo)
    Parse a 32-bit unsigned integer value from raw Modbus register values.
    static int[]
    unsignedIntArray(short[] array)
    Convert an array of shorts to unsigned short int values.
    static int
    Return the minimum number of 16-bit register words that can accommodate the active bits in a bit set.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ModbusDataUtils

      public ModbusDataUtils()
  • Method Details

    • integerArray

      public static Integer[] integerArray(short[] array)
      Convert an array of shorts to Integer objects.
      Parameters:
      array - the array to convert
      Returns:
      the converted array, or null if array is null
    • unsignedIntArray

      public static int[] unsignedIntArray(short[] array)
      Convert an array of shorts to unsigned short int values.
      Parameters:
      array - the array to convert
      Returns:
      the converted array, or null if array is null
    • shortArray

      public static short[] shortArray(int[] array)
      Convert an array of ints to shorts.
      Parameters:
      array - the array to convert
      Returns:
      the converted array, or null if array is null
    • encodeNumber

      public static short[] encodeNumber(ModbusDataType dataType, Number number)
      Encode a number into raw Modbus register values.
      Parameters:
      dataType - the desired Modbus data type
      number - the number to encode
      Returns:
      the encoded register values, in ModbusWordOrder.MostToLeastSignificant word order
      Throws:
      IllegalArgumentException - if dataType is not supported
      See Also:
    • encodeNumber

      public static short[] encodeNumber(ModbusDataType dataType, Number number, ModbusWordOrder wordOrder)
      Encode a number into raw Modbus register values.

      This method always returns a value if number is null. If dataType is not a supported type an, IllegalArgumentException will be thrown.

      Parameters:
      dataType - the desired Modbus data type
      number - the number to encode
      wordOrder - the desired word order
      Returns:
      the encoded register values
      Throws:
      IllegalArgumentException - if dataType is not supported
      Since:
      1.1
    • encodeInt16

      public static short[] encodeInt16(Short value)
      Encode a 16-bit signed integer value into raw Modbus unsigned short register values.
      Parameters:
      value - the value to encode
      Returns:
      the register values, which will have a length of 1
    • encodeUnsignedInt16

      public static short[] encodeUnsignedInt16(Integer value)
      Encode a 16-bit unsigned integer value into raw Modbus unsigned short register values.
      Parameters:
      value - the value to encode
      Returns:
      the register values, which will have a length of 1
    • encodeInt32

      public static short[] encodeInt32(Integer value)
      Encode a 32-bit signed integer value into raw Modbus unsigned short register values.
      Parameters:
      value - the value to encode
      Returns:
      the register values, which will have a length of 2 and use ModbusWordOrder.MostToLeastSignificant word order
    • encodeInt32

      public static short[] encodeInt32(Integer value, ModbusWordOrder wordOrder)
      Encode a 32-bit signed integer value into raw Modbus unsigned short register values.
      Parameters:
      value - the value to encode
      wordOrder - the resulting word order
      Returns:
      the register values, which will have a length of 2
      Since:
      1.1
    • encodeUnsignedInt32

      public static short[] encodeUnsignedInt32(Long value)
      Encode a 32-bit unsigned integer value into raw Modbus unsigned short register values.
      Parameters:
      value - the value to encode
      Returns:
      the register values, which will have a length of 2 and use ModbusWordOrder.MostToLeastSignificant word order
    • encodeUnsignedInt32

      public static short[] encodeUnsignedInt32(Long value, ModbusWordOrder wordOrder)
      Encode a 32-bit unsigned integer value into raw Modbus unsigned short register values.
      Parameters:
      value - the value to encode
      wordOrder - the resulting word order
      Returns:
      the register values, which will have a length of 2
      Since:
      1.1
    • encodeInt64

      public static short[] encodeInt64(Long value)
      Encode a 64-bit signed integer value into raw Modbus unsigned short register values.
      Parameters:
      value - the value to encode
      Returns:
      the register values, which will have a length of 4 and use ModbusWordOrder.MostToLeastSignificant word order
    • encodeInt64

      public static short[] encodeInt64(Long value, ModbusWordOrder wordOrder)
      Encode a 64-bit signed integer value into raw Modbus unsigned short register values.
      Parameters:
      value - the value to encode
      wordOrder - the resulting word order
      Returns:
      the register values, which will have a length of 4
      Since:
      1.1
    • encodeUnsignedInt64

      public static short[] encodeUnsignedInt64(BigInteger value)
      Encode an 64-bit unsigned integer value into raw Modbus unsigned short register values.
      Parameters:
      value - the integer to encode
      Returns:
      the register values, which will have a length of 4 and use ModbusWordOrder.MostToLeastSignificant word order
    • encodeUnsignedInt64

      public static short[] encodeUnsignedInt64(BigInteger value, ModbusWordOrder wordOrder)
      Encode an 64-bit unsigned integer value into raw Modbus unsigned short register values.
      Parameters:
      value - the integer to encode
      wordOrder - the resulting word order
      Returns:
      the register values, which will have a length of 4
      Since:
      1.1
    • encodeUnsignedInteger

      public static short[] encodeUnsignedInteger(BigInteger value)
      Encode an unsigned integer value into raw Modbus unsigned short register values.
      Parameters:
      value - the integer to encode
      Returns:
      the register values, which will have a length equal to the number of registers required to store the full value and use ModbusWordOrder.MostToLeastSignificant word order
    • encodeUnsignedInteger

      public static short[] encodeUnsignedInteger(BigInteger value, ModbusWordOrder wordOrder)
      Encode an unsigned integer value into raw Modbus unsigned short register values.
      Parameters:
      value - the integer to encode
      wordOrder - the resulting word order
      Returns:
      the register values, which will have a length equal to the number of registers required to store the full value
      Since:
      1.1
    • swapWordOrder

      public static void swapWordOrder(short[] array)
      Swap the order of an array of register values.

      This essentially reverses the array. The array is modified in-place.

      Parameters:
      array - the data to swap
      Since:
      1.1
    • encodeFloat16

      public static short encodeFloat16(net.solarnetwork.util.Half value)
      Encode an IEEE-754 16-bit float value into a raw Modbus unsigned short register value.
      Parameters:
      value - the half to encode
      Returns:
      the register value
      Since:
      2.2
    • encodeFloat32

      public static short[] encodeFloat32(Float value)
      Encode an IEEE-754 32-bit float value into raw Modbus unsigned short register values.
      Parameters:
      value - the float to encode
      Returns:
      the register values, which will have a length of 2 and use ModbusWordOrder.MostToLeastSignificant word order
    • encodeFloat32

      public static short[] encodeFloat32(Float value, ModbusWordOrder wordOrder)
      Encode an IEEE-754 32-bit float value into raw Modbus unsigned short register values.
      Parameters:
      value - the float to encode
      wordOrder - the resulting word order
      Returns:
      the register values, which will have a length of 2
      Since:
      1.1
    • encodeFloat64

      public static short[] encodeFloat64(Double value)
      Encode an IEEE-754 32-bit float value into raw Modbus unsigned short register values.
      Parameters:
      value - the float to encode
      Returns:
      the register values, which will have a length of 4 and use ModbusWordOrder.MostToLeastSignificant word order
    • encodeFloat64

      public static short[] encodeFloat64(Double value, ModbusWordOrder wordOrder)
      Encode an IEEE-754 32-bit float value into raw Modbus unsigned short register values.
      Parameters:
      value - the float to encode
      wordOrder - the resulting word order
      Returns:
      the register values, which will have a length of 4 and use ModbusWordOrder.MostToLeastSignificant word order
      Since:
      1.1
    • encodeBytes

      public static short[] encodeBytes(byte[] data)
      Encode an array of bytes into 16-bit raw Modbus register values.

      Each register value will hold up to two bytes.

      Parameters:
      data - the data to encode
      Returns:
      the register values, which will have a length of data.length / 2 and use ModbusWordOrder.MostToLeastSignificant word order
    • encodeBytes

      public static short[] encodeBytes(byte[] data, ModbusWordOrder wordOrder)
      Encode an array of bytes into 16-bit raw Modbus register values.

      Each register value will hold up to two bytes.

      Parameters:
      data - the data to encode
      wordOrder - the resulting word order
      Returns:
      the register values, which will have a length of data.length / 2
      Since:
      1.1
    • parseNumber

      public static Number parseNumber(ModbusDataType dataType, short[] words, int offset)
      Encode a number into raw Modbus register values.
      Parameters:
      dataType - the desired Modbus data type
      words - an array of Modbus register values using ModbusWordOrder.MostToLeastSignificant word order
      offset - an offset within words to start reading from
      Returns:
      the parsed number, or null if words is null or not long enough for the requested data type
      Throws:
      IllegalArgumentException - if dataType is not supported
    • parseNumber

      public static Number parseNumber(ModbusDataType dataType, short[] words, int offset, ModbusWordOrder wordOrder)
      Encode a number into raw Modbus register values.
      Parameters:
      dataType - the desired Modbus data type
      words - an array of Modbus register values
      offset - an offset within words to start reading from
      wordOrder - the word order of words
      Returns:
      the parsed number, or null if words is null or not long enough for the requested data type
      Throws:
      IllegalArgumentException - if dataType is not supported
      Since:
      1.1
    • toInt16

      public static short toInt16(short lo)
      Convert a 16-bit signed integer value from a raw Modbus register value.

      This method simply returns the given argument, but provided for completeness.

      Parameters:
      lo - bits 15-0
      Returns:
      the parsed integer
      Since:
      2.1
    • parseInt16

      public static Short parseInt16(short lo)
      Parse a 16-bit signed integer value from a raw Modbus register value.
      Parameters:
      lo - bits 15-0
      Returns:
      the parsed integer, never null
    • toUnsignedInt16

      public static int toUnsignedInt16(short lo)
      Convert a 16-bit unsigned integer value from a raw Modbus register value.
      Parameters:
      lo - bits 15-0
      Returns:
      the parsed integer
      Since:
      2.1
    • parseUnsignedInt16

      public static Integer parseUnsignedInt16(short lo)
      Parse a 16-bit unsigned integer value from a raw Modbus register value.
      Parameters:
      lo - bits 15-0
      Returns:
      the parsed integer, never null
    • toInt32

      public static int toInt32(short hi, short lo)
      Parse a 32-bit signed integer value from raw Modbus register values.
      Parameters:
      hi - bits 31-16
      lo - bits 15-0
      Returns:
      the parsed integer
      Since:
      2.1
    • parseInt32

      public static Integer parseInt32(short hi, short lo)
      Parse a 32-bit signed integer value from raw Modbus register values.
      Parameters:
      hi - bits 31-16
      lo - bits 15-0
      Returns:
      the parsed integer, never null
    • toUnsignedInt32

      public static long toUnsignedInt32(short hi, short lo)
      Parse a 32-bit unsigned integer value from raw Modbus register values.

      Note a long is returned to support unsigned 32-bit values.

      Parameters:
      hi - bits 31-16
      lo - bits 15-0
      Returns:
      the parsed integer
      Since:
      2.1
    • parseUnsignedInt32

      public static Long parseUnsignedInt32(short hi, short lo)
      Parse a 32-bit unsigned integer value from raw Modbus register values.

      Note a Long is returned to support unsigned 32-bit values.

      Parameters:
      hi - bits 31-16
      lo - bits 15-0
      Returns:
      the parsed integer, never null
    • toInt64

      public static long toInt64(short h1, short h2, short l1, short l2)
      Parse a 64-bit signed integer value from raw Modbus register values.
      Parameters:
      h1 - bits 63-48
      h2 - bits 47-32
      l1 - bits 31-16
      l2 - bits 15-0
      Returns:
      the parsed integer
      Since:
      2.1
    • parseInt64

      public static Long parseInt64(short h1, short h2, short l1, short l2)
      Parse a 64-bit signed integer value from raw Modbus register values.
      Parameters:
      h1 - bits 63-48
      h2 - bits 47-32
      l1 - bits 31-16
      l2 - bits 15-0
      Returns:
      the parsed integer, never null
    • parseUnsignedInt64

      public static BigInteger parseUnsignedInt64(short h1, short h2, short l1, short l2)
      Construct an 64-bit unsigned integer from raw Modbus register values.
      Parameters:
      h1 - bits 63-48
      h2 - bits 47-32
      l1 - bits 31-16
      l2 - bits 15-0
      Returns:
      the parsed integer, never null
    • parseFloat16

      public static net.solarnetwork.util.Half parseFloat16(short val)
      Parse an IEEE-754 16-bit float value from raw Modbus register values.
      Parameters:
      val - the 16 bits
      Returns:
      the parsed half, or null if not available or parsed half is NaN
      Since:
      2.2
    • toFloat32

      public static float toFloat32(short hi, short lo)
      Parse an IEEE-754 32-bit float value from raw Modbus register values.
      Parameters:
      hi - the high 16 bits
      lo - the low 16 bits
      Returns:
      the parsed float
      Since:
      2.1
    • parseFloat32

      public static Float parseFloat32(short hi, short lo)
      Parse an IEEE-754 32-bit float value from raw Modbus register values.
      Parameters:
      hi - the high 16 bits
      lo - the low 16 bits
      Returns:
      the parsed float, or null if not available or parsed float is NaN
    • toFloat64

      public static double toFloat64(short h1, short h2, short l1, short l2)
      Parse an IEEE-754 64-bit floating point value from raw Modbus register values.
      Parameters:
      h1 - bits 63-48
      h2 - bits 47-32
      l1 - bits 31-16
      l2 - bits 15-0
      Returns:
      the parsed double
      Since:
      2.1
    • parseFloat64

      public static Double parseFloat64(short h1, short h2, short l1, short l2)
      Parse an IEEE-754 64-bit floating point value from raw Modbus register values.
      Parameters:
      h1 - bits 63-48
      h2 - bits 47-32
      l1 - bits 31-16
      l2 - bits 15-0
      Returns:
      the parsed double, or null if the result is NaN
    • parseBytes

      public static byte[] parseBytes(short[] words, int offset)
      Parse any number of Modbus register values as a series of bytes.
      Parameters:
      words - the words to read as bytes, in ModbusWordOrder.MostToLeastSignificant word order
      offset - the word offset to start from
      Returns:
      the parsed bytes
    • parseBytes

      public static byte[] parseBytes(short[] words, int offset, ModbusWordOrder wordOrder)
      Parse any number of Modbus register values as a series of bytes.
      Parameters:
      words - the words to read as bytes
      offset - the word offset to start from
      wordOrder - the word order of words
      Returns:
      the parsed bytes
      Since:
      1.1
    • parseUnsignedInteger

      public static BigInteger parseUnsignedInteger(short[] words, int offset)
      Parse any number of Modbus register values as a series of bytes into a BigInteger.
      Parameters:
      words - the words to parse, in ModbusWordOrder.MostToLeastSignificant word order
      offset - the offset within words to start reading from
      Returns:
      the integer value
    • parseUnsignedInteger

      public static BigInteger parseUnsignedInteger(short[] words, int offset, ModbusWordOrder wordOrder)
      Parse any number of Modbus register values as a series of bytes into a BigInteger.
      Parameters:
      words - the words to parse
      offset - the offset within words to start reading from
      wordOrder - the word order of words
      Returns:
      the integer value
      Since:
      1.1
    • wordSize

      public static int wordSize(BitSet bits)
      Return the minimum number of 16-bit register words that can accommodate the active bits in a bit set.

      If no bits are set, the returned value is 0.

      Parameters:
      bits - the bit set
      Returns:
      the minimum number of words
      Since:
      2.3
    • shortArrayForBitSet

      public static short[] shortArrayForBitSet(BitSet bits, int count, ModbusWordOrder wordOrder)
      Convert a bit set into an array of 16-bit register values.
      Parameters:
      bits - the bit set
      count - the number of words to fill
      wordOrder - the word order for the result
      Returns:
      the words, never null
      Since:
      2.3
    • shortArrayForBitSet

      public static short[] shortArrayForBitSet(BitSet bits, int count)
      Convert a bit set into an array of 16-bit register values, using ModbusWordOrder.MostToLeastSignificant word order.
      Parameters:
      bits - the bit set
      count - the number of words to fill
      Returns:
      the words, never null
      Since:
      2.3
    • bitSetForShortArray

      public static BitSet bitSetForShortArray(short[] words, ModbusWordOrder wordOrder)
      Convert an array of 16-bit register values into a bit set.
      Parameters:
      words - the words
      wordOrder - the word order for the result
      Returns:
      the bit set, never null
      Since:
      2.3
    • bitSetForShortArray

      public static BitSet bitSetForShortArray(short[] words)
      Convert an array of 16-bit register values into a bit set, using ModbusWordOrder.MostToLeastSignificant word order.
      Parameters:
      words - the words
      Returns:
      the bit set, never null
      Since:
      2.3