DELL
2025-04-28 e6eb7fb0af366e370f125668d62e89eb0004f517
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
package com.dev.simpower;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Date;
 
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
 
import org.apache.logging.log4j.Logger;
 
import sp_comm.CommSerialPort;
import sp_comm.Comm_Socket;
import main.page_debug_inf;
 
import com.Com;
import com.ComFn;
import com.Crc16;
import com.PlaySound;
import com.dev.bts.data.FBS9100_ComBase;
import com.dev.bts.data.FBS9100_DFU;
import com.dev.fbs9600_mon.SPCommMon;
import com.dev.modbus.MyModBusRtu;
import com.dev.simpower.dev.Power_ACData;
import com.dev.simpower.dev.Power_AlmParam;
import com.dev.simpower.dev.Power_AlmState;
import com.dev.simpower.dev.Power_ComBase;
import com.dev.simpower.dev.Power_LIBMSData;
import com.dev.simpower.dev.Power_ModelData;
import com.dev.simpower.dev.Power_Param;
import com.dev.simpower.dev.Power_RoadState;
import com.dev.simpower.dev.Power_TimeControl;
 
public class SPCommPower implements Runnable/*, SerialPortEventListener*/ {
 
    public static final byte MODBUS_TYPE_YC = 0x01;
    public static final byte MODBUS_TYPE_STD = 0x02;
    
    public static final short CMD_TYPE_READ = 0x03;
    
    public static final short CMD_TYPE_WRITE_YC = 0x05;
    public static final short CMD_TYPE_WRITE_STD = 0x06;
    public static final short CMD_TYPE_WRITE_MULTY = 0x10;
    
    public static final int DEV_PARAM_DATA_STATE_NULL = 0;
    public static final int DEV_PARAM_DATA_STATE_QUERY = 1;
    public static final int DEV_PARAM_DATA_STATE_UPDATE = 2;
    
    
    private ByteBuffer dt_databuffer = ByteBuffer.allocate(0);                //Ò£¿ØÒ£µ÷Êý¾Ý
    private short dt_user_cmd = Power_ComBase.CMD_NULL;
    private int dt_user_cmd_regaddr = 0;                                    //Óû§¿ØÖÆÊ±µÄ¼Ä´æÆ÷µØÖ·
    
    /*********************************************************************/
    
    public short m_CMD_TYPE_WRITE = CMD_TYPE_WRITE_YC;
    private byte m_MODBUS_TYPE = MODBUS_TYPE_YC;
    
    private  boolean CommThreadRunning = false;
    public  boolean AutoStartResTestEn = false;
    public  Date AutoStartResTestTime = new Date();
    public  Date AutoSaveResDataTime = new Date();
    public  boolean AutoSaveResDataEn = false;
    public  int AutoSaveResDataCount = 0;
    public  String AutoSaveDataFileName = "";
    
    private ByteBuffer CommRxBuffer = ByteBuffer.allocate(1500); 
    private ByteBuffer CommTxBuffer = ByteBuffer.allocate(1048);
    
    public int comm_tx_cnt = 0;
    public int comm_rx_cnt = 0;
    
    private int comm_bautrate = 9600;
    public int dt_target_addr = 0;
    public int m_ZDHJ_REG_CNT = 1;
    public int ZDHJ_BattCount = -1;
    //--------------------------------------------------------//
    public int dev_param_data_state = 0;
    public int dev_param_addr = 0;
    public int dev_param_curr_range = 0;
    public double dev_param_FloatCurrLev = 0;
    public double dev_param_float_curr_lev = 0;
    public double dev_param_wenbo_curr_lev = 0;
    public int dev_param_auto_restest_interval = 0;
    public int dev_param_res_test_type = 0;
    public int dev_param_ResDisTestMethod = 0;
    public int dev_param_Batt_StdCap = 0;
    public double dev_param_res_dis_slope = 0;
    public double dev_param_res_wenbo_slope = 0;
    public int dev_param_monnum_setmode = 0;
    public int dev_param_devid = 0;
    public double[] dev_param_resparam_d = new double[4];
    public double[] dev_param_resparam_s = new double[4];
    //--------------------------------------------------------//
    public int dev_stat_version = 0;
    public int dev_stat_state = 0;
    public double GroupVol = 0;
    public double BattCurr = 0;
    public int BattState = 0;
    public int BattCurrDir = 0;
    public double AvgWenBoCurr = 0;
    public double PeakWenBoCurr = 0;
    public int SpiCommErrCount = 0;                                      //SPIͨÐÅ´íÎó¼ÆÊý
    public int ResTestCount = 0;                                     //ÄÚ×è²âÊÔ´ÎÊý
    public int CurentResTestMode = 0;                                 //µ±Ç°ÄÚ×è²âÊÔ·½·¨
    public int BianhaoErrNum = 0;                                     //±àºÅ´íÎóµ¥ÌåºÅ
    public double ExTemp = 0;                                        //ÍⲿζÈ
    //--------------------------------------------------------//
    public short mAddrSettedNow = 0;
    
    private short dt_cmd = CMD_TYPE_READ;
    public int dt_cmd_ack = Power_ComBase.CMD_NULL;
    public MyModBusRtu rtu_rt;
 
    public MyModBusRtu rtu_rx;
    
    public Date dt_cmd_ack_time = new Date();
    private short dt_cmd_regaddr = 0;
    private short dt_cmd_regcnt = 0;
    private short[] dt_cmd_regdata = null;
    
    private int dt_cmd_cnt = 0;
    
    private page_debug_inf dt_debug_inf;
    private int m_COMM_PORT_TYPE = 0;
    private CommSerialPort m_SP_Comm = null;
    private Comm_Socket m_SocketComm = null;
    private Logger m_Log = null;
    
    private int read_libms_index = 0;    //¶ÁÈ¡ï®µç³Ø°üË÷Òý
    private int read_model_index = 0;    //¶ÁÈ¡µçÔ´Ä£¿éË÷Òý
    
    public Power_ACData m_AcData;
    public Power_LIBMSData[] m_LibmsDatas;
    public Power_ModelData[] m_ModelDatas;
    public Power_AlmState m_AlmState;
    
    public Power_Param m_ParamFromDev;                //É豸¶ÁÈ¡µÄ²ÎÊý
    public Power_TimeControl m_TimeControlFromDev;    //É豸¶ÁÈ¡µÄ²ÎÊý
    public Power_RoadState m_RoadStateFromDev;        //É豸¶ÁÈ¡µÄ²ÎÊý
    
    public Power_AlmParam m_AlmParamFromDev;    //É豸¶ÁÈ¡µÄ¸æ¾¯²ÎÊý
    
    public String op_result_sysparam = "";                    //ϵͳ²ÎÊý½á¹û×Ö·û´®
    public String op_result_powerparam = "";                //µçÔ´½á¹û×Ö·û´®
    public String op_result_almaram = "";                    //µçÔ´¸æ¾¯½á¹û×Ö·û´®
    public String op_result_jiaozhun = "";                    //µçԴУ׼½á¹û×Ö·û´®
    
    
    public short dt_user_cmd_ack = Power_ComBase.CMD_NULL;                    //²Ù×÷·µ»Ø½á¹û,ÓÃÓÚ½çÃæÅжÏgeng'xin
    
    private boolean dt_dfu_en = false;                        //Ô¶³ÌÉý¼¶±êʶ
    
    private JTextArea dt_show_msg;                            //Éý¼¶Îı¾ÐÅÏ¢Óò
            
    private String dt_dfuFileName;                            //Éý¼¶ÎļþÃû³Æ
    
    public SPCommPower(int addr_t, int bitrate, page_debug_inf debug, Logger log) {
        dt_target_addr = addr_t & 0x00FF;
        comm_bautrate = bitrate;
        dt_debug_inf = debug;
        m_Log = log;
        rtu_rx = new MyModBusRtu();
        
        m_RoadStateFromDev = new Power_RoadState();
        m_TimeControlFromDev = new Power_TimeControl();
        m_ParamFromDev = new Power_Param();
        m_AcData = new Power_ACData();
        m_AlmState = new Power_AlmState();
        
        m_LibmsDatas = new Power_LIBMSData[Power_ComBase.LIBMSDATA_MAX_COUNT];
        for(int i =0;i<m_LibmsDatas.length;i++) {
            m_LibmsDatas[i] = new Power_LIBMSData();
        }
        
        m_ModelDatas = new Power_ModelData[Power_ComBase.LIMODEL_MAX_COUNT];
        for(int i =0;i<m_LibmsDatas.length;i++) {
            m_ModelDatas[i] = new Power_ModelData();
        }
    }
    
    public void setCommBautrate(int bt_rate) {
        if(SPCommMon.COMM_PORT_TYPE_Serial != m_COMM_PORT_TYPE) {
            return;
        }
        
        try {
            comm_bautrate = bt_rate;
            m_SP_Comm.serialPort.setSerialPortParams(comm_bautrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    //³õʼ»¯´®¿Ú
    public boolean OpenCommPort(int comm_port_type, String comm_name_or_ip, int bitrate_or_comm_port) {
        boolean res = false;
        m_COMM_PORT_TYPE = comm_port_type;
        if(SPCommMon.COMM_PORT_TYPE_Serial == m_COMM_PORT_TYPE) {
            m_SP_Comm = new CommSerialPort(bitrate_or_comm_port, m_Log);
            res = m_SP_Comm.OpenCommPort(comm_name_or_ip);
        } else {
            m_SocketComm = new Comm_Socket(comm_name_or_ip, bitrate_or_comm_port);
            res = true;
        }
        
        if(true == res) {
            CommTxBuffer.order(ByteOrder.BIG_ENDIAN);
            CommRxBuffer.order(ByteOrder.BIG_ENDIAN);
            comm_tx_cnt = 0;
            comm_rx_cnt = 0;
            CommThreadRunning = true;
        }
        
        return res;
    }
    
    private void makeCommTxData() {
        CommTxBuffer.clear();
        
        rtu_rt = new MyModBusRtu();
        ByteBuffer dt_data = ByteBuffer.allocate(0);
        
        rtu_rt.addr = dt_target_addr;
        if(dt_user_cmd == Power_ComBase.CMD_NULL) {
            //Ëæ»ú¶ÁȡʵʱÊý¾Ý
            
            if(++dt_cmd_cnt  > 1000) {
                dt_cmd_cnt = 0;
            }
            rtu_rt.Cmd = MyModBusRtu.CMD_TYPE_READ;
            
            if(dt_cmd_cnt%3 == 0) {
                //¶ÁÈ¡µçÔ´Ò£²âÊý¾Ý-²â
                rtu_rt.reg_addr = Power_ComBase.PWD_ACData_RegAddr_Start;
                rtu_rt.reg_count = Power_ACData.REG_Count;
                //rtu_rt.reg_count = 17;     //²âÊÔʱ×î¶à¶ÁÈ¡17¸ö¼Ä´æÆ÷
                
            }else if(dt_cmd_cnt%3 == 1) {
                //¶ÁÈ¡ï®µç³Ø°üÊý¾Ý
                rtu_rt.reg_addr = Power_ComBase.PWD_LIBMSData_RegAddr_Start[read_libms_index];
                rtu_rt.reg_count = Power_LIBMSData.REG_Count;
                
            }else if(dt_cmd_cnt%3 == 2) {
                //¶ÁÈ¡µçÔ´Ä£¿éÊý¾Ý
                rtu_rt.reg_addr = Power_ComBase.PWD_ModelData_RegAddr_Start[read_model_index];
                rtu_rt.reg_count = Power_ModelData.REG_Count;
            }
            
            if(dt_cmd_cnt%5 == 0) {
                //¶ÁÈ¡¸æ¾¯×´Ì¬
                rtu_rt.reg_addr = Power_ComBase.PWD_ModelAlm_RegAddr_Start;
                rtu_rt.reg_count = Power_AlmState.REG_Count;
            }
            if(dt_cmd_cnt%5 == 1) {
                //·Ö·¿ª¹Ø×´Ì¬
                rtu_rt.reg_addr = Power_ComBase.PWD_LoadControl_RegAddr_Start;
                rtu_rt.reg_count = Power_RoadState.REG_Count;
            }
        
        } else {
            //Êý¾Ý
            dt_data = dt_databuffer;
            //Ò£µ÷Ò£¿ØÊý¾Ý
            rtu_rt.Cmd = dt_user_cmd;
            dt_user_cmd = Power_ComBase.CMD_NULL;
            
            rtu_rt.reg_addr = dt_user_cmd_regaddr;
            if(dt_data.capacity() > 0) {
                rtu_rt.reg_count = dt_data.capacity()/2;        
            }else if(Power_ComBase.PWD_SysParam_RegAddr_Start == rtu_rt.reg_addr) {
                rtu_rt.reg_count = Power_Param.REG_Count;
            
            }else if(Power_ComBase.PWD_LoadControl_RegAddr_Start <= rtu_rt.reg_addr && rtu_rt.reg_addr <= Power_ComBase.PWD_LoadControl_RegAddr_Start) {
                //·Ö·¿ØÖÆÒÔ¼°ÃÅ´Å¿ØÖÆ
                rtu_rt.reg_count = Power_Param.Reg_Count_Control;
            
            }else if(Power_ComBase.PWD_AlmParam_RegAddr_Start == rtu_rt.reg_addr) {
                //¸æ¾¯²ÎÊý
                rtu_rt.reg_count = Power_AlmParam.REG_Count;
            }else if(Power_ComBase.PWD_PowerParam_RegAddr_Start == rtu_rt.reg_addr) {
                //µçÔ´²ÎÊý
                rtu_rt.reg_count = Power_Param.REG_Count_Power;
            }else if(Power_ComBase.PWD_SchoolTime_RegAddr_Start == rtu_rt.reg_addr) {
                //Уʱ²ÎÊý
                rtu_rt.reg_count = Power_Param.REG_Count_Time;
            }else if(Power_ComBase.PWD_Load1Time_RegAddr_Start == rtu_rt.reg_addr) {
                //·Ö·¶¨Ê±¿ØÖÆ1-6
                rtu_rt.reg_count = Power_TimeControl.REG_Count;
            }else if(Power_ComBase.PWD_Load7Time_RegAddr_Start == rtu_rt.reg_addr) {
                //·Ö·¶¨Ê±¿ØÖÆ7-12
                rtu_rt.reg_count = Power_TimeControl.REG_Count;
            }
        }
        
        makeRTData(rtu_rt,dt_data);
    }
        
    public void makeRTData(MyModBusRtu rtu,ByteBuffer bf) {
        CommTxBuffer.position(0);
        
        CommTxBuffer.put((byte)rtu.addr);
        CommTxBuffer.put((byte)rtu.Cmd);
        CommTxBuffer.putShort((short)rtu.reg_addr);
        
        if(rtu.Cmd == MyModBusRtu.CMD_TYPE_READ) {
            //¶ÁÈ¡
            CommTxBuffer.putShort((short)rtu.reg_count);
            
        } else if(
                rtu.Cmd == MyModBusRtu.CMD_TYPE_WRITE_STD 
                || rtu.Cmd == MyModBusRtu.CMD_TYPE_WRITE_SysUpdate 
                || rtu.Cmd == MyModBusRtu.CMD_TYPE_Read_UpdateFile
        ) {
            //µ¥µãдÈë
            CommTxBuffer.put(bf);
        } else if(rtu.Cmd == MyModBusRtu.CMD_TYPE_WRITE_MULTY) {
            //¶àµãдÈë
            CommTxBuffer.putShort((short)rtu.reg_count);
            CommTxBuffer.put((byte)bf.capacity());
            CommTxBuffer.put(bf);
        } 
        
        int crc = Crc16.CalCRC16(CommTxBuffer, CommTxBuffer.position()) & 0xFFFF;
        CommTxBuffer.putShort((short) crc);        
        CommTxBuffer.flip();
        
        //System.out.println("·¢ËÍÊý¾Ý³¤¶È£º" + CommTxBuffer.remaining());
        
    }
    
    public void tr_Msg(ByteBuffer bbf_tx, ByteBuffer bbf_rx, boolean showdat) {
        if(++comm_tx_cnt > 32500) {
            comm_tx_cnt = 0;
        }
        if(SPCommMon.COMM_PORT_TYPE_Serial == m_COMM_PORT_TYPE) {
            m_SP_Comm.SP_Comm(bbf_tx, bbf_rx, CommSerialPort.mutycomm_Type_Samd09, dt_debug_inf, showdat);
        } else {
            m_SocketComm.SocketComm(bbf_tx, bbf_rx, dt_debug_inf, showdat);
        }
    }
    
    public void CloseCommPort() {
        CommThreadRunning = false;
    }
    
    public void setCommCmd(short cmd, int cmd_reg_addr, short[] reg_data, int reg_len) {
        if(dt_cmd_regaddr > 0) {
            return;
        }
        
        dt_cmd = cmd;
        dt_cmd_regaddr = (short) cmd_reg_addr;
        dt_cmd_regdata = reg_data;
        dt_cmd_regcnt = (short) reg_len;
    }
    
    public void setCommCmd(short cmd, int cmd_user_reg_addr, ByteBuffer bf) {
        if(dt_cmd_regaddr > 0) {
            return;
        }
        
        dt_user_cmd = cmd;                                            //ÃüÁî
        dt_user_cmd_regaddr = (short) cmd_user_reg_addr;            //¼Ä´æÆ÷µØÖ·
        
        dt_databuffer = bf;                                            //Ò£¿Ø»òÒ£µ÷Êý¾Ý
    }
    
    
    public void setCommCmdAck(int ack_t) {
        dt_cmd_ack = ack_t;
        dt_cmd_ack_time = new Date();
        //System.out.println(Com.get_DTF(dt_cmd_ack_time, Com.DTF_YMDhms) + ", " + dev_param_data_state);
        String wav_f = "6133.wav";
        if(ack_t%2 == 0) {
            wav_f = "4577.wav";
        }
        if(ack_t > 0) {
            new PlaySound(wav_f);
        }
    }
    
    private boolean processRxData(ByteBuffer bbf_rx) {
        rtu_rx.clear();
        boolean isSuccess = false;
        if(rtu_rx.putByteBuffer(bbf_rx)) {
            
            if(MyModBusRtu.CMD_TYPE_READ == rtu_rx.Cmd) {
                
                if(Power_ComBase.PWD_ACData_RegAddr_Start == rtu_rt.reg_addr) {
                    //¶ÁÈ¡¼«¼ò»¯µçÔ´Ò£²âÊý¾Ý
                    if(m_AcData.putByteBuffer(bbf_rx)) {
                        isSuccess = true;
                    }
                }else if(Power_ComBase.PWD_LIBMSData_RegAddr_Start1 <= rtu_rt.reg_addr && rtu_rt.reg_addr <= Power_ComBase.PWD_LIBMSData_RegAddr_Start4) {
                    //ï®µç³Ø°üÊý¾Ý
                    if(m_LibmsDatas[read_libms_index].putByteBuffer(bbf_rx)) {
                        
                        //System.out.println("ï®µç³Ø°ü£º"+(read_libms_index+1)+"¶ÁÈ¡³É¹¦");
                        read_libms_index ++;
                        if(read_libms_index >= Power_ComBase.LIBMSDATA_MAX_COUNT) {
                            read_libms_index = 0;
                        }
                        isSuccess = true;
                    }
                }else if(Power_ComBase.PWD_ModelData_RegAddr_Start1 <= rtu_rt.reg_addr && rtu_rt.reg_addr <= Power_ComBase.PWD_ModelData_RegAddr_Start4) {
                    //µçÔ´Ä£¿éÊý¾Ý
                    if(m_ModelDatas[read_model_index].putByteBuffer(bbf_rx)) {
                        
                        //System.out.println("µçÔ´Ä£¿é£º"+(read_model_index+1)+"¶ÁÈ¡³É¹¦");
                        read_model_index ++;
                        if(read_model_index >= Power_ComBase.LIMODEL_MAX_COUNT) {
                            read_model_index = 0;
                        }
                        isSuccess = true;
                    }
                }else if(Power_ComBase.PWD_ModelAlm_RegAddr_Start == rtu_rt.reg_addr) {
                    if(m_AlmState.putByteBuffer(bbf_rx)) {                        
                        isSuccess = true;
                    }
                }else if(Power_ComBase.PWD_SysParam_RegAddr_Start == rtu_rt.reg_addr) {
                    if(m_ParamFromDev.putByteBuffer(bbf_rx)) {
                        dt_user_cmd_ack = Power_ComBase.PWD_SysParam_Read_ACK;
                        op_result_sysparam = "¶ÁÈ¡µçԴϵͳ²ÎÊý³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                        isSuccess = true;
                    }
                }else if(Power_ComBase.PWD_PowerParam_RegAddr_Start == rtu_rt.reg_addr) {
                    //System.out.println("999");
                    if(m_ParamFromDev.putPowerByteBuffer(bbf_rx)) {
                        dt_user_cmd_ack = Power_ComBase.PWD_PwdParam_Read_ACK;
                        op_result_powerparam = "¶ÁÈ¡µçÔ´²ÎÊý³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                        isSuccess = true;
                    }
                }else if(Power_ComBase.PWD_AlmParam_RegAddr_Start == rtu_rt.reg_addr) {
                    m_AlmParamFromDev = new Power_AlmParam();
                    if(m_AlmParamFromDev.putByteBuffer(bbf_rx)) {
                        dt_user_cmd_ack = Power_ComBase.PWD_AlmParam_Read_ACK;
                        op_result_almaram = "¶ÁÈ¡µçÔ´¸æ¾¯²ÎÊý³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                        isSuccess = true;
                    }
                }else if(Power_ComBase.PWD_SchoolTime_RegAddr_Start == rtu_rt.reg_addr) {
                    if(m_ParamFromDev.putTimeByteBuffer(bbf_rx)) {
                        dt_user_cmd_ack = Power_ComBase.PWD_SchoolTime_Read_ACK;
                        op_result_jiaozhun = "¶ÁÈ¡É豸ʱ¼ä³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                        isSuccess = true;
                    }
                }else if(Power_ComBase.PWD_Load1Time_RegAddr_Start == rtu_rt.reg_addr) {
                    if(m_TimeControlFromDev.putByteBuffer1(bbf_rx)) {
                        dt_user_cmd_ack = Power_ComBase.PWD_Load1Time_Read_ACK;
                        op_result_jiaozhun = "¶ÁÈ¡1-6··Öʱ²ÎÊý³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                        isSuccess = true;
                    }
                }else if(Power_ComBase.PWD_Load7Time_RegAddr_Start == rtu_rt.reg_addr) {
                    if(m_TimeControlFromDev.putByteBuffer2(bbf_rx)) {
                        dt_user_cmd_ack = Power_ComBase.PWD_Load2Time_Read_ACK;
                        op_result_jiaozhun = "¶ÁÈ¡7-12··Öʱ²ÎÊý³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                        isSuccess = true;
                    }
                }else if(Power_ComBase.PWD_LoadControl_RegAddr_Start == rtu_rt.reg_addr) {
                    if(m_RoadStateFromDev.putByteBuffer(bbf_rx)) {
                        //System.err.println("¶ÁÈ¡·Ö·1~12·Ö·״̬³É¹¦" + m_RoadStateFromDev);
                        isSuccess = true;
                    }
                }
                
                
                
            }else if(MyModBusRtu.CMD_TYPE_WRITE_STD == rtu_rx.Cmd) {                
                if(Power_ComBase.PWD_DoorControl_RegAddr_Start == rtu_rx.reg_addr) {
                    //ÃÅ´Å¿ØÖÆ
                    if(Power_ComBase.CMD_Fail != rtu_rx.result) {
                        op_result_sysparam = "¿ØÖÆÃÅ´Å´ò¿ª³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                    }else {
                        op_result_sysparam = "¿ØÖÆÃŴŹرճɹ¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                    }
                    isSuccess = true;
                }else if(Power_ComBase.PWD_LoadControl_RegAddr_Start <= rtu_rx.reg_addr && rtu_rx.reg_addr < Power_ComBase.PWD_BackControl_RegAddr_Start) {
                    //·Ö·1~12¿ØÖÆ
                    int idx = rtu_rx.reg_addr - Power_ComBase.PWD_LoadControl_RegAddr_Start +1;
                    if(Power_ComBase.CMD_Fail != rtu_rx.result) {
                        op_result_sysparam = "¿ØÖÆ·Ö·" + idx + "´ò¿ª³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                    }else {
                        op_result_sysparam = "¿ØÖÆ·Ö·" + idx + "¹Ø±Õ³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                    }
                    isSuccess = true;
                }else if(Power_ComBase.PWD_BackControl_RegAddr_Start <= rtu_rx.reg_addr && rtu_rx.reg_addr < Power_ComBase.PWD_DoorControl_RegAddr_Start) {
                    //·Ö·1~12±¸µç¿ØÖÆ
                    int idx = rtu_rx.reg_addr - Power_ComBase.PWD_BackControl_RegAddr_Start +1;
                    if(Power_ComBase.CMD_Fail != rtu_rx.result) {
                        op_result_sysparam = "¿ØÖÆ·Ö·" + idx + "±¸µç¿ØÖÆ´ò¿ª³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                    }else {
                        op_result_sysparam = "¿ØÖÆ·Ö·" + idx + "±¸µç¿ØÖƹرÕʧ°Ü" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                    }
                    isSuccess = true;
                }
                
                
            }else if(MyModBusRtu.CMD_TYPE_WRITE_MULTY == rtu_rx.Cmd) {
                if(Power_ComBase.PWD_PowerParam_RegAddr_Start == rtu_rx.reg_addr) {
                    //ÉèÖõçÔ´²ÎÊý
                    if(rtu_rx.result == Power_Param.REG_Count_Power) {
                        op_result_powerparam = "ÉèÖõçÔ´²ÎÊý³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                        isSuccess = true;
                    }else {
                        op_result_powerparam = "ÉèÖõçÔ´²ÎÊýʧ°Ü" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                    }
                }else if(Power_ComBase.PWD_AlmParam_RegAddr_Start == rtu_rx.reg_addr) {
                    //ÉèÖõçÔ´¸æ¾¯²ÎÊý
                    if(rtu_rx.result == Power_AlmParam.REG_Count) {
                        op_result_almaram = "ÉèÖõçÔ´¸æ¾¯²ÎÊý³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                        isSuccess = true;
                    }else {
                        op_result_almaram = "ÉèÖõçÔ´¸æ¾¯²ÎÊýʧ°Ü" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                    }
                }else if(Power_ComBase.PWD_SysParam_RegAddr_Start == rtu_rx.reg_addr) {
                    //ÉèÖõçԴϵͳ²ÎÊý
                    if(rtu_rx.result == Power_Param.REG_Count) {
                        op_result_sysparam = "ÉèÖõçԴϵͳ²ÎÊý³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                        isSuccess = true;
                    }else {
                        op_result_sysparam = "ÉèÖõçԴϵͳ²ÎÊýʧ°Ü" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                    }
                }else if(Power_ComBase.PWD_SchoolTime_RegAddr_Start == rtu_rx.reg_addr) {
                    //ÉèÖõçԴУʱ
                    if(rtu_rx.result == Power_Param.REG_Count_Time) {
                        op_result_jiaozhun = "ÉèÖõçԴУʱ³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                        isSuccess = true;
                    }else {
                        op_result_jiaozhun = "ÉèÖõçԴУʱʧ°Ü" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                    }
                }else if(Power_ComBase.PWD_Load1Time_RegAddr_Start == rtu_rx.reg_addr) {
                    //ÉèÖ÷Ö·1-6·Öʱ¿ØÖÆ
                    if(rtu_rx.result == Power_TimeControl.REG_Count) {
                        op_result_jiaozhun = "ÉèÖ÷Ö·1-6·Öʱ²ÎÊý³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                        isSuccess = true;
                    }else {
                        op_result_jiaozhun = "ÉèÖ÷Ö·1-6·Öʱ²ÎÊýʧ°Ü" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                    }
                }else if(Power_ComBase.PWD_Load7Time_RegAddr_Start == rtu_rx.reg_addr) {
                    //ÉèÖ÷Ö·7-12·Öʱ¿ØÖÆ
                    if(rtu_rx.result == Power_TimeControl.REG_Count) {
                        op_result_jiaozhun = "ÉèÖ÷Ö·7-12·Öʱ²ÎÊý³É¹¦" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                        isSuccess = true;
                    }else {
                        op_result_jiaozhun = "ÉèÖ÷Ö·7-12·Öʱ²ÎÊýʧ°Ü" + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                    }
                }
            } else if(MyModBusRtu.CMD_TYPE_WRITE_SysUpdate == rtu_rx.Cmd) {
                if(Power_ComBase.PWD_SysReStart_RegAddr_Start == rtu_rx.reg_addr) {
                    //Ô¶³ÌÖØÆôÉ豸
                    if(rtu_rx.result == 0xBBBB) {
                        //É豸Զ³ÌÖØÆô³É¹¦
                        dt_user_cmd_ack = Power_ComBase.PWD_SysRestart_Success_ACK;                        
                        isSuccess = true;
                    }else {
                        //É豸Զ³ÌÖØÆôʧ°Ü
                        dt_user_cmd_ack = Power_ComBase.PWD_SysRestart_Fail_ACK;
                    }
                }else if(Power_ComBase.PWD_TransUpdateFile_RegAddr_Start == rtu_rx.reg_addr) {
                    if(rtu_rx.result != 0) {
                        isSuccess = true;
                    }
                }else if(Power_ComBase.PWD_StartUpdate_RegAddr_Start == rtu_rx.reg_addr) {
                    if(rtu_rx.result != 0) {
                        isSuccess = true;                        
                        appendUppdateInfo("System Update Success ... " + Com.get_DTF(new Date(), Com.DTF_YMDhms) + "\n");
                    }else {
                        appendUppdateInfo("System Update Fail ... " + Com.get_DTF(new Date(), Com.DTF_YMDhms) + "\n");
                    }
                }
            } else if(MyModBusRtu.CMD_TYPE_Read_UpdateFile == rtu_rx.Cmd) {
                if(Power_ComBase.PWD_TransUpdateFile_RegAddr_Start == rtu_rt.reg_addr) {
                    if(bbf_rx.remaining() > 2) {
                        isSuccess = true;
                    }else {
                        System.err.println("·µ»ØÊý¾Ý³¤¶È²»×ã");
                    }
                }
            }        
        }
        if(bbf_rx.limit() > 0 && isSuccess) {
            if(++comm_rx_cnt > 32500) {
                comm_rx_cnt = 0;
            }
        }
        return isSuccess;
    }
    
    public void setAutoStartRestTest(String filename, int type, boolean en) {
        AutoSaveDataFileName = filename;
        AutoStartResTestEn = en;
        AutoSaveResDataCount = 0;
    }
    
    private void closeSPCommPort() {
        if(SPCommMon.COMM_PORT_TYPE_Serial == m_COMM_PORT_TYPE) {
            m_SP_Comm.serialPort.close();
        } else {
            m_SocketComm.socketClose();
        }
    }
    
    public void run() {
        while(true == CommThreadRunning) {
            try {
                Thread.sleep(200);
                if(dt_dfu_en == false) {
                    makeCommTxData();
                    tr_Msg(CommTxBuffer, CommRxBuffer, true);
                    processRxData(CommRxBuffer);
                    
                    
                } else {
                    //--------------------------- BOOTLOADER_CMD_WRITE-----------------------------//
                    
                    dt_show_msg.append("---------------------------------------------------\n");
                    String dfu_text_inf = "Power System Update Start." + Com.get_DTF(new Date(), Com.DTF_YMDhms) + "\n";
                    appendUppdateInfo(dfu_text_inf);
                    boolean prog_ok = false;
                    FileInputStream fis = null;
                    try {
                        File f = new File(dt_dfuFileName);
                        long file_len = f.length();
                        byte[] buf_to_flash = new byte[Power_ComBase.DFU_BUF_LEN];
                        
                        Thread.sleep(100);
                        fis = new FileInputStream(f);
                        int dfu_datablock_num = 1;
                        int percent = 0;
                        while(true)
                        {
                            //====================================//
                            if((false == dt_dfu_en) || (false == CommThreadRunning)) {
                                dfu_text_inf = "DFU Manual Stop . . .\n";
                                appendUppdateInfo(dfu_text_inf);
                                break;
                            }
                            //====================================//
                            for(int n=0; n<buf_to_flash.length; n++) {
                                buf_to_flash[n] = (byte) 0xFF;
                            }
                            //====================================//
                            int data_len_towrite = fis.read(buf_to_flash);
                            if(data_len_towrite > 0) {
                                Thread.sleep(1);
                                for(int cnt_t=0; cnt_t<3; cnt_t++) {                            
                                    
                                    //¹¹Ôì·¢ËÍÊý¾Ý
                                    makeSendFileData(buf_to_flash,dfu_datablock_num);
                                    dt_user_cmd = MyModBusRtu.CMD_TYPE_WRITE_SysUpdate;
                                    dt_user_cmd_regaddr = Power_ComBase.PWD_TransUpdateFile_RegAddr_Start;
                                    
                                    makeCommTxData();
                                    tr_Msg(CommTxBuffer, CommRxBuffer, true);
                                    
                                    if(true == processRxData(CommRxBuffer)) {
                                        if(rtu_rx.result == dfu_datablock_num) {
                                            prog_ok = true;
                                            break;
                                        }
                                    } else {
                                        prog_ok = false;
                                        Thread.sleep(500);
                                    }
                                }
                                if(false == prog_ok) {
                                    dfu_text_inf = "DFU Write Error!\n";
                                    appendUppdateInfo(dfu_text_inf);
                                    break;
                                } else {
                                    dfu_datablock_num += 1;
                                }
                                
                                int tran_len_t = dfu_datablock_num*FBS9100_DFU.DFU_BUF_LEN;
                                if(percent < (tran_len_t*100)/file_len) {
                                    percent = (int) ((tran_len_t*100)/file_len);
                                    if(percent > 100) {
                                        percent = 100;
                                    }
                                    appendUppdateInfo(String.format("DFU Write: %d%% Done.\n", percent));
                                }
                            } else {
                                dfu_text_inf = "DFU Write: 100% Done!\n";
                                appendUppdateInfo(dfu_text_inf);
                                //System.out.println(dfu_text_inf);
                                break;
                            }
                        }
                    } catch (IOException | InterruptedException e1) {
                        e1.printStackTrace();
                    } finally {
                        try {
                            fis.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                    
                    if((true == prog_ok) && (true == dt_dfu_en) && (true == CommThreadRunning)) {
                        boolean check_ok = false;
                        FileInputStream fis_ck = null;
                        try {
                            File f_ck = new File(dt_dfuFileName);
                            long file_len_ck = f_ck.length();
                            byte[] buf_check = new byte[Power_ComBase.DFU_BUF_LEN];
                            
                            Thread.sleep(100);
                            fis_ck = new FileInputStream(f_ck);
                            int check_datablock_num = 1;
                            int percent = 0;
                            while(true)
                            {
                                //====================================//
                                if((false == dt_dfu_en) || (false == CommThreadRunning)) {
                                    dfu_text_inf = "DFU Check Manual Stop . . .\n";
                                    appendUppdateInfo(dfu_text_inf);
                                    break;
                                }
                                //====================================//
                                for(int n=0; n<buf_check.length; n++) {
                                    buf_check[n] = (byte) 0xFF;
                                }
                                //====================================//
                                int len_buf_check = fis_ck.read(buf_check);
                                if(len_buf_check > 0) {
                                    Thread.sleep(1);
                                    for(int cnt_t=0; cnt_t<3; cnt_t++) {
                                        
                                        //¹¹Ôì·¢ËÍÊý¾Ý
                                        dt_databuffer = power_util.createSysUpdateControlByteBuffer(check_datablock_num);
                                        dt_user_cmd = MyModBusRtu.CMD_TYPE_Read_UpdateFile;
                                        dt_user_cmd_regaddr = Power_ComBase.PWD_TransUpdateFile_RegAddr_Start;                                        
                                        
                                        makeCommTxData();
                                        tr_Msg(CommTxBuffer, CommRxBuffer, true);
                                        if(true == processRxData(CommRxBuffer)) {
                                            if(rtu_rx.result == check_datablock_num) {
                                                if(true == checkDfuReadAckBuf(CommRxBuffer, buf_check)) {
                                                    check_ok = true;
                                                } else {
                                                    check_ok = false;
                                                    Thread.sleep(500);
                                                }
                                                if(true == check_ok) {
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    if(false == check_ok) {
                                        dfu_text_inf = "DFU File Check Error!\n ";
                                        appendUppdateInfo(dfu_text_inf);
                                        break;
                                    } else {
                                        check_datablock_num += 1;
                                    }
                                    
                                    int tran_len_t = check_datablock_num*FBS9100_DFU.DFU_BUF_LEN;
                                    if(percent < (tran_len_t*100)/file_len_ck) {
                                        percent = (int) ((tran_len_t*100)/file_len_ck);
                                        if(percent > 100) {
                                            percent = 100;
                                        }
                                        appendUppdateInfo(String.format("DFU File Check: %d%% Done.\n", percent));
                                    }
                                } else {
                                    dfu_text_inf = "DFU File Check: 100% Done!\n";
                                    appendUppdateInfo(dfu_text_inf);
                                    
                                    setCommCmd(MyModBusRtu.CMD_TYPE_WRITE_SysUpdate, Power_ComBase.PWD_StartUpdate_RegAddr_Start, power_util.createSysUpdateControlByteBuffer(0xDFDF));
                                    //setCommCmd(FBS9100_ComBase.CMD_SystemUpdate, ByteBuffer.allocate(0));
                                    break;
                                }
                            }
                        } catch (IOException | InterruptedException e1) {
                            e1.printStackTrace();
                        } finally {
                            try {
                                fis_ck.close();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }
                    //---------------------------------------------------------------------------------//
                    dt_dfu_en = false;
                    //---------------------------------------------------------------------------------//
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                CloseCommPort();
                JOptionPane.showMessageDialog(null, "ZDHJ CommPort ´®¿ÚÒì³£: " + e.getMessage());
            }
        }
        /******************************************/
        closeSPCommPort();
        /******************************************/
        dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) 
                                + " ZDHJ CommPort is quit,"
                                + " COMM_PORT_TYPE: " + m_COMM_PORT_TYPE + "\n");
        /******************************************/
    }
    
    
    public void makeSendFileData(byte[] data,int block_num) {
        dt_databuffer = ByteBuffer.allocate(data.length + 2);
        dt_databuffer.order(ByteOrder.BIG_ENDIAN);
        dt_databuffer.putShort(FBS9100_ComBase.changeIntToShort(block_num));
        dt_databuffer.put(data);
        dt_databuffer.flip();
    }
    
    public void setCommCmd_DFU(boolean dfu_en, String dfu_file, JTextArea show_area) {
        dt_dfu_en = dfu_en;
        dt_show_msg = show_area;
        
        dt_dfuFileName = dfu_file;
    }
    
    
    public boolean checkDfuReadAckBuf(ByteBuffer bf,byte[] data) {
        boolean  check_ok = false;
        if(data.length <= bf.remaining()) {
            check_ok = true;
            for(int k = 0 ;k< data.length;k++) {
                if(data[k] != bf.get()) {
                    System.err.println("УÑé×Ö½Ú£º" + k + "ʧ°Ü");
                    check_ok = false;
                    break;
                }
            }
        }else {
            //System.err.println("bf£º"+ComFn.bytesToHexString(bf.array(), bf.array().length));
            //System.err.println("data£º"+ComFn.bytesToHexString(data, data.length));
            //System.err.println("data.length£º" + data.length + "\tbf.remaining():"+bf.remaining());
        }    
        return check_ok;
    }
    
    public void appendUppdateInfo(String msg) {        
        StringBuffer buffer = new StringBuffer(dt_show_msg.getText());
        dt_show_msg.setText(msg);
        dt_show_msg.append(buffer.toString());
    }
}