net.wimpi.modbus.util
Class ModbusUtil

java.lang.Object
  extended by net.wimpi.modbus.util.ModbusUtil

public final class ModbusUtil
extends Object

Helper class that provides utility methods.

Version:
@version@ (@date@)
Author:
Dieter Wimberger, John Charlton

Field Summary
private static short[] auchCRCHi
           
private static short[] auchCRCLo
           
private static BytesOutputStream m_ByteOut
           
 
Constructor Summary
ModbusUtil()
           
 
Method Summary
static int[] calculateCRC(byte[] data, int offset, int len)
           
static int calculateLRC(byte[] data, int off, 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 byte[] longToRegisters(long v)
          Converts a long value to a byte[8].
static byte lowByte(int wd)
          Returs the low byte of an integer word.
static int makeWord(int hibyte, int lowbyte)
           
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 String toHex(byte[] data)
          Returns the given byte[] as hex encoded string.
static String toHex(byte[] data, int off, int length)
          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).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_ByteOut

private static BytesOutputStream m_ByteOut

auchCRCHi

private static final short[] auchCRCHi

auchCRCLo

private static final short[] auchCRCLo
Constructor Detail

ModbusUtil

public ModbusUtil()
Method Detail

toHex

public static final String toHex(ModbusMessage msg)
Converts a ModbusMessage instance into a hex encoded string representation.

Parameters:
msg - the message to be converted.
Returns:
the converted hex encoded string representation of the message.

toHex

public static final String toHex(byte[] data)
Returns the given byte[] as hex encoded string.

Parameters:
data - a byte[] array.
Returns:
a hex encoded String.

toHex

public static final String toHex(byte[] data,
                                 int off,
                                 int length)
Returns a String containing unsigned hexadecimal numbers as digits. The String will coontain two hex digit characters for each byte from the passed in byte[].
The bytes will be separated by a space character.

Parameters:
data - the array of bytes to be converted into a hex-string.
off - the offset to start converting from.
length - the number of bytes to be converted.
Returns:
the generated hexadecimal representation as String.

toHex

public static final byte[] toHex(int i)
Returns a byte[] containing the given byte as unsigned hexadecimal number digits.

Parameters:
i - the int to be converted into a hex string.
Returns:
the generated hexadecimal representation as byte[].

registerToUnsignedShort

public static final int registerToUnsignedShort(byte[] bytes)
Converts the register (a 16 bit value) into an unsigned short. The value returned is:

(((a & 0xff) << 8) | (b & 0xff))
 

This conversion has been taken from the documentation of the DataInput interface.

Parameters:
bytes - a register as byte[2].
Returns:
the unsigned short value as int.
See Also:
DataInput

unsignedShortToRegister

public static final byte[] unsignedShortToRegister(int v)
Converts the given unsigned short into a register (2 bytes). The byte values in the register, in the order shown, are:


 (byte)(0xff & (v >> 8))
 (byte)(0xff & v)
 

This conversion has been taken from the documentation of the DataOutput interface.

Parameters:
v -
Returns:
the register as byte[2].
See Also:
DataOutput

registerToShort

public static final short registerToShort(byte[] bytes)
Converts the given register (16-bit value) into a short. The value returned is:


 (short)((a << 8) | (b & 0xff))
 

This conversion has been taken from the documentation of the DataInput interface.

Parameters:
bytes - bytes a register as byte[2].
Returns:
the signed short as short.

registerToShort

public static final short registerToShort(byte[] bytes,
                                          int idx)
Converts the register (16-bit value) at the given index into a short. The value returned is:


 (short)((a << 8) | (b & 0xff))
 

This conversion has been taken from the documentation of the DataInput interface.

Parameters:
bytes - a byte[] containing a short value.
idx - an offset into the given byte[].
Returns:
the signed short as short.

shortToRegister

public static final byte[] shortToRegister(short s)
Converts the given short into a register (2 bytes). The byte values in the register, in the order shown, are:


 (byte)(0xff & (v >> 8))
 (byte)(0xff & v)
 

Parameters:
s -
Returns:
a register containing the given short value.

registersToInt

public static final int registersToInt(byte[] bytes)
Converts a byte[4] binary int value to a primitive int.
The value returned is:

 
 (((a & 0xff) << 24) | ((b & 0xff) << 16) |
  ((c & 0xff) << 8) | (d & 0xff))
 

Parameters:
bytes - registers as byte[4].
Returns:
the integer contained in the given register bytes.

intToRegisters

public static final byte[] intToRegisters(int v)
Converts an int value to a byte[4] array.

Parameters:
v - the value to be converted.
Returns:
a byte[4] containing the value.

registersToLong

public static final long registersToLong(byte[] bytes)
Converts a byte[8] binary long value into a long primitive.

Parameters:
bytes - a byte[8] containing a long value.
Returns:
a long value.

longToRegisters

public static final byte[] longToRegisters(long v)
Converts a long value to a byte[8].

Parameters:
v - the value to be converted.
Returns:
a byte[8] containing the long value.

registersToFloat

public static final float registersToFloat(byte[] bytes)
Converts a byte[4] binary float value to a float primitive.

Parameters:
bytes - the byte[4] containing the float value.
Returns:
a float value.

floatToRegisters

public static final byte[] floatToRegisters(float f)
Converts a float value to a byte[4] binary float value.

Parameters:
f - the float to be converted.
Returns:
a byte[4] containing the float value.

registersToDouble

public static final double registersToDouble(byte[] bytes)
Converts a byte[8] binary double value into a double primitive.

Parameters:
bytes - a byte[8] to be converted.
Returns:
a double value.

doubleToRegisters

public static final byte[] doubleToRegisters(double d)
Converts a double value to a byte[8].

Parameters:
d - the double to be converted.
Returns:
a byte[8].

unsignedByteToInt

public static final int unsignedByteToInt(byte b)
Converts an unsigned byte to an integer.

Parameters:
b - the byte to be converted.
Returns:
an integer containing the unsigned byte value.

lowByte

public static final byte lowByte(int wd)
Returs the low byte of an integer word.

Parameters:
wd -
Returns:
the low byte.

hiByte

public static final byte hiByte(int wd)
Parameters:
wd -
Returns:
the hi byte.

makeWord

public static final int makeWord(int hibyte,
                                 int lowbyte)
Parameters:
hibyte -
lowbyte -
Returns:
a word.

calculateCRC

public static final int[] calculateCRC(byte[] data,
                                       int offset,
                                       int len)

calculateLRC

public static final int calculateLRC(byte[] data,
                                     int off,
                                     int len)


Copyright © 2012. All Rights Reserved.