package com.modbus.util; import java.nio.ByteBuffer; import java.nio.ByteOrder; import com.base.ComBase; public class BaseData { public static final int ReadByteCount = 12; public int transsend; //·¢ËÍÊÂÎñ±êʶ·û public int transreceive; //½ÓÊÕÊÂÎñ±êʶ·û public int protocolident; //ЭÒé±êʶ·û public int datacount; //Êý¾Ý³¤¶È[±¾×Ö½ÚÏÂÒ»¸öµ½×îºó] public int unitident = 0x02; //µ¥Ôª±êʶ·û[µ±Ç°µ¥Ôª¹Ì¶¨Îª2] public int funcode; //¹¦ÄÜÂë public int addrstart; //¼Ä´æÆ÷ÆðʼµØÖ· public int regcount; //¼Ä´æÆ÷ÊýÁ¿ public int bytecount; //Êý¾ÝÇø×Ö½ÚÊýÁ¿ public byte[] data; //Êý¾ÝÇøÊý¾Ý public BaseData() { } public BaseData(int cmd_code,int regstart,int regcount) { this.funcode = cmd_code; this.addrstart = regstart; this.regcount = regcount; } /** * ¹¹Ôì·¢ËÍÊý¾Ý * @param cmd_code ÃüÁîÂë * @param addrstart ¼Ä´æÆ÷ÆðʼµØÖ· * @param regcount ¼Ä´æÆ÷ÊýÁ¿ * @return */ public byte[] createReadData() { ByteBuffer bf = ByteBuffer.allocate(ReadByteCount); bf.order(ByteOrder.BIG_ENDIAN); bf.putShort(ComBase.changeIntToShort(addrstart)); //½ÓÊÕ±êʶ ½«¼Ä´æÆ÷ÆðʼµØÖ··ÅÔÚ±êʶÖÐ[2¸ö×Ö½Ú] bf.putShort(ComBase.changeIntToShort(0x0000)); //ЭÒé±êʶ·û bf.putShort(ComBase.changeIntToShort(0x0006)); //ÏÂÃæ×Ö½Ú³¤¶È bf.put(ComBase.changeIntToByte(unitident)); //µ¥Ôª±êʶ·û(É豸ID) bf.put(ComBase.changeIntToByte(funcode)); //¹¦ÄÜÂë bf.putShort(ComBase.changeIntToShort(addrstart)); //¼Ä´æÆ÷ÆðʼµØÖ· bf.putShort(ComBase.changeIntToShort(regcount)); bf.flip(); byte[] databyte = new byte[ReadByteCount]; bf.get(databyte); return databyte; } /** * ¹¹Ôì¶àµãдÈëÊý¾Ý * @param value * @return */ public byte[] createWiriteData(byte[] data) { ByteBuffer bf = ByteBuffer.allocate(ReadByteCount+regcount*2+1); bf.order(ByteOrder.BIG_ENDIAN); bf.putShort(ComBase.changeIntToShort(addrstart)); //½ÓÊÕ±êʶ ½«¼Ä´æÆ÷ÆðʼµØÖ··ÅÔÚ±êʶÖÐ[2¸ö×Ö½Ú] bf.putShort(ComBase.changeIntToShort(0x0000)); //ЭÒé±êʶ·û bf.putShort(ComBase.changeIntToShort(7+regcount*2)); //ÏÂÃæ×Ö½Ú³¤¶È bf.put(ComBase.changeIntToByte(unitident)); //µ¥Ôª±êʶ·û(É豸ID) bf.put(ComBase.changeIntToByte(funcode)); //¹¦ÄÜÂë bf.putShort(ComBase.changeIntToShort(addrstart)); //¼Ä´æÆ÷ÆðʼµØÖ· bf.putShort(ComBase.changeIntToShort(regcount)); //¼Ä´æÆ÷ÊýÁ¿ bf.put(ComBase.changeIntToByte(regcount*2)); //д×Ö½ÚÊýÁ¿ bf.put(data); //Ä¿±êÖµ bf.flip(); byte[] databyte = new byte[ReadByteCount]; bf.get(databyte); return databyte; } /** * ¹¹Ôì°´Å¥¿ØÖÆÊý¾Ý * @return */ public static byte[] createButtonByte(int value) { ByteBuffer bf = ByteBuffer.allocate(2); bf.order(ByteOrder.BIG_ENDIAN); bf.putShort(ComBase.changeIntToShort(value)); bf.flip(); byte[] b = new byte[2]; bf.get(b); return b; } /** * ¹¹ÔìдÈë¶à¸ö¼Ä´æÆ÷Êý¾Ý * @param value * @return */ public static byte[] createFloatByte(float value) { ByteBuffer bf = ByteBuffer.allocate(4); bf.order(ByteOrder.BIG_ENDIAN); bf.putFloat(value); bf.flip(); byte[] b = new byte[4]; bf.get(b); return b; } /** * * @return */ public boolean putByteBuffer(ByteBuffer bf) { ByteBuffer tmpbuf = bf; if(tmpbuf.limit() < 8) { return false; } tmpbuf.position(0); transsend = ComBase.changeShortToInt(bf.getShort()); protocolident = ComBase.changeShortToInt(bf.getShort()); datacount = ComBase.changeShortToInt(bf.getShort()); unitident = ComBase.changeByteToInt(bf.get()); if(unitident != 0x02) { return false; } funcode = ComBase.changeByteToInt(bf.get()); return true; } }