9度通讯程序适用于9度多组设备
Administrator
2023-12-27 c736aaea5522e295b42676f094d313e7f6a4afb4
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.fbs9009;
 
import java.nio.ByteBuffer;
import java.util.Arrays;
 
import com.base.ComBase;
import com.dev.fbs9100.ComFn;
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 LD_batt_states {
    public static int BYTE_LEN=72;           //¼Ä´æÆ÷¸öÊý
    public static int MONDATA_BYTE_LEN = 27;    
    
    private int cmd;                     //·¢ËÍÊý¾ÝµÄÃüÁî
    private int cmd_addr;                 //Ä£¿éµØÖ·
    private int cmd_addr_back;             //¼Ä´æÆ÷µÄÆðʼµØÖ·
    private int dataCount;                 //Êý¾ÝµÄ³¤¶È
    
    private LD_batt_state[] ld_batts=new LD_batt_state[9];    //µç³ØÊý¾Ý
    private double max_mon_vol;            //×î´óµ¥Ìåµçѹ
    private int max_mon_num;            //×î´óµ¥ÌåµçѹµÄ±àºÅ
    private double min_mon_vol;            //×îµÍµ¥Ìåµçѹ
    private double min_mon_num;            //×îµÍµ¥ÌåµçѹµÄ±àºÅ
    
    public double[] mon_vols = new double[9];        //µ¥Ìåµçѹ
    public double[] mon_tmps = new double[9];        //µ¥ÌåζÈ
    public double[] mon_ress = new double[9];        //µ¥ÌåÄÚ×è
    
    
    public boolean setLD_batt_state(ByteBuffer bf){
        boolean flag = false;
          ByteBuffer tmpbuf = bf;    
          //System.out.println("pos:"+bf.position()+"\tlimit:"+bf.limit()+"\tcap"+bf.capacity());
          tmpbuf.position(0);    
          
          try {
            this.cmd_addr = ComBase.changeByteToInt(tmpbuf.get());                    //»ñÈ¡É豸µØÖ·
            this.cmd = ComBase.changeByteToInt(tmpbuf.get());
            this.cmd_addr_back = ComBase.changeShortToInt(tmpbuf.getShort());        //»ñÈ¡¼Ä´æÆ÷µÄÆðʼµØÖ·
            //this.dataCount = ComBase.changeByteToInt(tmpbuf.get());                //Êý¾ÝÇø×Ö½ÚÊý        
            this.dataCount = ComBase.changeByteToInt(tmpbuf.get());
            //System.out.println("Êý¾ÝÇø×Ö½ÚÊýµÄ³¤¶È:"+this.dataCount);
            for (int i = 0; i < LD_batt_states.BYTE_LEN/8; i++) {
                LD_batt_state ld=new LD_batt_state();
                ld.setBatt_num(i+1);
                if(tmpbuf.remaining() < 16) {
                    System.out.println("setLD_batt_state:"+ComFn.bytesToHexString(tmpbuf.array(), tmpbuf.limit()));
                    System.out.println("setLD_batt_state pos:"+tmpbuf.position()+"\tlimit:"+tmpbuf.limit()+"\tcap"+tmpbuf.capacity());
                    break;
                }
                ld.setBatt_state(ComBase.changeShortToInt(tmpbuf.getShort()));        
                ld.setBatt_vol(ComBase.changeShortToDouble(tmpbuf.getShort())*0.001);             
                ld.setBatt_curr(ComBase.changeShortToDouble(tmpbuf.getShort())*0.001);            
                ld.setBatt_cap(ComBase.changeShortToDouble(tmpbuf.getShort())*0.01);            
                ld.setStop_reason(ComBase.changeShortToInt(tmpbuf.getShort()));
                ld.setTest_time_hour(ComBase.changeShortToInt(tmpbuf.getShort()));     
                ld.setTest_time_minute(ComBase.changeShortToInt(tmpbuf.getShort()));        
                ld.setTest_time_second(ComBase.changeShortToInt(tmpbuf.getShort()));
                this.ld_batts[i]=ld;
                if(i == 0){
                    this.max_mon_vol = ld.getBatt_vol();            
                    this.max_mon_num = ld.getBatt_num();        
                    this.min_mon_vol = ld.getBatt_vol();
                    this.min_mon_num = ld.getBatt_num();
                }
                if(ld.getBatt_vol() > max_mon_vol){
                    max_mon_vol = ld.getBatt_vol();
                    max_mon_num = ld.getBatt_num();
                }
                if(ld.getBatt_vol() < min_mon_vol){
                    min_mon_vol = ld.getBatt_vol();
                    min_mon_num = ld.getBatt_num();
                }
            }
            flag = true;
        } catch (Exception e) {
            System.out.println(ComFn.bytesToHexString(tmpbuf.array(), tmpbuf.limit()));
            System.err.println("Exception pos:"+tmpbuf.position()+"\tlimit:"+tmpbuf.limit()+"\tcap"+tmpbuf.capacity());
            e.printStackTrace();
        } 
        return flag;
    }    
    
    public boolean setLD_battmon_state(ByteBuffer bf){
        boolean flag = false;
          ByteBuffer tmpbuf = bf;    
          //System.out.println("pos:"+bf.position()+"\tlimit:"+bf.limit()+"\tcap"+bf.capacity());
          tmpbuf.position(0);    
          
          try {
            this.cmd_addr = ComBase.changeByteToInt(tmpbuf.get());                    //»ñÈ¡É豸µØÖ·
            this.cmd = ComBase.changeByteToInt(tmpbuf.get());
            this.cmd_addr_back = ComBase.changeShortToInt(tmpbuf.getShort());        //»ñÈ¡¼Ä´æÆ÷µÄÆðʼµØÖ·
            //this.dataCount = ComBase.changeByteToInt(tmpbuf.get());                //Êý¾ÝÇø×Ö½ÚÊý        
            this.dataCount = ComBase.changeByteToInt(tmpbuf.get());
            //System.out.println("Êý¾ÝÇø×Ö½ÚÊýµÄ³¤¶È:"+this.dataCount);
            
            if(this.dataCount >= 9*3) {
                //System.out.print("µ¥Ìåµçѹ:");
                for(int i=0;i<this.mon_vols.length;i++) {
                    this.mon_vols[i] = (ComBase.changeShortToInt(tmpbuf.getShort())*0.001);
                    //System.out.print(mon_vols[i]+"\t");
                }
                //System.out.println();
                //System.out.print("µ¥ÌåζÈ:");
                for(int i=0;i<this.mon_tmps.length;i++) {
                    this.mon_tmps[i] = (ComBase.changeShortToInt(tmpbuf.getShort())*0.1) - 10.0f;
                    //System.out.print(mon_tmps[i]+"\t");
                }
                //System.out.println();
                //System.out.print("µ¥ÌåÄÚ×è:");
                for(int i=0;i<this.mon_ress.length;i++) {
                    this.mon_ress[i] = (ComBase.changeShortToInt(tmpbuf.getShort())*0.001);
                    //System.out.print(mon_ress[i]+"\t");
                }
                flag = true;
            }else {
                flag = false;
            }
        } catch (Exception e) {
            System.out.println(ComFn.bytesToHexString(tmpbuf.array(), tmpbuf.limit()));
            System.err.println("Exception pos:"+tmpbuf.position()+"\tlimit:"+tmpbuf.limit()+"\tcap"+tmpbuf.capacity());
            e.printStackTrace();
        } 
        return flag;
    }
 
    //Åжϵ¥ÌåÊÇ·ñ´¦ÓÚºËÈݲâÊÔ
    public boolean isDischarge(){
        for(int i=0;i<this.ld_batts.length;i++){
            if(this.ld_batts[i] != null && this.ld_batts[i].batt_state == MyModBusCom.BATT_DISCHARGE){
                return true;
            }
        }
        return false;
    }
    
    //»ñÈ¡µ±Ç°ÕýÔÚ½øÐкËÈݲâÊԵĵ¥ÌåµÄ±àºÅ
    public int getNowTestMonnum(){
        int mon_num = 0;
        for(int i=0;i<this.ld_batts.length;i++){
            if(this.ld_batts[i].batt_state == MyModBusCom.BATT_DISCHARGE){
                return (i+1);
            }
        }
        return mon_num;
    }
    
    /**
     * »ñÈ¡µ¥ÌåµÄ²âÊÔµçÁ÷         ÓÅÏȼ¶¹ØÏµ           ·ÅµçµçÁ÷      >>    ³äµçµçÁ÷       >>     0
     * µ±Óе¥ÌåÔڷŵçµÄʱºò,×é¶ËµÄµçÁ÷ÏÔʾ·ÅµçµÄµçÁ÷(²»¹ÜÊÇ·ñÓÐûÓе¥ÌåÔÚ³äµç)
     * µ±Ã»Óе¥ÌåÔڷŵç,Óе¥ÌåÔÚ³äµçµÄʱºòÏÔʾ³äµçµÄ¶îʱºòµÄµçÁ÷
     * ¼Èûµ¥Ìå·Åµç,ҲûÓе¥Ìå³äµçµÄʱºòÏÔʾ0
     * @return
     */
    public double getBattCurr(){
        double battcurr = 0;
        for(int i = 0 ; i < this.ld_batts.length ; i++){
            if(this.ld_batts[i].batt_state == MyModBusCom.BATT_DISCHARGE){
                battcurr = this.ld_batts[i].batt_curr;
                break;
            }
            if(this.ld_batts[i].batt_state == MyModBusCom.BATT_CHARGE && battcurr == 0){
                battcurr = this.ld_batts[i].batt_curr;
            }
        }
        return battcurr;
    }
    
    
    /**
     * »ñÈ¡µ¥ÌåµÄ²âÊÔÈÝÁ¿         ÓÅÏȼ¶¹ØÏµ           ·ÅµçµçÁ÷      >>    ³äµçµçÁ÷       >>     0
     * µ±Óе¥ÌåÔڷŵçµÄʱºò,×é¶ËµÄµçÁ÷ÏÔʾ·ÅµçµÄ²âÊÔÈÝÁ¿(²»¹ÜÊÇ·ñÓÐûÓе¥ÌåÔÚ³äµç)
     * µ±Ã»Óе¥ÌåÔڷŵç,Óе¥ÌåÔÚ³äµçµÄʱºòÏÔʾ³äµçµÄ¶îʱºòµÄÈÝÁ¿
     * ¼Èûµ¥Ìå·Åµç,ҲûÓе¥Ìå³äµçµÄʱºòÏÔʾ0
     * @return
     */
    public double getBattTestCap(){
        double test_cap = 0;
        for(int i = 0 ; i < this.ld_batts.length ; i++){
            if(this.ld_batts[i].batt_state == MyModBusCom.BATT_DISCHARGE){
                test_cap = this.ld_batts[i].batt_cap;
                break;
            }
            if(this.ld_batts[i].batt_state == MyModBusCom.BATT_CHARGE && test_cap == 0){
                test_cap = this.ld_batts[i].batt_cap;
            }
        }
        return test_cap;
    }
    
    public double getMax_mon_vol() {
        return max_mon_vol;
    }
 
    public void setMax_mon_vol(double max_mon_vol) {
        this.max_mon_vol = max_mon_vol;
    }
 
    public int getMax_mon_num() {
        return max_mon_num;
    }
 
    public void setMax_mon_num(int max_mon_num) {
        this.max_mon_num = max_mon_num;
    }
 
    public double getMin_mon_vol() {
        return min_mon_vol;
    }
 
    public void setMin_mon_vol(double min_mon_vol) {
        this.min_mon_vol = min_mon_vol;
    }
 
    public double getMin_mon_num() {
        return min_mon_num;
    }
 
    public void setMin_mon_num(double min_mon_num) {
        this.min_mon_num = min_mon_num;
    }
 
    public LD_batt_state[] getLd_batts() {
        return ld_batts;
    }
 
    public void setLd_batts(LD_batt_state[] ld_batts) {
        this.ld_batts = ld_batts;
    }
 
    
    
    class LD_batt_state{
        private int batt_num;                //µç³Ø±àºÅ
        private int batt_state;              //µç³Ø×´Ì¬ 
        private double batt_vol;             //µç³Øµçѹ
        private double batt_curr;            //µç³ØµçÁ÷
        private double batt_cap;             //µç³ØÈÝÁ¿
        private int stop_reason;             //Í£Ö¹Ô­Òò
        private int test_time_hour;          //²âÊÔʱ¼ä¡ª¡ªÐ¡Ê±
        private int test_time_minute;        //²âÊÔʱ¼ä¡ª¡ª·ÖÖÓ
        private int test_time_second;        //²âÊÔʱ¼ä¡ª¡ªÃë
        
        public int getBatt_num() {
            return batt_num;
        }
        public void setBatt_num(int batt_num) {
            this.batt_num = batt_num;
        }
        public int getBatt_state() {
            return batt_state;
        }
        public void setBatt_state(int batt_state) {
            this.batt_state = batt_state;
        }
        public double getBatt_vol() {
            return batt_vol;
        }
        public void setBatt_vol(double batt_vol) {
            this.batt_vol = batt_vol;
        }
        public double getBatt_curr() {
            return batt_curr;
        }
        public void setBatt_curr(double batt_curr) {
            this.batt_curr = batt_curr;
        }
        public double getBatt_cap() {
            return batt_cap;
        }
        public void setBatt_cap(double batt_cap) {
            this.batt_cap = batt_cap;
        }
        public int getStop_reason() {
            return stop_reason;
        }
        public void setStop_reason(int stop_reason) {
            this.stop_reason = stop_reason;
        }
        public int getTest_time_hour() {
            return test_time_hour;
        }
        public void setTest_time_hour(int test_time_hour) {
            this.test_time_hour = test_time_hour;
        }
        public int getTest_time_minute() {
            return test_time_minute;
        }
        public void setTest_time_minute(int test_time_minute) {
            this.test_time_minute = test_time_minute;
        }
        public int getTest_time_second() {
            return test_time_second;
        }
        public void setTest_time_second(int test_time_second) {
            this.test_time_second = test_time_second;
        }
        @Override
        public String toString() {
            return "LD_batt_state [batt_num=" + batt_num + " batt_vol=" + batt_vol
                    + ", batt_curr=" + batt_curr + ", batt_cap=" + batt_cap + ", stop_reason=" + stop_reason + "]";
        }
        
    }
 
    @Override
    public String toString() {
        return "LD_batt_states [cmd=" + cmd + ", cmd_addr=" + cmd_addr + ", dataCount=" + dataCount + ", ld_batts="
                + Arrays.toString(ld_batts) + "]";
    }
 
    public BatchRead<Integer> createLDBattStateBatchRead(MyModbusMaster master, int index) {
        int offset = MyModBusData.BATT_STATE_ADDR_INDEX[index];
        int count = 0;
        BatchRead<Integer> batch = new BatchRead<Integer>();
        for(int i=0;i<ld_batts.length;i++) {
            batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++,DataType.TWO_BYTE_INT_UNSIGNED, master));        //µç³Ø×´Ì¬                            
            batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++, DataType.TWO_BYTE_INT_UNSIGNED, master));    //µç³Øµçѹ                        
            batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++, DataType.TWO_BYTE_INT_UNSIGNED, master));    //µç³ØµçÁ÷                    
            batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++, DataType.TWO_BYTE_INT_UNSIGNED, master));    //µç³ØÈÝÁ¿                            
            batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++, DataType.TWO_BYTE_INT_UNSIGNED, master));    //Í£Ö¹Ô­Òò                    
            batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++, DataType.TWO_BYTE_INT_UNSIGNED, master));    //²âÊÔʱ³¤(H)                    
            batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++, DataType.TWO_BYTE_INT_UNSIGNED, master));    //²âÊÔʱ³¤(M)                
            batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++, DataType.TWO_BYTE_INT_UNSIGNED, master));    //²âÊÔʱ³¤(S)                
        }        
        return batch;
    }
 
    public boolean putLDBattStateBatchResult(BatchResults<Integer> res) {
        if(null != res) {
            int index = 0;
            for(int i = 0;i<ld_batts.length;i++) {
                LD_batt_state ld = ld_batts[i];
                ld.setBatt_num(i+1);
                
                ld.setBatt_state(res.getIntValue(index++));        
                ld.setBatt_vol((double)res.getIntValue(index++)*0.001);             
                ld.setBatt_curr((double)res.getIntValue(index++)*0.001);            
                ld.setBatt_cap((double)res.getIntValue(index++)*0.01);            
                ld.setStop_reason(res.getIntValue(index++));
                ld.setTest_time_hour(res.getIntValue(index++));     
                ld.setTest_time_minute(res.getIntValue(index++));        
                ld.setTest_time_second(res.getIntValue(index++));
                
                if(i == 0){
                    this.max_mon_vol = ld.getBatt_vol();            
                    this.max_mon_num = ld.getBatt_num();        
                    this.min_mon_vol = ld.getBatt_vol();
                    this.min_mon_num = ld.getBatt_num();
                }
                if(ld.getBatt_vol() > max_mon_vol){
                    max_mon_vol = ld.getBatt_vol();
                    max_mon_num = ld.getBatt_num();
                }
                if(ld.getBatt_vol() < min_mon_vol){
                    min_mon_vol = ld.getBatt_vol();
                    min_mon_num = ld.getBatt_num();
                }
            }
            return true;
        }
        return false;
    }
 
    public BatchRead<Integer> createLDMonDataBatchRead(MyModbusMaster master, int index) {
        int offset = MyModBusData.BATT_MON_DATA_ADDR_INDEX[index];
        int count = 0;
        BatchRead<Integer> batch = new BatchRead<Integer>();
        for(int i=0;i<mon_vols.length;i++) {
            batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++,DataType.TWO_BYTE_INT_UNSIGNED, master));        //µç³Ø×´Ì¬                            
        }        
        for(int i=0;i<mon_tmps.length;i++) {
            batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++,DataType.TWO_BYTE_INT_UNSIGNED, master));        //µç³Ø×´Ì¬                            
        }        
        for(int i=0;i<mon_ress.length;i++) {
            batch.addLocator(count,MyModbusUtils.createBaseLocator(offset+count++,DataType.TWO_BYTE_INT_UNSIGNED, master));        //µç³Ø×´Ì¬                            
        }        
        return batch;
    }
 
    public boolean putLDMonDataBatchResult(BatchResults<Integer> res) {
        if(null != res) {
            int index = 0;
            //System.out.print("µ¥Ìåµçѹ:");
            for(int i=0;i<this.mon_vols.length;i++) {
                this.mon_vols[i] = (double)res.getIntValue(index++)*0.001;
                //System.out.print(mon_vols[i]+"\t");
            }
            //System.out.println();
            //System.out.print("µ¥ÌåζÈ:");
            for(int i=0;i<this.mon_tmps.length;i++) {
                this.mon_tmps[i] = ((double)res.getIntValue(index++)*0.1) - 10.0f;
                //System.out.print(mon_tmps[i]+"\t");
            }
            //System.out.println();
            //System.out.print("µ¥ÌåÄÚ×è:");
            for(int i=0;i<this.mon_ress.length;i++) {
                this.mon_ress[i] = ((double)res.getIntValue(index++)*0.001);
                //System.out.print(mon_ress[i]+"\t");
            }
            return true;
        }
        return false;
    }         
}