📄 modbusutil.java
字号:
//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 net.wimpi.modbus.util;import net.wimpi.modbus.Modbus;import net.wimpi.modbus.io.BytesOutputStream;import net.wimpi.modbus.msg.ModbusMessage;import java.io.IOException;/** * Helper class that provides utility methods. * * @author Dieter Wimberger * @author John Charlton * * @version 1.2rc1 (09/11/2004) */public final class ModbusUtil { private static BytesOutputStream m_ByteOut = new BytesOutputStream(Modbus.MAX_MESSAGE_LENGTH); /** * Converts a <tt>ModbusMessage</tt> instance into * a hex encoded string representation. * * @param msg the message to be converted. * @return the converted hex encoded string representation of the message. */ public static final String toHex(ModbusMessage msg) { String ret = "-1"; try { synchronized (m_ByteOut) { msg.writeTo(m_ByteOut); ret = toHex(m_ByteOut.getBuffer(), 0, m_ByteOut.size()); m_ByteOut.reset(); } } catch (IOException ex) { } return ret; }//toHex /** * Returns the given byte[] as hex encoded string. * * @param data a byte[] array. * @return a hex encoded String. */ public static final String toHex(byte[] data) { return toHex(data, 0, data.length); }//toHex /** * Returns a <tt>String</tt> containing unsigned hexadecimal * numbers as digits. * The <tt>String</tt> will coontain two hex digit characters * for each byte from the passed in <tt>byte[]</tt>.<br> * The bytes will be separated by a space character. * <p/> * * @param data the array of bytes to be converted into a hex-string. * @param off the offset to start converting from. * @param length the number of bytes to be converted. * * @return the generated hexadecimal representation as <code>String</code>. */ 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 /** * Returns a <tt>byte[]</tt> containing the given * byte as unsigned hexadecimal number digits. * <p/> * * @param i the int to be converted into a hex string. * @return the generated hexadecimal representation as <code>byte[]</code>. */ 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 /** * Converts the register (a 16 bit value) into an unsigned short. * The value returned is: * <p><pre><code>(((a & 0xff) << 8) | (b & 0xff)) * </code></pre> * <p/> * This conversion has been taken from the documentation of * the <tt>DataInput</tt> interface. * * @param bytes a register as <tt>byte[2]</tt>. * @return the unsigned short value as <tt>int</tt>. * @see java.io.DataInput */ 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: * <p/> * <pre><code> * (byte)(0xff & (v >> 8)) * (byte)(0xff & v) * </code></pre> * <p/> * This conversion has been taken from the documentation of * the <tt>DataOutput</tt> interface. * * @param v * @return the register as <tt>byte[2]</tt>. * @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 <tt>short</tt>. * The value returned is: * <p/> * <pre><code> * (short)((a << 8) | (b & 0xff)) * </code></pre> * <p/> * This conversion has been taken from the documentation of * the <tt>DataInput</tt> interface. * * @param bytes bytes a register as <tt>byte[2]</tt>. * @return the signed short as <tt>short</tt>. */ 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 <tt>short</tt>. * The value returned is: * <p/> * <pre><code> * (short)((a << 8) | (b & 0xff)) * </code></pre> * <p/> * This conversion has been taken from the documentation of * the <tt>DataInput</tt> interface. * * @param bytes a <tt>byte[]</tt> containing a short value. * @param idx an offset into the given byte[]. * @return the signed short as <tt>short</tt>. */ public static final short registerToShort(byte[] bytes, int idx) { return (short) ((bytes[idx] << 8) | (bytes[idx + 1] & 0xff)); }//registerToShort /** * Converts the given <tt>short</tt> into a register * (2 bytes). * The byte values in the register, in the order * shown, are: * <p/> * <pre><code> * (byte)(0xff & (v >> 8)) * (byte)(0xff & v) * </code></pre> * * @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.<br> * The value returned is: * <p><pre> * <code> * (((a & 0xff) << 24) | ((b & 0xff) << 16) | *  ((c & 0xff) << 8) | (d & 0xff)) * </code></pre> * * @param bytes registers as <tt>byte[4]</tt>. * @return the integer contained in the given register bytes. */ 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -