Class LockingModbusConnection
- All Implemented Interfaces:
Closeable,AutoCloseable,ModbusConnection
ModbusConnection that wraps another connection with a lock.
The open() method will acquire the lock, and the close()
method will release the lock.
- Since:
- 3.3
- Version:
- 2.1
- Author:
- matt
-
Constructor Summary
ConstructorsConstructorDescriptionLockingModbusConnection(ModbusConnection delegate, ReentrantLock lock, long timeout, TimeUnit timeoutUnit, String description, org.slf4j.Logger log) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the connection, if it is open.intGet the Modbus Unit ID this device represents.voidopen()Open the connection, if it is not already open.byte[]readBytes(ModbusReadFunction function, int address, int count) Get the raw bytes of specific registers.readDiscreetValues(int[] addresses, int count) Get the values of a set of "coil" type registers, as a BitSet.readDiscreetValues(int address, int count) Get the values of a set of "coil" type registers, as a BitSet.readInputDiscreteValues(int address, int count) Get the values of a set of "input discrete" type registers, as a BitSet.readString(ModbusReadFunction function, int address, int count, boolean trim, Charset charset) Read a set of registers as bytes and interpret as a string.short[]readWords(ModbusReadFunction function, int address, int count) Get the values of specific 16-bit Modbus registers as an array of 16-bit words.int[]readWordsUnsigned(ModbusReadFunction function, int address, int count) Get the values of specific 16-bit Modbus registers as an array of unsigned 16-bit words.voidwriteBytes(ModbusWriteFunction function, int address, byte[] values) Write raw byte values to registers.voidwriteDiscreetValues(int[] addresses, BitSet bits) Write values of a set of "coil" type registers, via a BitSet.voidwriteString(ModbusWriteFunction function, int address, String value, Charset charset) Write a string as raw byte values to registers.voidwriteWords(ModbusWriteFunction function, int address, int[] values) Write unsigned 16-bit word values to 16-bit Modbus registers.voidwriteWords(ModbusWriteFunction function, int address, short[] values) Write 16-bit word values to 16-bit Modbus registers.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.solarnetwork.node.io.modbus.ModbusConnection
readDiscreteValues, readDiscreteValues, writeDiscreteValues
-
Constructor Details
-
LockingModbusConnection
public LockingModbusConnection(ModbusConnection delegate, ReentrantLock lock, long timeout, TimeUnit timeoutUnit, String description, org.slf4j.Logger log) Constructor.- Parameters:
delegate- the delegate connectionlock- the lock to usetimeout- the lock timeouttimeoutUnit- the lock timeout unitdescription- a logging descriptionlog- the logger to use- Throws:
IllegalArgumentException- if any argument is null
-
-
Method Details
-
getUnitId
public int getUnitId()Description copied from interface:ModbusConnectionGet the Modbus Unit ID this device represents.- Specified by:
getUnitIdin interfaceModbusConnection- Returns:
- the unit ID
-
open
Description copied from interface:ModbusConnectionOpen the connection, if it is not already open. The connection must be opened before calling any of the other methods in this API.- Specified by:
openin interfaceModbusConnection- Throws:
IOException- if the connection cannot be openednet.solarnetwork.node.service.LockTimeoutException- if a lock is required to open the connection and it could not be obtained within a configured maximum amount of time
-
close
public void close()Description copied from interface:ModbusConnectionClose the connection, if it is open.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceModbusConnection
-
readDiscreetValues
Description copied from interface:ModbusConnectionGet the values of a set of "coil" type registers, as a BitSet.This uses a Modbus function code 1 request.
This method is required but deprecated to preserve backwards compatibility.
- Specified by:
readDiscreetValuesin interfaceModbusConnection- Parameters:
address- the 0-based Modbus register address to readcount- the count of discreet registers to read- Returns:
- BitSet, with indexes set from 0 to a
count - 1 - Throws:
IOException- if any communication error occurs
-
readDiscreetValues
Description copied from interface:ModbusConnectionGet the values of a set of "coil" type registers, as a BitSet.This uses a Modbus function code 1 request. The returned set will have a size equal to
addresses.length * count.This method is required but deprecated to preserve backwards compatibility.
- Specified by:
readDiscreetValuesin interfaceModbusConnection- Parameters:
addresses- the 0-based Modbus register addresses to readcount- the count of coils to read with each address- Returns:
- BitSet, with each
countindexes for each index in theaddressesparameter - Throws:
IOException- if any communication error occurs
-
writeDiscreetValues
Description copied from interface:ModbusConnectionWrite values of a set of "coil" type registers, via a BitSet.This uses a Modbus function code 5 request, once for each address in
addresses. Each address at index i corresponds to the value of bit at index i. Thus bits 0 toaddresses.length - 1are used.This method is required but deprecated to preserve backwards compatibility.
- Specified by:
writeDiscreetValuesin interfaceModbusConnection- Parameters:
addresses- the Modbus register addresses to start writing tobits- the bits to write, each index corresponding to an index inaddresses- Throws:
IOException- if any communication error occurs
-
readInputDiscreteValues
Description copied from interface:ModbusConnectionGet the values of a set of "input discrete" type registers, as a BitSet.This uses a Modbus function code 2 request. The returned bitset will have
countvalues set, from 0 tocount - 1.- Specified by:
readInputDiscreteValuesin interfaceModbusConnection- Parameters:
address- the Modbus register addresses to start reading fromcount- the count of registers to read- Returns:
- BitSet, with each 0 to
countindexes - Throws:
IOException- if any communication error occurs
-
readWords
Description copied from interface:ModbusConnectionGet the values of specific 16-bit Modbus registers as an array of 16-bit words.Note that the raw short values can be treated as unsigned shorts by converting them to integers, like
int unsigned = ((int)s) && 0xFFFF, or by callingShort.toUnsignedInt(short). Thus the values returned by this method are technically the same as those returned byModbusConnection.readWordsUnsigned(ModbusReadFunction, int, int), without having been cast to ints.- Specified by:
readWordsin interfaceModbusConnection- Parameters:
function- the Modbus function code to useaddress- the 0-based Modbus register address to start reading fromcount- the number of Modbus 16-bit registers to read- Returns:
- array of register values; the result will have a length equal to
count - Throws:
IOException- if any communication error occurs
-
readWordsUnsigned
public int[] readWordsUnsigned(ModbusReadFunction function, int address, int count) throws IOException Description copied from interface:ModbusConnectionGet the values of specific 16-bit Modbus registers as an array of unsigned 16-bit words.Note that the raw int values can be treated as signed shorts by casting them to shorts, like
short signed = (short)s. Thus the values returned by this method are technically the same as those returned byModbusConnection.readWords(ModbusReadFunction, int, int), having been cast to ints.- Specified by:
readWordsUnsignedin interfaceModbusConnection- Parameters:
function- the Modbus function code to useaddress- the 0-based Modbus register address to start reading fromcount- the number of 16-bit Modbus registers to read- Returns:
- array of register values; the result will have a length equal to
count - Throws:
IOException- if any communication error occurs
-
writeWords
public void writeWords(ModbusWriteFunction function, int address, short[] values) throws IOException Description copied from interface:ModbusConnectionWrite 16-bit word values to 16-bit Modbus registers.- Specified by:
writeWordsin interfaceModbusConnection- Parameters:
function- the Modbus function code to useaddress- the 0-based Modbus register address to start writing tovalues- the 16-bit values to write- Throws:
IOException- if any communication error occurs
-
writeWords
Description copied from interface:ModbusConnectionWrite unsigned 16-bit word values to 16-bit Modbus registers.All the elements in
valueswill be truncated to 16-bits and then stored in Modbus registers.- Specified by:
writeWordsin interfaceModbusConnection- Parameters:
function- the Modbus function code to useaddress- the 0-based Modbus register address to start writing tovalues- the unsigned 16-bit values to write- Throws:
IOException- if any communication error occurs
-
readBytes
Description copied from interface:ModbusConnectionGet the raw bytes of specific registers.Each 16-bit modbus register value will be decomposed into two output bytes, so that the returned result will have a length equal to
count * 2.- Specified by:
readBytesin interfaceModbusConnection- Parameters:
function- the Modbus function code to useaddress- the 0-based Modbus register address to start reading fromcount- the number of Modbus 16-bit registers to read- Returns:
- register words as an array of bytes
- Throws:
IOException- if any communication error occurs
-
writeBytes
Description copied from interface:ModbusConnectionWrite raw byte values to registers.- Specified by:
writeBytesin interfaceModbusConnection- Parameters:
function- the Modbus function code to useaddress- the 0-based Modbus register address to start writing to;values.length * 216-bit registers will be writtenvalues- the byte values to write- Throws:
IOException- if any communication error occurs
-
readString
public String readString(ModbusReadFunction function, int address, int count, boolean trim, Charset charset) throws IOException Description copied from interface:ModbusConnectionRead a set of registers as bytes and interpret as a string.- Specified by:
readStringin interfaceModbusConnection- Parameters:
function- the Modbus function code to useaddress- the 0-based Modbus register address to start reading fromcount- the number of Modbus 16-bit registers to readtrim- if true then remove leading/trailing whitespace from the resulting stringcharset- the character set to interpret the bytes as- Returns:
- String from interpreting raw bytes as a string
- Throws:
IOException- if any communication error occurs- See Also:
-
writeString
public void writeString(ModbusWriteFunction function, int address, String value, Charset charset) throws IOException Description copied from interface:ModbusConnectionWrite a string as raw byte values to registers.- Specified by:
writeStringin interfaceModbusConnection- Parameters:
function- the Modbus function code to useaddress- the 0-based Modbus register address to start writing tovalue- the string value to writecharset- the character set to interpret the bytes as- Throws:
IOException- if any communication error occurs
-