public class ModbusUtil extends Object
| Modifier and Type | Method and Description |
|---|---|
static int[] |
calculateCRC(byte[] data,
int offset,
int len) |
static byte[] |
doubleToRegisters(double d)
Converts a double value to a byte[8].
|
static byte[] |
floatToRegisters(float f)
Converts a float value to a byte[4] binary float value.
|
static byte |
hiByte(int wd) |
static byte[] |
intToRegisters(int v)
Converts an int value to a byte[4] array.
|
static boolean |
isBlank(List<Object> list)
Return true if the list is null or empty
|
static boolean |
isBlank(Object[] list)
Return true if the array is null or empty
|
static boolean |
isBlank(String value)
Return true if the string is null or empty
|
static byte[] |
longToRegisters(long v)
Converts a long value to a byte[8].
|
static byte |
lowByte(int wd)
Returns the low byte of an integer word.
|
static int |
makeWord(int hibyte,
int lowbyte)
Makes a word from 2 bytes
|
static double |
registersToDouble(byte[] bytes)
Converts a byte[8] binary double value into a double primitive.
|
static float |
registersToFloat(byte[] bytes)
Converts a byte[4] binary float value to a float primitive.
|
static int |
registersToInt(byte[] bytes)
Converts a byte[4] binary int value to a primitive int.
The value returned is: |
static long |
registersToLong(byte[] bytes)
Converts a byte[8] binary long value into a long
primitive.
|
static short |
registerToShort(byte[] bytes)
Converts the given register (16-bit value) into
a short.
|
static short |
registerToShort(byte[] bytes,
int idx)
Converts the register (16-bit value) at the given index
into a short.
|
static int |
registerToUnsignedShort(byte[] bytes)
Converts the register (a 16 bit value) into an unsigned short.
|
static byte[] |
shortToRegister(short s)
Converts the given short into a register
(2 bytes).
|
static void |
sleep(long time)
Sleeps safely for the specified amount of time unless awoken by an interruption
|
static String |
toHex(byte[] data)
Returns the given byte[] as hex encoded string.
|
static String |
toHex(byte[] data,
int off,
int end)
Returns a String containing unsigned hexadecimal
numbers as digits.
|
static byte[] |
toHex(int i)
Returns a byte[] containing the given
byte as unsigned hexadecimal number digits.
|
static String |
toHex(ModbusMessage msg)
Converts a ModbusMessage instance into
a hex encoded string representation.
|
static int |
unsignedByteToInt(byte b)
Converts an unsigned byte to an integer.
|
static byte[] |
unsignedShortToRegister(int v)
Converts the given unsigned short into a register
(2 bytes).
|
public static String toHex(ModbusMessage msg)
msg - the message to be converted.public static String toHex(byte[] data)
data - a byte[] array.public static String toHex(byte[] data, int off, int end)
data - the array of bytes to be converted into a hex-string.off - the offset to start converting from.end - the offset of the end of the byte array.String.public static byte[] toHex(int i)
i - the int to be converted into a hex string.byte[].public static int registerToUnsignedShort(byte[] bytes)
(((a & 0xff) << 8) | (b & 0xff))
This conversion has been taken from the documentation of
the DataInput interface.bytes - a register as byte[2].DataInputpublic static byte[] unsignedShortToRegister(int v)
(byte)(0xff & (v >> 8))
(byte)(0xff & v)
This conversion has been taken from the documentation of
the DataOutput interface.v - Value to convertDataOutputpublic static short registerToShort(byte[] bytes)
(short)((a << 8) | (b & 0xff))
This conversion has been taken from the documentation of
the DataInput interface.bytes - bytes a register as byte[2].public static short registerToShort(byte[] bytes,
int idx)
(short)((a << 8) | (b & 0xff))
This conversion has been taken from the documentation of
the DataInput interface.bytes - a byte[] containing a short value.idx - an offset into the given byte[].public static byte[] shortToRegister(short s)
(byte)(0xff & (v >> 8))
(byte)(0xff & v)
s - Value to convertpublic static int registersToInt(byte[] bytes)
(((a & 0xff) << 24) | ((b & 0xff) << 16) |
((c & 0xff) << 8) | (d & 0xff))
bytes - registers as byte[4].public static byte[] intToRegisters(int v)
v - the value to be converted.public static long registersToLong(byte[] bytes)
bytes - a byte[8] containing a long value.public static byte[] longToRegisters(long v)
v - the value to be converted.public static float registersToFloat(byte[] bytes)
bytes - the byte[4] containing the float value.public static byte[] floatToRegisters(float f)
f - the float to be converted.public static double registersToDouble(byte[] bytes)
bytes - a byte[8] to be converted.public static byte[] doubleToRegisters(double d)
d - the double to be converted.public static int unsignedByteToInt(byte b)
b - the byte to be converted.public static byte lowByte(int wd)
wd - word to get low byte frompublic static byte hiByte(int wd)
wd - word to get high byte frompublic static int makeWord(int hibyte,
int lowbyte)
hibyte - High bytelowbyte - Low bytepublic static int[] calculateCRC(byte[] data,
int offset,
int len)
public static boolean isBlank(String value)
value - String to checkpublic static boolean isBlank(List<Object> list)
list - List to checkpublic static boolean isBlank(Object[] list)
list - Array to checkpublic static void sleep(long time)
time - Time in millisecondsCopyright © 2025. All Rights Reserved.