Administrator
2023-02-13 1b7bf3002bf005a6bf62ad1613d08c689db995c3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.fgkj.bres;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Date;
 
import com.fgkj.data.Com;
import com.fgkj.data.ComBase;
import com.fgkj.data.ComFn;
 
public class RESData {
    public static final int MONOMER_NUM_MAX                     =   300;
    
    public static final int BYTE_LEN = 1812;
    public static final int DATABYTE_LEN = 1808;
    
    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 int battSum;                                        //固定值300
    private float[] MonomerVol = new float[MONOMER_NUM_MAX];    //单体电压,分辨率0.001V
    private float[] MonomerTemp = new float[MONOMER_NUM_MAX];    //温度,偏移100,分辨率0.1℃
    private float[]  MonomerRes = new float[MONOMER_NUM_MAX];    //内阻,分辨率0.001mΩ
    private int CRC16;
    
    public boolean setData(byte[] databuf) {
        ByteBuffer bf = ByteBuffer.allocate(BYTE_LEN);
        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 < DATABYTE_LEN) {
            //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;
        }        
        
        bf.position(0);
        for(int i=0;i<dataType.length;i++) {
            dataType[i] = ComBase.changeByteToInt(bf.get());                         //固定值0xAA 0xAA 0xAA 0xAA
        }
        long 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)*/
 
        //System.out.println(Com.getDateTimeFormat(data_Time, Com.DTF_YMDhms));        
        
        battSum = ComBase.changeShortToInt(bf.getShort());                            //固定值300
        for(int i=0;i<MonomerVol.length;i++) {            
            MonomerVol[i] = (float)(ComBase.changeShortToFloat(bf.getShort())*0.001);        //单体电压,分辨率0.001V
        }
        for(int i=0;i<MonomerTemp.length;i++) {
            MonomerTemp[i] = (float)(ComBase.changeShortToFloat(bf.getShort())*0.1 - 10);    //温度,偏移100,分辨率0.1℃
        }
        for(int i=0;i<MonomerRes.length;i++) {
            MonomerRes[i]  = (float)(ComBase.changeShortToFloat(bf.getShort())*0.001);        //内阻,分辨率0.001mΩ
        }    
        return true;
    }
    
    public static int getMonomerNumMax() {
        return MONOMER_NUM_MAX;
    }
    public int[] getDataType() {
        return dataType;
    }
    public Date getData_Time() {
        return data_Time;
    }
    public int getBattSum() {
        return battSum;
    }
    public float[] getMonomerVol() {
        return MonomerVol;
    }
    public float[] getMonomerTemp() {
        return MonomerTemp;
    }
    public float[] getMonomerRes() {
        return MonomerRes;
    }
    public int getCRC16() {
        return CRC16;
    }
    public void setDataType(int[] dataType) {
        this.dataType = dataType;
    }
    public void setData_Time(Date data_Time) {
        this.data_Time = data_Time;
    }
    public void setBattSum(int battSum) {
        this.battSum = battSum;
    }
    public void setMonomerVol(float[] monomerVol) {
        MonomerVol = monomerVol;
    }
    public void setMonomerTemp(float[] monomerTemp) {
        MonomerTemp = monomerTemp;
    }
    public void setMonomerRes(float[] monomerRes) {
        MonomerRes = monomerRes;
    }
    public void setCRC16(int cRC16) {
        CRC16 = cRC16;
    }
 
    @Override
    public String toString() {
        return "RESData [data_Time=" + data_Time + ", battSum=" + battSum + ", MonomerVol="
                + Arrays.toString(MonomerVol) + ", MonomerTemp=" + Arrays.toString(MonomerTemp) + ", MonomerRes="
                + Arrays.toString(MonomerRes) + "]";
    }
    
    
}