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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package com.dev.fbs9600;
 
import java.util.Date;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.battdata_rt.BattData_RT;
import com.dev.modbus4j.MyModbusMaster;
import com.dev.modbus4j.MyModbusUtils;
import com.serotonin.modbus4j.BatchRead;
import com.serotonin.modbus4j.BatchResults;
import com.serotonin.modbus4j.code.DataType;
 
public class FBS9600_State {
    private static final int BATT_MAXGOROUP_COUNT = 16;
    static final int UsrCmdNull = 0;
    static final int UsrCmdTestRes = 81;
    static final int UsrCmdTestResSuc = 82;
    static final int WorkStateNull = 0;
    static final int WorkStateRes = 1;
    public String ip_addr;
    public int dev_id;
    public int check_dev_id;                //校验设备id
    private int op_cmd;
    public int BattGroupNum;                //当前电池组号启动或停止时判断要启动和停止的组号
    private boolean op_cmd_ack_fromdev = false;
    int work_state;
    
    public int com_count;                    //通信计数
    public int com_err_count;                //连续错误计数
    
    
    public int com_totalcount;                //总的错误计数
    public int com_err_totalcount;            //总的错误计数
    
    long alm_rec_id = 0L;
    public int[] dev_works = new int[BATT_MAXGOROUP_COUNT];
    public float[] dev_currs = new float[BATT_MAXGOROUP_COUNT];
    public float[] dev_groupvols = new float[BATT_MAXGOROUP_COUNT];
    public int[] batt_type =  new int[BATT_MAXGOROUP_COUNT];                        //电池类型0铅酸;1锂电池
    public FBS9600_Lithium_Data[] lidata = new FBS9600_Lithium_Data[BATT_MAXGOROUP_COUNT];
    
    private Logger logger;
    public FBS9600_State(String ipaddr, int id) {
        this.ip_addr = ipaddr;
        this.dev_id = id;
        
        this.logger = LogManager.getLogger(this);
    }
 
    public void setWorkState(int work_state) {
        this.work_state = work_state;
    }
 
    public void clearWorkState() {
        this.work_state = 0;
    }
 
    public int getWorkState() {
        return this.work_state;
    }
 
    public void setFBS9600CmdAckFromDev(boolean stat) {
        this.op_cmd_ack_fromdev = stat;
    }
 
    public boolean getFBS9600CmdAckFromDev() {
        return this.op_cmd_ack_fromdev;
    }
 
    public void setFBS9600Cmd(int cmd) {
        this.op_cmd = cmd;
    }
 
    public int getFBS9600Cmd() {
        return this.op_cmd;
    }
 
    public void setComCountInc() {
        this.com_err_count = 0;
        this.com_count += 1;
        if (this.com_count >= 90000000)
            this.com_count = 1;
    }
 
    public int getOp_cmd() {
        return op_cmd;
    }
 
    public int getBattGroupNum() {
        return BattGroupNum;
    }
 
    public void setOp_cmd(int op_cmd) {
        this.op_cmd = op_cmd;
    }
 
    public void setBattGroupNum(int battGroupNum) {
        BattGroupNum = battGroupNum;
    }
 
    public void setComErrCountInc() {
        this.com_err_count += 1;
        this.com_err_totalcount += 1;
        if (this.com_err_totalcount >= 90000000)
            this.com_err_totalcount = 20;
            this.com_err_count = 20;
    }
 
    public int getTotalCommCount() {
        int totalCommCount = (this.com_count + this.com_totalcount);
        if(totalCommCount > 90000000) {
            this.com_totalcount = 20;
        }
        
        return (this.com_count + this.com_totalcount);
    }
    
    public int getTotalErrCommCount() {
        int totalCommErrCount = (this.com_err_count + this.com_err_totalcount);
        if(totalCommErrCount > 90000000) {
            this.com_err_totalcount = 20;
        }
        return (this.com_err_count + this.com_err_totalcount);
    }
    /**
     * 构造State 的设备ID读取集合
     * @param master
     * @return
     */
    public BatchRead<Integer> createBatchRead(MyModbusMaster master) {
        BatchRead<Integer> batch = new BatchRead<Integer>();
        batch.addLocator(0,MyModbusUtils.createBaseLocator(0xF001,DataType.TWO_BYTE_INT_UNSIGNED, master));                                    //机箱跳闸输出    
        batch.addLocator(1,MyModbusUtils.createBaseLocator(0xF002, DataType.TWO_BYTE_INT_UNSIGNED, master));                                    //故障复位信号    
        
        return batch;
    }
    
    /**
     *     解析State 的设备ID读取集合
     * @param res
     */
    public boolean putBatchResult(BatchResults<Integer> res) {
        if(res != null) {
            int dev_head = res.getIntValue(0);
            int dev_food = res.getIntValue(1);
            
            this.check_dev_id = dev_head*100000+dev_food;
            
            logger.debug("check_dev_id:"+this.check_dev_id+"\t 0xF001:"+dev_head+"\t 0xF002:"+dev_food);
            
            return true;
        }
        return false;
    }
 
    /**
     *     读取电池组组端数据信息
     * @param master
     * @return
     */
    public BatchRead<Integer> createGruopBatchRead(MyModbusMaster master,int index) {
        int offset = index*0x1000;
        BatchRead<Integer> batch = new BatchRead<Integer>();
        batch.addLocator(0,MyModbusUtils.createBaseLocator(0x0003+offset,DataType.TWO_BYTE_INT_UNSIGNED, master));        //模块状态:0-正常采集 1-内阻测试 2-单体编号                            
        batch.addLocator(1,MyModbusUtils.createBaseLocator(0x0004+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));        //组端电压                        
        batch.addLocator(2,MyModbusUtils.createBaseLocator(0x0006+offset, DataType.TWO_BYTE_INT_SIGNED, master));        //组端电流                        
        batch.addLocator(3,MyModbusUtils.createBaseLocator(0x0007+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));        //蓄电池状态                            
        batch.addLocator(4,MyModbusUtils.createBaseLocator(0x000A+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));        //纹波电流均值                            
        batch.addLocator(5,MyModbusUtils.createBaseLocator(0x000B+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));        //纹波电流峰值                                
        batch.addLocator(6,MyModbusUtils.createBaseLocator(0x0ABD+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));        //电池类型                                
        
        return batch;
    }
 
    
    
    public boolean putGroupBatchResult(BatchResults<Integer> res,int index,BattData_RT battData_RT) {
        if(res != null) {
            dev_works[index] = res.getIntValue(0);
            //System.out.println("work_state"+(index+1)+":"+dev_works[index]);
            dev_groupvols[index] = ((float)res.getIntValue(1))/10;
            battData_RT.mTestData.updateGroupVolFrom_FBSDev(dev_groupvols[index]);
            battData_RT.mTestData.newDataRecordTime = new Date();
            
            //内阻测试过程中处理
            if(dev_works[index] == FBS9600_CommData.DevWorkstate_ResTest) {                
                battData_RT.mTestData.setBatt_res_test_state(1);                
            }else {
                battData_RT.mTestData.setBatt_res_test_state(0);
            }
            
            float battcurr = ((float)((short)res.getValue(2)))/10;
            //float battcurr = 10;
            int battstate = res.getIntValue(3);            
            if (1 == battstate) {
                battcurr = Math.abs(battcurr)* -1.0F;
            }
            dev_currs[index] = battcurr;
            battData_RT.mTestData.updateCurrFrom_FBSDev(battcurr);        
            float curr_avg = ((float)res.getIntValue(4))/10;
            float curr_top = ((float)res.getIntValue(5))/10;
            //System.out.println(res);
            batt_type[index] = res.getIntValue(6);        //电池型号
            return true;
        }
        return false;
    }
 
    /**
     *     读取单体电压
     * @param master
     * @param index
     * @param monCount
     * @return
     */
    public BatchRead<Integer> createMonVolBatchRead(MyModbusMaster master, int index,int monCount) {
        int offset = index*0x1000;
        BatchRead<Integer> batch = new BatchRead<Integer>();
        for(int i=0;i<monCount;i++) {            
            batch.addLocator(i,MyModbusUtils.createBaseLocator(0x001C+offset+i,DataType.TWO_BYTE_INT_UNSIGNED, master));        //单体电压                        
        }
        return batch;
    }
 
    /**
     *     读取单体电压
     * @param res
     * @param index
     * @param battData_RT
     * @return
     */
    public boolean putMonVolBatchResult(BatchResults<Integer> res, int index, BattData_RT battData_RT) {
        if(res != null) {
            //System.out.print("组号"+(index+1) + "\t");
            for(int i=0;i<battData_RT.MonCount;i++) {
                battData_RT.al_MonVol.get(i).monVol = ((float)res.getIntValue(i))/1000;
                //System.out.print((i+1)+"#:"+battData_RT.al_MonVol.get(i).monVol+"\t");
            }    
            //System.out.println();
            return true;
        }
        return false;
    }
    
    /**
     *     读取单体内阻
     * @param master
     * @param index
     * @param monCount
     * @return
     */
    public BatchRead<Integer> createMonResBatchRead(MyModbusMaster master, int index,int monCount) {
        int offset = index*0x1000;
        BatchRead<Integer> batch = new BatchRead<Integer>();
        for(int i=0;i<monCount;i++) {            
            batch.addLocator(i,MyModbusUtils.createBaseLocator(0x04D4+offset+i,DataType.TWO_BYTE_INT_UNSIGNED, master));        //单体电压                        
        }
        return batch;
    }
 
    /**
     *     读取单体内阻
     * @param res
     * @param index
     * @param battData_RT
     * @return
     */
    public boolean putMonResBatchResult(BatchResults<Integer> res, int index, BattData_RT battData_RT) {
        if(res != null) {
            for(int i=0;i<battData_RT.MonCount;i++) {
                battData_RT.al_MonVol.get(i).monRes = ((float)res.getIntValue(i))/1000;
            }        
            return true;
        }
        return false;
    }
    
    /**
     *     读取单体温度
     * @param master
     * @param index
     * @param monCount
     * @return
     */
    public BatchRead<Integer> createMonTmpBatchRead(MyModbusMaster master, int index,int monCount) {
        int offset = index*0x1000;
        BatchRead<Integer> batch = new BatchRead<Integer>();
        for(int i=0;i<monCount;i++) {            
            batch.addLocator(i,MyModbusUtils.createBaseLocator(0x0278+offset+i,DataType.TWO_BYTE_INT_UNSIGNED, master));        //单体电压                        
        }
        return batch;
    }
 
    /**
     *     读取单体温度
     * @param res
     * @param index
     * @param battData_RT
     * @return
     */
    public boolean putMonTmpBatchResult(BatchResults<Integer> res, int index, BattData_RT battData_RT) {
        if(res != null) {
            for(int i=0;i<battData_RT.MonCount;i++) {
                battData_RT.al_MonVol.get(i).monTmp = ((float)res.getIntValue(i)-100)/10;
            }        
            return true;
        }
        return false;
    }
    /**
     *     读取单体均衡电流
     * @param master
     * @param index
     * @param monCount
     * @return
     */
    public BatchRead<Integer> createMonJhCurrBatchRead(MyModbusMaster master, int index,int monCount) {
        int offset = index*0x1000;
        BatchRead<Integer> batch = new BatchRead<Integer>();
        for(int i=0;i<monCount;i++) {            
            batch.addLocator(i,MyModbusUtils.createBaseLocator(0x098D+offset+i,DataType.TWO_BYTE_INT_UNSIGNED, master));        //单体均衡电流                    
        }
        return batch;
    }
    
    /**
     *     读取单体均衡电流
     * @param res
     * @param index
     * @param battData_RT
     * @return
     */
    public boolean putMonJhCurrBatchResult(BatchResults<Integer> res, int index, BattData_RT battData_RT) {
        if(res != null) {
            for(int i=0;i<battData_RT.MonCount;i++) {
                //System.out.println("均衡电流:"+res.getIntValue(i));
                battData_RT.al_MonVol.get(i).monJHcurr = (float)res.getIntValue(i)/1000;
            }        
            return true;
        }
        return false;
    }
 
    public void setCommData(MyModbusMaster master) {
        this.com_count = master.getTolcommcount();
        this.com_err_count = master.getTotalerr();
        
    }
    
    public BatchRead<Integer> createLithiumBatchRead(MyModbusMaster master, int index) {
        int offset = index*0x1000;
        BatchRead<Integer> batch = new BatchRead<Integer>();
        batch.addLocator(0,MyModbusUtils.createBaseLocator(0x0ABE+offset,DataType.TWO_BYTE_INT_UNSIGNED, master));            //环境温度
        batch.addLocator(1,MyModbusUtils.createBaseLocator(0x0ABF+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));            //功率温度                        
        batch.addLocator(2,MyModbusUtils.createBaseLocator(0x0AC0+offset, DataType.TWO_BYTE_INT_SIGNED, master));            //剩余容量                        
        batch.addLocator(3,MyModbusUtils.createBaseLocator(0x0AC1+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));            //自定义遥测量数量                    
        batch.addLocator(4,MyModbusUtils.createBaseLocator(0x0AC2+offset, DataType.TWO_BYTE_INT_SIGNED, master));            //电池总容量                        
        batch.addLocator(5,MyModbusUtils.createBaseLocator(0x0AC3+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));            //电池循环次数                                
        batch.addLocator(6,MyModbusUtils.createBaseLocator(0x0AC4+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));            //环境温度告警状态                            
        batch.addLocator(7,MyModbusUtils.createBaseLocator(0x0AC5+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));            //功率温度告警状态                        
        batch.addLocator(8,MyModbusUtils.createBaseLocator(0x0AC6+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));            //充放电电流告警状态                        
        batch.addLocator(9,MyModbusUtils.createBaseLocator(0x0AC7+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));            //电池总电压告警状态                    
        batch.addLocator(10,MyModbusUtils.createBaseLocator(0x0AC8+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));        //自定义告警量数量                    
        batch.addLocator(11,MyModbusUtils.createBaseLocator(0x0AC9+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));        //均衡事件代码                        
        batch.addLocator(12,MyModbusUtils.createBaseLocator(0x0ACA+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));        //电压事件代码                        
        batch.addLocator(13,MyModbusUtils.createBaseLocator(0x0ACB+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));        //温度事件代码                        
        batch.addLocator(14,MyModbusUtils.createBaseLocator(0x0ACC+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));        //电流事件代码                        
        batch.addLocator(15,MyModbusUtils.createBaseLocator(0x0ACD+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));        //FET状态代码                        
        batch.addLocator(16,MyModbusUtils.createBaseLocator(0x0ACE+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));        //均衡状态代码                            
        //batch.addLocator(17,MyModbusUtils.createBaseLocator(0x0ACF+offset, DataType.TWO_BYTE_INT_UNSIGNED, master));        //系统状态代码                        
        
        return batch;
    }
 
    public boolean putLithiumBatchResult(BatchResults<Integer> res, int index,BattData_RT battData_RT) {
        if(null != res) {
            if(null == lidata[index]) {
                lidata[index] = new FBS9600_Lithium_Data(battData_RT.BattGroupId);
            }
            FBS9600_Lithium_Data data = lidata[index];
            data.envir_tmp = ((float)res.getIntValue(0)-100)/10;                    //'环境温度',
            data.power_tmp = ((float)res.getIntValue(1)-100)/10;                    //'功率温度',
            data.rest_cap = (short)res.getValue(4);                                    //'剩余容量',
            data.remote_control_count = res.getIntValue(3);                            //'自定义遥控数量',
            data.batt_total_cap = (short)res.getValue(4);                            //'电池总容量',
            data.batt_cycles = res.getIntValue(5);                                    //'电池循环次数',
            data.evir_tmp_alm_state = res.getIntValue(6);                            //'环境温度告警状态',
            data.power_tmp_alm_state = res.getIntValue(7);                            //'功率温度告警状态',
            data.discharge_curr_alm_state = res.getIntValue(8);                        //'充放电电流告警状态',
            data.batt_totalvol_alm_state = res.getIntValue(9);                        //'电池总电压告警状态',
            data.custom_alm_count = res.getIntValue(10);                            //'自定义告警量数量',
            data.junheng_event_code = res.getIntValue(11);                            //'均衡事件代码',
            data.vol_event_code = res.getIntValue(12);                                //'电压事件代码',
            data.tmp_event_code = res.getIntValue(13);                                //'温度事件代码',
            data.curr_event_code = res.getIntValue(14);                                //'电流事件代码',
            data.fet_state_code = res.getIntValue(15);                                //'FET状态代码',
            data.junheng_state_code = res.getIntValue(16);                            //'均衡状态代码',
            //data.sys_state_code = (int)res.getValue(17);                            //'系统状态代码',
            return true;
        }
        return false;
    }
    
    public static void main(String[] args) {
        short ff = 12;
        System.out.println((float)ff);
    }
}