Class ModbusData
- All Implemented Interfaces:
net.solarnetwork.node.domain.DataAccessor
This class is designed to operate as a cache of data read from a Modbus
device. The data is modeled as a sparse array of register address keys with
associated 16-bit values. It supports thread-safe write access to the saved
data and thread-safe read access if ModbusData(ModbusData) or
copy() are invoked to get a copy of the data.
- Since:
- 2.3
- Version:
- 3.1
- Author:
- matt
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAPI for performing updates to the saved data.static interfaceAPI for performing updates to the data.static classMutable view of Modbus data registers, meant to be used for thread-safe writes. -
Field Summary
Fields inherited from interface net.solarnetwork.node.domain.DataAccessor
INFO_KEY_DEVICE_MANUFACTURE_DATE, INFO_KEY_DEVICE_MANUFACTURER, INFO_KEY_DEVICE_MODEL, INFO_KEY_DEVICE_NAME, INFO_KEY_DEVICE_SERIAL_NUMBER, INFO_KEY_DEVICE_VERSION -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncopy()Create a copy of this object.final StringGet a string of data values, useful for debugging.final net.solarnetwork.util.IntShortMapGet direct access to all modbus registers.final ModbusDataexpire()Force the data timestamp to be expired.final StringgetAsciiString(int addr, int count, boolean trim) Construct an ASCII string out of a data address range.final StringgetAsciiString(ModbusReference ref, boolean trim) Construct an ASCII string from a reference.final StringgetAsciiString(ModbusReference ref, int offset, boolean trim) Construct an ASCII string from a relative reference.final BooleangetBoolean(int addr) Construct a 1-bit boolean from a data register address.byte[]getBytes(int addr, int count) Construct a byte array out of a data address range.final net.solarnetwork.util.HalfgetFloat16(int addr) Construct a 16-bit float from data register addresses.final FloatgetFloat32(int addr) Construct a 32-bit float from a starting data register address.final FloatgetFloat32(int hiAddr, int loAddr) Construct a 32-bit float from data register addresses.final DoublegetFloat64(int addr) Construct a 32-bit float from a starting data register address.final DoublegetFloat64(int h1Addr, int h2Addr, int l1Addr, int l2Addr) Construct a 32-bit float from data register addresses.final ShortgetInt16(int addr) Construct a signed 16-bit integer from a data register address.final IntegergetInt32(int addr) Construct a signed 32-bit integer from data register addresses.final IntegergetInt32(int hiAddr, int loAddr) Construct a signed 32-bit integer from data register addresses.final LonggetInt64(int addr) Construct a signed 64-bit integer from a starting data register address.final LonggetInt64(int h1Addr, int h2Addr, int l1Addr, int l2Addr) Construct a signed 64-bit long value from data register addresses.final StringgetLatin1String(ModbusReference ref, int offset, boolean trim) Construct an ISO-LATIN-1 string from a relative reference.final NumbergetNumber(ModbusReference ref) Get a number value from a reference.final NumbergetNumber(ModbusReference ref, int offset) Get a number value from a relative reference.final StringConstruct a string out of a data address range.final StringConstruct a string out of a data address range.Get a read-only Map view of all modbus registers as unsigned integer values.final IntegergetUnsignedInt16(int addr) Construct an unsigned 16-bit integer from a data register address.final LonggetUnsignedInt32(int addr) Construct an unsigned 32-bit integer from a starting data register address.final LonggetUnsignedInt32(int hiAddr, int loAddr) Construct an unsigned 32-bit integer from data register addresses.final BigIntegergetUnsignedInt64(int addr) Construct an unsigned 64-bit integer from data register addresses.final BigIntegergetUnsignedInt64(int h1Addr, int h2Addr, int l1Addr, int l2Addr) Construct an unsigned 64-bit integer from data register addresses.final StringgetUtf8String(int addr, int count, boolean trim) Construct a UTF-8 string out of a data address range.final StringgetUtf8String(ModbusReference ref, boolean trim) Construct a UTF-8 string from a reference.final StringgetUtf8String(ModbusReference ref, int offset, boolean trim) Construct a UTF-8 string from a reference.getValue(ModbusDataType dataType, int address, int count) Get a value.getValue(ModbusReference ref) Get a value for a reference.final ModbusWordOrderGet the word ordering to use when reading multi-register data types.booleanisEmpty()Test if the register data is empty.final ModbusDataPerform a set of updates to saved register data.final voidrefreshData(ModbusConnection conn, ModbusReadFunction readFunction, Collection<net.solarnetwork.util.IntRange> ranges) Refresh a range of data from the Modbus device into this object.final voidrefreshData(ModbusConnection conn, ModbusReadFunction readFunction, Collection<net.solarnetwork.util.IntRange> ranges, ModbusData.MutableModbusData m) Read data from the device and update a mutable data instance.final voidrefreshData(ModbusConnection conn, ModbusReadFunction readFunction, net.solarnetwork.util.IntRangeSet rangeSet, int maxRangeLength) Refresh a range of data from the Modbus device into this object.final voidrefreshData(ModbusConnection conn, net.solarnetwork.util.IntRangeSet rangeSet) Refresh a range of data from the Modbus device into this object.final voidsetWordOrder(ModbusWordOrder wordOrder) Set the word ordering to use when reading multi-register data types.intsize()Get the number of registers with a set value.final voidslice(short[] dest, int destFrom, int address, int length) Copy the raw modbus data for a set of addresses into an array.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface net.solarnetwork.node.domain.DataAccessor
deviceInfo, deviceInfoBuilder
-
Constructor Details
-
ModbusData
public ModbusData()Default constructor. -
ModbusData
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
-
size
public int size()Get the number of registers with a set value.- Returns:
- the number of set registers
-
isEmpty
public boolean isEmpty()Test if the register data is empty.- Returns:
- true if no registers have been set
-
getDataTimestamp
- Specified by:
getDataTimestampin interfacenet.solarnetwork.node.domain.DataAccessor
-
getDeviceInfo
- Specified by:
getDeviceInfoin interfacenet.solarnetwork.node.domain.DataAccessor
-
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:
-
getNumber
Get a number value from a reference.- Parameters:
ref- the reference to get the number value for- Returns:
- the value, or null if
refis null - Throws:
IllegalArgumentException- if the reference data type is not numeric- Since:
- 1.4
-
getNumber
Get a number value from a relative reference.- Parameters:
ref- the relative reference to get the number value foroffset- the address offset to add toModbusReference.getAddress()- Returns:
- the value, or null if
refis null - Throws:
IllegalArgumentException- if the reference data type is not numeric- Since:
- 1.4
-
getBoolean
Construct a 1-bit boolean from a data register address.- Parameters:
addr- the address- Returns:
- the boolean, never null
-
getUnsignedInt16
Construct an unsigned 16-bit integer from a data register address.- Parameters:
addr- the register address- Returns:
- the integer, never null
-
getInt16
Construct a signed 16-bit integer from a data register address.- Parameters:
addr- the register address- Returns:
- the short, never null
-
getUnsignedInt32
Construct an unsigned 32-bit integer from data register addresses.- Parameters:
hiAddr- the address of the high 16 bitsloAddr- the address of the low 16 bits- Returns:
- the parsed value, or null if not available
-
getInt32
Construct a signed 32-bit integer from data register addresses.- Parameters:
hiAddr- the address of the high 16 bitsloAddr- the address of the low 16 bits- Returns:
- the parsed value, or null if not available
- Since:
- 1.1
-
getInt32
Construct a signed 32-bit integer from data register addresses.This method will respect the configured
getWordOrder()value.- Parameters:
addr- the address of the first register; the second register is assumed to beaddr + 1- Returns:
- the parsed value, or null if not available
- Since:
- 1.1
-
getUnsignedInt32
Construct an unsigned 32-bit integer from a starting data register address.This method will respect the configured
getWordOrder()value.- Parameters:
addr- the address of the first register; the second register is assumed to beaddr + 1- Returns:
- the parsed value, or null if not available
-
getFloat16
public final net.solarnetwork.util.Half getFloat16(int addr) Construct a 16-bit float from data register addresses.- Parameters:
addr- the address of the 16 bits- Returns:
- the parsed value, or null if not available.
- Since:
- 1.4
-
getFloat32
Construct a 32-bit float from data register addresses.- Parameters:
hiAddr- the address of the high 16 bitsloAddr- the address of the low 16 bits- Returns:
- the parsed value, or null if not available.
-
getFloat32
Construct a 32-bit float from a starting data register address.This method will respect the configured
getWordOrder()value.- Parameters:
addr- the address of the first register; the second register is assumed to beaddr + 1- Returns:
- The parsed value, or null if not available.
-
getInt64
Construct a signed 64-bit long value from data register addresses.- Parameters:
h1Addr- the address of bits 63-48h2Addr- the address of bits 47-32l1Addr- the address of bits 31-16l2Addr- the address of bits 15-0- Returns:
- the parsed long
-
getInt64
Construct a signed 64-bit integer from a starting data register address.This method will respect the configured
getWordOrder()value.- Parameters:
addr- the address of the first register; the remaining three registers are assumed to beaddr + 1,addr + 2, andaddr + 3- Returns:
- the parsed value, or null if not available
-
getUnsignedInt64
Construct an unsigned 64-bit integer from data register addresses.- Parameters:
h1Addr- the address of bits 63-48h2Addr- the address of bits 47-32l1Addr- the address of bits 31-16l2Addr- the address of bits 15-0- Returns:
- the parsed value, or null if not available
- Since:
- 1.1
-
getUnsignedInt64
Construct an unsigned 64-bit integer from data register addresses.This method will respect the configured
getWordOrder()value.- Parameters:
addr- the address of the first register; the remaining three registers are assumed to beaddr + 1,addr + 2, andaddr + 3- Returns:
- the parsed value, or null if not available
- Since:
- 1.1
-
getFloat64
Construct a 32-bit float from data register addresses.- Parameters:
h1Addr- the address of bits 63-48h2Addr- the address of bits 47-32l1Addr- the address of bits 31-16l2Addr- the address of bits 15-0- Returns:
- the parsed value, or null if not available
-
getFloat64
Construct a 32-bit float from a starting data register address.This method will respect the configured
getWordOrder()value.- Parameters:
addr- the address of the first register; the remaining three registers are assumed to beaddr + 1,addr + 2, andaddr + 3- Returns:
- The parsed value, or null if not available.
-
getBytes
public byte[] getBytes(int addr, int count) Construct a byte array out of a data address range.This method will assume
ModbusWordOrder.MostToLeastSignificantword ordering.- Parameters:
addr- the starting address of the 16-bit register to readcount- the number of 16-bit registers to read- Returns:
- the byte array, which will have a length of
count * 2
-
getString
Construct a string out of a data address range.This method calls
getBytes(int, int)to interpret the data addresses as a raw byte array, and then interprets the result as a string in the given character set.- Parameters:
addr- the starting address of the 16-bit register to readcount- the number of 16-bit registers to readtrim- if true then remove leading/trailing whitespace from the resulting stringcharsetName- the character set to interpret the bytes as- Returns:
- the parsed string, or null if
countis 0 - Throws:
IllegalCharsetNameException- If the given character set name is illegalIllegalArgumentException- IfcharsetNameis nullUnsupportedCharsetException- If no support for the named character set is available
-
getString
Construct a string out of a data address range.This method calls
getBytes(int, int)to interpret the data addresses as a raw byte array, and then interprets the result as a string in the given character set.- Parameters:
addr- the starting address of the 16-bit register to readcount- the number of 16-bit registers to readtrim- if true then remove leading/trailing whitespace from the resulting string as well as any null bytes, i.e. \0charset- the character set to interpret the bytes as- Returns:
- the parsed string, or null if
countis 0
-
getUtf8String
Construct a UTF-8 string out of a data address range.- Parameters:
addr- the starting address of the 16-bit register to readcount- the number of 16-bit registers to readtrim- if true then remove leading/trailing whitespace from the resulting string- Returns:
- the parsed string
-
getAsciiString
Construct an ASCII string out of a data address range.- Parameters:
addr- the starting address of the 16-bit register to readcount- the number of 16-bit registers to readtrim- if true then remove leading/trailing whitespace from the resulting string- Returns:
- the parsed string
-
getUtf8String
Construct a UTF-8 string from a reference.- Parameters:
ref- the reference to get the string value fortrim- if true then remove leading/trailing whitespace from the resulting string- Returns:
- the parsed string
- Since:
- 1.4
-
getUtf8String
Construct a UTF-8 string from a reference.- Parameters:
ref- the reference to get the string value foroffset- the address offset to add toModbusReference.getAddress()trim- if true then remove leading/trailing whitespace from the resulting string- Returns:
- the parsed string
- Since:
- 1.4
-
getAsciiString
Construct an ASCII string from a reference.- Parameters:
ref- the reference to get the string value fortrim- if true then remove leading/trailing whitespace from the resulting string- Returns:
- the parsed string
- Since:
- 1.4
-
getAsciiString
Construct an ASCII string from a relative reference.- Parameters:
ref- the reference to get the string value foroffset- the address offset to add toModbusReference.getAddress()trim- if true then remove leading/trailing whitespace from the resulting string- Returns:
- the parsed string
- Since:
- 1.4
-
getLatin1String
Construct an ISO-LATIN-1 string from a relative reference.- Parameters:
ref- the reference to get the string value foroffset- the address offset to add toModbusReference.getAddress()trim- if true then remove leading/trailing whitespace from the resulting string- Returns:
- the parsed string
- Since:
- 1.8
-
performUpdates
Perform a set of updates to saved register data.- Parameters:
action- the callback to perform the updates on- Returns:
- this object to allow method chaining
- Throws:
IOException- if any communication error occurs
-
expire
Force the data timestamp to be expired.Calling this method will reset the
dataTimestampto zero, effectively expiring the data.- Returns:
- this object to allow method chaining
- Since:
- 1.2
-
getValue
Get a value for a reference.- Parameters:
ref- the reference to get the number value for- Returns:
- the value, or null if
refis null - Since:
- 3.1
-
getValue
Get a value.- Parameters:
dataType- the data typeaddress- the register addresscount- the word length, for variable-length data types only- Returns:
- the decoded value, or null
- Since:
- 3.1
-
toString
-
dataDebugString
Get a string of data values, useful for debugging.The generated string will contain a register address followed by two register values per line, printed as hexidecimal integers, with a prefix and suffix line. For example:
ModbusData{ 30000: 0x4141, 0x727E 30006: 0xFFC0, 0x0000 ... 30344: 0x0000, 0x0000 }- Returns:
- debug string
-
slice
public final void slice(short[] dest, int destFrom, int address, int length) Copy the raw modbus data for a set of addresses into an array.- Parameters:
dest- the destination arraydestFrom- the starting destination array index to populateaddress- the Modbus address to start fromlength- the number of Modbus registers to copy- Since:
- 2.0
-
getWordOrder
Get the word ordering to use when reading multi-register data types.- Returns:
- the word order
- Since:
- 1.3
-
setWordOrder
Set the word ordering to use when reading multi-register data types.- Parameters:
wordOrder- the word order to use; null will be ignored- Since:
- 1.3
-
refreshData
public final void refreshData(ModbusConnection conn, net.solarnetwork.util.IntRangeSet rangeSet) throws IOException Refresh a range of data from the Modbus device into this object.This method uses the
ModbusReadFunction.ReadHoldingRegisterwith a maximum of 64 addresses read per transaction.- Parameters:
conn- the connectionrangeSet- the Modbus registers to read, where eachIntRangein the set defines inclusive address ranges to read- Throws:
IOException- if any communication error occurs- Since:
- 1.6
-
refreshData
public final void refreshData(ModbusConnection conn, ModbusReadFunction readFunction, net.solarnetwork.util.IntRangeSet rangeSet, int maxRangeLength) throws IOException Refresh a range of data from the Modbus device into this object.- Parameters:
conn- the connectionreadFunction- the Modbus read function to userangeSet- the Modbus registers to read, where eachIntRangein the set defines inclusive address ranges to readmaxRangeLength- the maximum length of any combined range in the resulting set- Throws:
IOException- if any communication error occurs- Since:
- 1.6
-
refreshData
public final void refreshData(ModbusConnection conn, ModbusReadFunction readFunction, Collection<net.solarnetwork.util.IntRange> ranges) throws IOException Refresh a range of data from the Modbus device into this object.- Parameters:
conn- the connectionreadFunction- the Modbus read function to useranges- the Modbus registers to read, where eachIntRangein the set defines inclusive address ranges to read- Throws:
IOException- if any communication error occurs- Since:
- 2.0
-
refreshData
public final void refreshData(ModbusConnection conn, ModbusReadFunction readFunction, Collection<net.solarnetwork.util.IntRange> ranges, ModbusData.MutableModbusData m) throws IOException Read data from the device and update a mutable data instance.- Parameters:
conn- the connectionreadFunction- the read functionranges- the ranges of Modbus addresses to read/updatem- the mutable data to update- Throws:
IOException- if any communication error occurs- Since:
- 2.0
-
getUnsignedDataMap
Get a read-only Map view of all modbus registers as unsigned integer values.- Returns:
- the data map, never null
- Since:
- 1.7
-
dataRegisters
public final net.solarnetwork.util.IntShortMap dataRegisters()Get direct access to all modbus registers.- Returns:
- the data map, never null
- Since:
- 3.1
-