Administrator
2021-04-29 fb323fb880c57f6572c6290a59d90feb213b93a1
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.dev.fbs9600;
 
import com.base.Com;
import com.base.Crc16;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Date;
 
public class FBS9600_CommData {
    public static final short REGADDR_NULL = 0;
    public static final short ResTestState_REGADDR = 4160;
    public static final short GROUPVOL_REGADDR_START = 4;
    public static final short ONLINEVOL_REGADDR_START = 8197;
    public static final short ONLINEVOL_REGADDR_END = 8200;
    public static final short GROUPCUR_REGADDR_START = 6;
    public static final short BATTSTATE_REGADDR_START = 8206;
    public static final short BATTSTATE_REGADDR_END = 8209;
    public static final short GROUPTMP_REGADDR_START = 8214;
    public static final short GROUPTMP_REGADDR_END = 8217;
    public static final short MONVOL_REGADDR_START = 28;
    public static final short MONVOL_REGADDR_END = 327;
    public static final short MONTMP_REGADDR_START = 632;
    public static final short MONTMP_REGADDR_END = 931;
    public static final short MONRES_REGADDR_START = 1236;
    public static final short MONRES_REGADDR_END = 1535;
    public static final short TEST_MONRES_REGADDR = 4087;
    public static final byte MB_CMD_NULL = 0;
    public static final byte MB_CMD_READ = 3;
    public static final byte MB_CMD_WRITE = 6;
    public static final int CURR_DIR_CHARGE = 0;
    public static final int CURR_DIR_DISCHARGE = 1;
    public static final int BATTSTATE_NULL = 0;
    public static final int BATTSTATE_FLOAT = 1;
    public static final int BATTSTATE_CHARGE = 2;
    public static final int BATTSTATE_DISCHARGE = 3;
    public static final int CMD_GET_BATT_VOL = 1;
    public static final int CMD_GET_BATT_STATE = 2;
    public static final int BATT_GROUP_COUNT_MAX = 4;
    public static final int BATT_MON_COUNT_MAX = 512;
    private boolean m_UsrWriteCmdAck = false;
    
    public static final int     CMD_Test_NULL_Ack                        =    0;            //停止内阻测试成功
    public static final int     CMD_StartResTest                        =    0x51;            //启动内阻测试
    public static final int     CMD_StartResTestAck                        =    0x52;            //启动内阻测试成功
    public static final int     CMD_StopResTest                            =    0x53;            //停止内阻测试
    public static final int     CMD_StopResTestAck                        =    0x54;            //停止内阻测试成功
 
    boolean battres_test_state = false;
 
    float[] groupvol = new float[4];
    float[] groupvols = new float[4];
 
    float[] onlinevol = new float[4];
    float[] battcurr = new float[4];
    float[] battcurrs = new float[4];
    int[] battstate = new int[4];
    int[] battcurrdir = new int[4];
    float[] grouptmp = new float[4];
 
    float[] battvol = new float[512];
    float[] batttmp = new float[512];
    float[] battres = new float[512];
    byte[] data_byte_rx;
    ByteBuffer data_bf;
 
    public FBS9600_CommData() {
        this.data_byte_rx = new byte[256];
        this.data_bf = ByteBuffer.allocate(512);
        this.data_bf.order(ByteOrder.BIG_ENDIAN);
    }
 
    public boolean getUsrWriteCmdAck() {
        return this.m_UsrWriteCmdAck;
    }
 
    public byte[] makeDataToByteArray(boolean tcp_modbus, byte addr, byte cmd, short reg_addr, short reg_count) {
        this.data_bf.clear();
 
        if (tcp_modbus) {
            this.data_bf.putShort((short) -21846);
            this.data_bf.putShort(reg_addr);
            this.data_bf.putShort((short) 8);
            this.data_bf.put((byte) 0);
            this.data_bf.put(cmd);
            this.data_bf.putShort(reg_addr);
        } else {
            this.data_bf.put(addr);
            this.data_bf.put(cmd);
            this.data_bf.putShort(reg_addr);
        }
 
        if (3 == cmd) {
            this.data_bf.putShort(reg_count);
        } else if (6 == cmd) {
            this.m_UsrWriteCmdAck = false;
            this.data_bf.putShort((short) 1);
        }
        int crc = Crc16.CalCRC16(this.data_bf, this.data_bf.position()) & 0xFFFF;
 
        this.data_bf.putShort((short) crc);
 
        byte[] d_byte_tx = new byte[this.data_bf.position()];
 
        this.data_bf.position(0);
        this.data_bf.get(d_byte_tx);
 
        return d_byte_tx;
    }
 
    boolean getDataFromByteArray(boolean tcp_modbus, short reg_addr, short reg_count, short packet_len,
            int reg_count_last) {
        this.data_bf.clear();
        this.data_bf.put(this.data_byte_rx);
        this.data_bf.flip();
 
        this.data_bf.position(0);
        int crc = Crc16.CalCRC16(this.data_bf, packet_len - 2) & 0xFFFF;
 
        this.data_bf.position(packet_len - 2);
        int tmp_crc = this.data_bf.getShort() & 0xFFFF;
 
        if (crc != tmp_crc) {
            return false;
        }
 
        this.data_bf.position(0);
        byte cmd = 0;
        if (tcp_modbus) {
            if (-21846 != this.data_bf.getShort()) {
                return false;
            }
            if (this.data_bf.getShort() != reg_addr % 4096) {
                return false;
            }
            this.data_bf.getShort();
            this.data_bf.get();
            cmd = this.data_bf.get();
            this.data_bf.get();
        } else {
            this.data_bf.get();
            cmd = this.data_bf.get();
            this.data_bf.get();
        }
 
        if (3 == cmd) {
            if (4 == reg_addr % 4096) {
                int battgroup_num = (int) Math.floor(reg_addr / 4096);
                for (int n = 0; n < 4; n++) {
                    this.groupvols[n] = ((this.data_bf.getShort() & 0xFFFF) / 10.0F);
                    if (n == 0)
                        this.groupvol[battgroup_num] = this.groupvols[0];
 
                }
 
                for (int n = 0; n < 4; n++) {
                    this.battcurrs[n] = ((this.data_bf.getShort() & 0xFFFF) / 10.0F);
                    if (n == 0)
                        this.battcurr[battgroup_num] = this.battcurrs[0];
 
                }
 
            }
 
            if ((reg_addr % 4096 >= 28) && (reg_addr % 4096 <= 327)) {
                String str = Com.getDateTimeFormat(new Date(), "yyyy-MM-dd HH:mm:ss") + " - ";
                for (int n = reg_count_last; n < reg_count_last + reg_count; n++) {
                    this.battvol[(reg_addr % 4096 - 28 + n)] = ((this.data_bf.getShort() & 0xFFFF) / 1000.0F);
                }
                for (int n = 0; n < 24; n++) {
                    str = str + "#" + (n + 1) + ": " + String.format("%1.3f; ",
                            new Object[] { Float.valueOf(this.battvol[(reg_addr % 4096 - 28 + n)]) });
                }
 
            } else if ((reg_addr % 4096 >= 632) && (reg_addr % 4096 <= 931)) {
                for (int n = reg_count_last; n < reg_count_last + reg_count; n++) {
                    this.batttmp[(reg_addr % 4096 - 632 + n)] = ((this.data_bf.getShort() & 0xFFFF) / 10.0F - 10.0F);
                }
 
            } else if ((reg_addr % 4096 >= 1236) && (reg_addr % 4096 <= 1535)) {
                for (int n = reg_count_last; n < reg_count_last + reg_count; n++) {
                    this.battres[(reg_addr % 4096 - 1236 + n)] = ((this.data_bf.getShort() & 0xFFFF) / 1000.0F);
                }
            }
 
        } else if (6 == cmd) {
            this.m_UsrWriteCmdAck = true;
        }
 
        return true;
    }
}