//License /*** * Java Modbus Library (jamod) * Copyright (c) 2002-2004, jamod development team * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the author nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. ***/ package tbDataModel_Dell; /** * Helper class that provides utility methods. * ¸¨ÖúÀà ÌṩmodbusUtil * @author Dieter Wimberger * @author John Charlton * * @version 1.2rc1 (09/11/2004) */ public final class ModbusUtil { public static final String toHex(byte[] data) { return toHex(data, 0, data.length); }//toHex public static final String toHex(byte[] data, int off, int length) { //double size, two bytes (hex range) for one byte StringBuffer buf = new StringBuffer(data.length * 2); for (int i = off; i < length; i++) { //don't forget the second hex digit if (((int) data[i] & 0xff) < 0x10) { buf.append("0"); } buf.append(Long.toString((int) data[i] & 0xff, 16)); if (i < data.length - 1) { buf.append(" "); } } return buf.toString(); }//toHex public static final byte[] toHex(int i) { StringBuffer buf = new StringBuffer(2); //don't forget the second hex digit if (((int) i & 0xff) < 0x10) { buf.append("0"); } buf.append(Long.toString((int) i & 0xff, 16).toUpperCase()); return buf.toString().getBytes(); }//toHex public static final int registerToUnsignedShort(byte[] bytes) { return ((bytes[0] & 0xff) << 8 | (bytes[1] & 0xff)); }//registerToUnsignedShort /** * 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. * * @param v * @return the register as byte[2]. * @see java.io.DataOutput */ public static final byte[] unsignedShortToRegister(int v) { byte[] register = new byte[2]; register[0] = (byte) (0xff & (v >> 8)); register[1] = (byte) (0xff & v); return register; }//unsignedShortToRegister /** * 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. * * @param bytes bytes a register as byte[2]. * @return the signed short as short. */ public static final short registerToShort(byte[] bytes) { return (short) ((bytes[0] << 8) | (bytes[1] & 0xff)); }//registerToShort /** * 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. * * @param bytes a byte[] containing a short value. * @param idx an offset into the given byte[]. * @return the signed short as short. */ public static final short registerToShort(byte[] bytes, int idx) { return (short) ((bytes[idx] << 8) | (bytes[idx + 1] & 0xff)); }//registerToShort /** * 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)
   * 
* * @param s * @return a register containing the given short value. */ public static final byte[] shortToRegister(short s) { byte[] register = new byte[2]; register[0] = (byte) (0xff & (s >> 8)); register[1] = (byte) (0xff & s); return register; }//shortToRegister /** * 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))
   * 
* * @param bytes registers as byte[4]. * @return the integer contained in the given register bytes. */ public static final int registersToInt(byte[] bytes,int idx) { return ( ((bytes[idx] & 0xff) << 24) | ((bytes[idx+1] & 0xff) << 16) | ((bytes[idx+2] & 0xff) << 8) | (bytes[idx+3] & 0xff) ); }//registersToInt public static final int registersToInt(byte[] bytes) { return ( ((bytes[0] & 0xff) << 24) | ((bytes[1] & 0xff) << 16) | ((bytes[2] & 0xff) << 8) | (bytes[3] & 0xff) ); }//registersToInt /** * Converts an int value to a byte[4] array. * * @param v the value to be converted. * @return a byte[4] containing the value. */ public static final byte[] intToRegisters(int v) { byte[] registers = new byte[4]; registers[0] = (byte) (0xff & (v >> 24)); registers[1] = (byte) (0xff & (v >> 16)); registers[2] = (byte) (0xff & (v >> 8)); registers[3] = (byte) (0xff & v); return registers; }//intToRegisters /** * Converts a byte[8] binary long value into a long * primitive. * * @param bytes a byte[8] containing a long value. * @return a long value. */ public static final long registersToLong(byte[] bytes) { return ( (((long) (bytes[0] & 0xff) << 56) | ((long) (bytes[1] & 0xff) << 48) | ((long) (bytes[2] & 0xff) << 40) | ((long) (bytes[3] & 0xff) << 32) | ((long) (bytes[4] & 0xff) << 24) | ((long) (bytes[5] & 0xff) << 16) | ((long) (bytes[6] & 0xff) << 8) | ((long) (bytes[7] & 0xff))) ); }//registersToLong /** * Converts a long value to a byte[8]. * * @param v the value to be converted. * @return a byte[8] containing the long value. */ public static final byte[] longToRegisters(long v) { byte[] registers = new byte[8]; registers[0] = (byte) (0xff & (v >> 56)); registers[1] = (byte) (0xff & (v >> 48)); registers[2] = (byte) (0xff & (v >> 40)); registers[3] = (byte) (0xff & (v >> 32)); registers[4] = (byte) (0xff & (v >> 24)); registers[5] = (byte) (0xff & (v >> 16)); registers[6] = (byte) (0xff & (v >> 8)); registers[7] = (byte) (0xff & v); return registers; }//longToRegisters /** * Converts a byte[4] binary float value to a float primitive. * * @param bytes the byte[4] containing the float value. * @return a float value. */ public static final float registersToFloat(byte[] bytes) { return Float.intBitsToFloat(( ((bytes[0] & 0xff) << 24) | ((bytes[1] & 0xff) << 16) | ((bytes[2] & 0xff) << 8) | (bytes[3] & 0xff) )); }//registersToFloat /** * Converts a float value to a byte[4] binary float value. * * @param f the float to be converted. * @return a byte[4] containing the float value. */ public static final byte[] floatToRegisters(float f) { return intToRegisters(Float.floatToIntBits(f)); }//floatToRegisters /** * Converts a byte[8] binary double value into a double primitive. * * @param bytes a byte[8] to be converted. * @return a double value. */ public static final double registersToDouble(byte[] bytes) { return Double.longBitsToDouble(( (((long) (bytes[0] & 0xff) << 56) | ((long) (bytes[1] & 0xff) << 48) | ((long) (bytes[2] & 0xff) << 40) | ((long) (bytes[3] & 0xff) << 32) | ((long) (bytes[4] & 0xff) << 24) | ((long) (bytes[5] & 0xff) << 16) | ((long) (bytes[6] & 0xff) << 8) | ((long) (bytes[7] & 0xff))) )); }//registersToDouble /** * Converts a double value to a byte[8]. * * @param d the double to be converted. * @return a byte[8]. */ public static final byte[] doubleToRegisters(double d) { return longToRegisters(Double.doubleToLongBits(d)); }//doubleToRegisters /** * Converts an unsigned byte to an integer. * * @param b the byte to be converted. * @return an integer containing the unsigned byte value. */ public static final int unsignedByteToInt(byte b) { return (int) b & 0xFF; }//unsignedByteToInt /** * Returns the broadcast address for the subnet of the host the code * is executed on. * * @return the broadcast address as InetAddress. *

* public static final InetAddress getBroadcastAddress() { * byte[] addr = new byte[4]; * try { * addr = InetAddress.getLocalHost().getAddress(); * addr[3] = -1; * return getAddressFromBytes(addr); * } catch (Exception ex) { * ex.printStackTrace(); * return null; * } * }//getBroadcastAddress */ /* public static final InetAddress getAddressFromBytes(byte[] addr) throws Exception { StringBuffer sbuf = new StringBuffer(); for (int i = 0; i < addr.length; i++) { if (addr[i] < 0) { sbuf.append(256 + addr[i]); } else { sbuf.append(addr[i]); } if (i < (addr.length - 1)) { sbuf.append('.'); } } //DEBUG:System.out.println(sbuf.toString()); return InetAddress.getByName(sbuf.toString()); }//getAddressFromBytes */ //TODO: John description. /** * Returs the low byte of an integer word. * * @param wd * @return */ public static final byte lowByte(int wd) { return (new Integer(0xff & wd).byteValue()); }// lowByte //TODO: John description. /** * * @param wd * @return */ public static final byte hiByte(int wd) { return (new Integer(0xff & (wd >> 8)).byteValue()); }// hiByte //TODO: John description. /** * * @param hibyte * @param lowbyte * @return */ public static final int makeWord(int hibyte, int lowbyte) { int hi = 0xFF & hibyte; int low = 0xFF & lowbyte; return ((hi << 8) | low); }// makeWord public static byte convertStringToHex(String str){ char[] chars = str.toCharArray(); StringBuffer hex = new StringBuffer(); int size=chars.length; for(int i = 0; i