| | |
| | | package com.whyc.res; |
| | | |
| | | import com.whyc.util.ComBase; |
| | | import com.whyc.util.ComFn; |
| | | |
| | | import java.nio.ByteBuffer; |
| | | import java.nio.ByteOrder; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | |
| | |
| | | |
| | | public static final int BYTE_LEN = 1812; |
| | | public static final int DATABYTE_LEN = 1808; |
| | | |
| | | |
| | | public static final int DATA_CHECK_BYTE_LEN = 6; |
| | | |
| | | private int[] dataType = new int[4]; //固定值0xAA 0xAA 0xAA 0xAA |
| | | |
| | | private Date data_Time; //记录时间/*Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC)*/ |
| | | private long dateTime; //时间毫秒数 |
| | | |
| | | private int battSum; //固定值300 |
| | | private float[] MonomerVol = new float[MONOMER_NUM_MAX]; //单体电压,分辨率0.001V |
| | |
| | | private int CRC16; |
| | | |
| | | public boolean setData(byte[] databuf) { |
| | | ByteBuffer bf = ByteBuffer.allocate(BYTE_LEN); |
| | | int Data_Len = 3 * battSum * 2 + 12; |
| | | |
| | | ByteBuffer bf = ByteBuffer.allocate(Data_Len); |
| | | bf.order(ByteOrder.LITTLE_ENDIAN); |
| | | for(int i=0;i<dataType.length;i++) { |
| | | bf.put((byte)0xAA); |
| | | } |
| | | //System.out.println(databuf.length); |
| | | bf.putInt((int)dateTime); |
| | | bf.putShort((short) battSum); |
| | | |
| | | bf.put(databuf); |
| | | bf.flip(); |
| | | bf.position(0); |
| | | if(databuf.length < DATABYTE_LEN) { |
| | | if(databuf.length < (3 * battSum * 2 + 2)) { |
| | | //System.out.println("长度错误"); |
| | | return false; |
| | | } |
| | | //System.err.println(ComFn.bytesToHexString(databuf, databuf.length)); |
| | | int crc0 = bf.getShort(BYTE_LEN-2) & 0xFFFF; |
| | | bf.position(BYTE_LEN-2); |
| | | bf.putShort(ComBase.changeIntToShort(0)); |
| | | int crc1 = RES_Crc16.CalCRC16(bf, bf.limit()); |
| | | if(crc0 != crc1) { |
| | | //System.out.println(crc0+"==="+crc1); |
| | | return false; |
| | | } |
| | | // System.err.println(ComFn.bytesToHexString(bf.array(), bf.array().length)); |
| | | // int crc0 = bf.getShort(Data_Len-2) & 0xFFFF; |
| | | // bf.position(Data_Len-2); |
| | | // bf.putShort(ComBase.changeIntToShort(0)); |
| | | // int crc1 = RES_Crc16.CalCRC16(bf, bf.limit()); |
| | | // if(crc0 != crc1) { |
| | | // System.err.println(crc0+"==="+crc1); |
| | | // return false; |
| | | // } |
| | | |
| | | bf.position(0); |
| | | for(int i=0;i<dataType.length;i++) { |
| | |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | public boolean readResMonCount(byte[] databuf) { |
| | | ByteBuffer bf = ByteBuffer.allocate(10); //读取时间以及单体数量,只能先读取单体数量才能确定后面读取结构体长度 |
| | | bf.order(ByteOrder.LITTLE_ENDIAN); |
| | | for(int i=0;i<dataType.length;i++) { |
| | | bf.put((byte)0xAA); |
| | | } |
| | | //System.out.println(databuf.length); |
| | | bf.put(databuf); |
| | | bf.position(0); |
| | | if(databuf.length < 6) { |
| | | //System.out.println("长度错误"); |
| | | return false; |
| | | } |
| | | |
| | | bf.position(0); |
| | | for(int i=0;i<dataType.length;i++) { |
| | | dataType[i] = ComBase.changeByteToInt(bf.get()); //固定值0xAA 0xAA 0xAA 0xAA |
| | | } |
| | | dateTime = bf.getInt(); |
| | | //System.out.println(dateTime); |
| | | //System.out.println(new Date().getTime()); |
| | | data_Time = new Date(dateTime*1000); //记录时间/*Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC)*/ |
| | | |
| | | SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); |
| | | //System.out.println("记录时间" + sdf.format(data_Time)); |
| | | |
| | | battSum = ComBase.changeShortToInt(bf.getShort()); //固定值300 电操-500 |
| | | //System.out.println("battSum:"+battSum); |
| | | return true; |
| | | } |
| | | |
| | | public static int getMonomerNumMax() { |
| | | return MONOMER_NUM_MAX; |