mxpopstar
2022-05-03 e75ef5f04f61aa5fbd89fd5c413dcee1819b7a91
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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
package com.dev.fbs9600_mon;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
 
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.CRC32;
 
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
 
import sp_comm.CommSerialPort;
 
import com.Com;
import com.ComFn;
import com.Crc16;
import com.Ecb_Aes;
import com.PlaySound;
import com.app.reg.RegistCoade;
 
public class SPCommMon_MSP extends SPCommMon {
    public static final short CMD_TYPE_READ = 0x03;
    public static final short CMD_TYPE_WRITE = 0x05;
    
    public static final short UART_MonomerSetJunHengVol_RegAddr = 0x0008;
    public static final short UART_MonomerClrTmpAddr_RegAddr = 0x0009;
    public static final short UART_MonomerLowSign_RegAddr = 0x000A;
    public static final short UART_MonomerSetAddr_RegAddr = 0x000B;
    public static final short UART_MonomerResTest_RegAddr = 0x000C;
    public static final short UART_MonomerAdjVol_RegAddr  = 0x000D;
    public static final short UART_MonomerAdjTemp_RegAddr = 0x000E;
    public static final short UART_MonomerAdjRes_RegAddr  = 0x000F;
    public static final short UART_MonomerAdjOffSet_RegAddr = 0x0010;    //Æ«ÒÆÁ¿
    public static final short UART_MonomerAdjReset_RegAddr = 0x0011;    //¸´Î»
    public static final short UART_MonomerDFU_RegAddr = 0x0012;            //Æô¶¯dfu
    
    public static final byte CMD_FBS9100_WriteDFU = (byte) 0x86;
    public static final byte CMD_FBS9100_ReadDFU = (byte) 0x87;
    
    public static final byte BOOTLOADER_CMD_QUIT = 0;
    public static final byte BOOTLOADER_CMD_GETINF = 1;
    public static final byte BOOTLOADER_CMD_WRITE = 2;
    public static final byte BOOTLOADER_CMD_READ = 3;
    
    public static final int BOOTLOADER_BUF_LEN_128 = 128;
    public static final int BOOTLOADER_BUF_LEN_256 = 256;
    private static int BOOTLOADER_BUF_LEN = BOOTLOADER_BUF_LEN_256;
    private int BOOT_APP_ADDR = 0x9000;//0xD000;
    private int BOOT_APPVECTOR_ADDR = 0xFBDC;//0xFDE0;
    private String BOOT_APPVECTOR_tag = "@fb";//0xFDE0;
    private String BOOT_APPVECTOR_tagfull = "@fbe0";//0xFDE0;
 
    // ¼ì²âϵͳÖпÉÓõÄͨѶ¶Ë¿ÚÀà
    private static CommPortIdentifier portId;
    private static Enumeration<CommPortIdentifier> portList;
    // ÊäÈëÊä³öÁ÷
    public static InputStream inputStream;
    public static OutputStream outputStream;
    // RS-232µÄ´®ÐпÚ
    public static SerialPort serialPort;
    
    private static boolean CommThreadRunning = false;
    private ByteBuffer CommRxBuffer = ByteBuffer.allocate(1500); 
    private ByteBuffer CommTxBuffer = ByteBuffer.allocate(1048);
    
    private ByteBuffer DFUCommRxBuffer = ByteBuffer.allocate(1048); 
    private ByteBuffer DFUCommTxBuffer = ByteBuffer.allocate(1048);
    
    private ByteBuffer FBS9100TxBuffer = ByteBuffer.allocate(1048);
    private ByteBuffer FBS9100RxBuffer = ByteBuffer.allocate(1500);
    /*
    public short comm_OK_cnt = 0;
    public short comm_Err_cnt = 0;
    public boolean dt_new_tag = false;
    */
    private int comm_bautrate = 9600;
    /*
    public int dt_target_addr = 0;
    public short dt_dfu_addr_start = 0;
    public short dt_dfu_addr_end = 0;
    */
    /*
    public short dt_addr = 0;
    public short dt_junhengcurr = 0;
    public short dt_vol = 0;
    public short dt_tmp = 0;
    public short dt_abn_curr = 0;
    public int dt_res = 0;
    public int dt_res_conn = 0;
    public short dt_version = 0;
    public short dt_voltype = -1;
    public int dt_rescurr = 0;
    public int dt_res_asyscode = 0;
    public int dt_lockpage_count = 0;
    public int dt_ssb = 0;
    public short mAddrSettedNow = 0;
    public int mBianHao_TestRes = -1;
    */
    private short dt_cmd = CMD_TYPE_READ;
    private short dt_cmd_regaddr = 0;
    private short dt_cmd_regcnt = 0;
    private short dt_cmd_data = 0;
    private short dt_cmd_data1 = 0;
    
    private boolean dt_normal_comm_en = true;
    private boolean dt_update_en = false;
    private String dt_update_file = "";
    int dt_dfu_FirmVersion = 20400;
    private int dt_dfu_bitrate = 9600;
    private JTextArea dt_show_msg = null;
    /*
    public String dt_set_addr_res_inf = "";
    public char[] dt_dfu_pwd = new char[6];
    public Date dt_app_start_time = new Date();
    public char[] dt_dfu_pwd_auto = new char[6];
    */
    private String dt_dfu_pwd_sn = "";
    private int dt_multy_comm_type = CommSerialPort.mutycomm_Type_Msp430;
    
    public SPCommMon_MSP(int addr_t, int bitrate, int dfu_buf_len, int multy_com_type, JTextArea show_area) {
        super(addr_t, bitrate, dfu_buf_len, multy_com_type, show_area, null, null);
        // TODO Auto-generated constructor stub
        dt_target_addr = addr_t & 0xFFFF;
        comm_bautrate = bitrate;
        dt_multy_comm_type = multy_com_type;
        dt_show_msg = show_area;
        
        BOOTLOADER_BUF_LEN = dfu_buf_len;
        
        //-------------------------------------------------------------//
        dt_dfu_pwd_sn = RegistCoade.getRegistSerialCoade("amtb_amtb_amtb", "37373737");
        CRC32 crc = new CRC32();
        crc.update((dt_dfu_pwd_sn + "ÄÏÎÞ°¢ÃÖÍÓ·ð").getBytes());
        char[] tmp_char = String.format("%06d", Math.abs(crc.getValue())%1000000).toCharArray();
        for(int n=0; n<dt_dfu_pwd_auto.length; n++) {
            dt_dfu_pwd_auto[n] = tmp_char[n];
        }
        //-------------------------------------------------------------//
        dt_show_msg.setText(dt_dfu_pwd_sn);
    }
    
    public void setCommAddr(int addr_t) {
        dt_target_addr = addr_t & 0xFFFF;
    }
    
    public void setDfuCommAddr(int addr_start_t, int addr_end_t) {
        dt_dfu_addr_start = (short) (addr_start_t & 0xFFFF);
        setCommAddr(dt_dfu_addr_start);
        dt_dfu_addr_end = (short) (addr_end_t & 0xFFFF);
    }
    
    public static void searchCommPort(JComboBox<String> cb_comm) {
        cb_comm.removeAllItems();
        @SuppressWarnings("unchecked")
        Enumeration<CommPortIdentifier> pl = CommPortIdentifier.getPortIdentifiers();
        while (pl.hasMoreElements()) {
            CommPortIdentifier port_id = pl.nextElement();
            if (port_id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                cb_comm.addItem(port_id.getName());
            }
        }
    }
    
    public void setCommBautrate(int bt_rate) {
        try {
            serialPort.setSerialPortParams(bt_rate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public String getBianHaoTest_Str() {
        String str = "δ²âÊÔ";
        
        if(1 == mBianHao_TestRes) {
            str = "À­µÍ¹ÊÕÏ";
        } else if(2 == mBianHao_TestRes) {
            str = "À­¸ß¹ÊÕÏ";
        } if(0 == mBianHao_TestRes) {
            str = "²âÊÔÕý³£";
        }
        
        return str;
    }
    
    //³õʼ»¯´®¿Ú
    @SuppressWarnings("unchecked")
    public boolean OpenCommPort(String comm_name) {
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            portId = portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (portId.getName().equals(comm_name)) {
                    try {
                        serialPort = (SerialPort) portId.open("SerialPort-Test", 2000);
                        //serialPort.addEventListener(new SPComm());
                        serialPort.notifyOnDataAvailable(true);
                        // ÉèÖô®¿ÚͨѶ²ÎÊý//
                        serialPort.setSerialPortParams(comm_bautrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                        
                        outputStream = serialPort.getOutputStream();
                        inputStream = serialPort.getInputStream();
                    } catch (PortInUseException e) {
                        e.printStackTrace();
                    } catch (UnsupportedCommOperationException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } 
                }
            }
        }
        if(null == serialPort) {
            System.out.println(comm_name + "²»´æÔÚ»òÕýÔÚʹÓÃÖÐ! MSP!");
            return false;
        } else {
            System.out.println(comm_name + "´ò¿ª´®¿Ú³É¹¦! MSP!");
            CommTxBuffer.order(ByteOrder.BIG_ENDIAN);
            CommRxBuffer.order(ByteOrder.BIG_ENDIAN);
            DFUCommTxBuffer.order(ByteOrder.LITTLE_ENDIAN);
            DFUCommRxBuffer.order(ByteOrder.LITTLE_ENDIAN);
            FBS9100TxBuffer.order(ByteOrder.LITTLE_ENDIAN);
            FBS9100RxBuffer.order(ByteOrder.LITTLE_ENDIAN);
            comm_tx_cnt = 0;
            comm_rx_cnt = 0;
            CommThreadRunning = true;
            return true;
        }
    }
    
    private void makeCommTxData() {
        CommTxBuffer.clear();
        
        if(CMD_TYPE_READ == dt_cmd) {
            CommTxBuffer.put((byte) dt_target_addr);
            CommTxBuffer.put((byte) 0x03);
            CommTxBuffer.put((byte) 0x00);
            CommTxBuffer.put((byte) 0x00);
            CommTxBuffer.put((byte) 0x00);
            CommTxBuffer.put((byte) 0x16);
        } else {
            CommTxBuffer.put((byte) dt_target_addr);
            CommTxBuffer.put((byte) 0x05);
            CommTxBuffer.put((byte) (dt_cmd_regaddr>>8));
            CommTxBuffer.put((byte) dt_cmd_regaddr);
            CommTxBuffer.put((byte) (dt_cmd_regcnt>>8));
            CommTxBuffer.put((byte) dt_cmd_regcnt);
            CommTxBuffer.put((byte) (dt_cmd_data>>8));
            CommTxBuffer.put((byte) dt_cmd_data);
            CommTxBuffer.put((byte) (dt_cmd_data1>>8));
            CommTxBuffer.put((byte) dt_cmd_data1);
            dt_cmd = CMD_TYPE_READ;
        }
        
        int crc = Crc16.CalCRC16(CommTxBuffer, CommTxBuffer.position()) & 0xFFFF;
        CommTxBuffer.putShort((short) (crc>>8 | crc<<8));
        CommTxBuffer.flip();
    }
    
    private void makeCommTxDataNull() {
        CommTxBuffer.clear();
        CommTxBuffer.put((byte) 0xFF);
        CommTxBuffer.put((byte) 0x00);
        CommTxBuffer.put((byte) (0));
        CommTxBuffer.put((byte) 0);
        CommTxBuffer.put((byte) (0));
        CommTxBuffer.put((byte) 0);
        CommTxBuffer.put((byte) 0);
        CommTxBuffer.put((byte) 0);
        
        int crc = Crc16.CalCRC16(CommTxBuffer, CommTxBuffer.position()) & 0xFFFF;
        CommTxBuffer.putShort((short) (crc>>8 | crc<<8));
        CommTxBuffer.flip();
    }
    
    //Ïò´®¿Ú·¢ËÍÐÅÏ¢·½·¨
    public void sendMsg(ByteBuffer bbf_tx) throws IOException {
        byte[] bt_addr = new byte[1];
        if(CommSerialPort.mutycomm_Type_Msp430 == dt_multy_comm_type) {
            bt_addr[0] = bbf_tx.get();
        }
        
        byte[] bt_dat = new byte[bbf_tx.remaining()];
        for(int n=0; n<bt_dat.length; n++) {
            bt_dat[n] = bbf_tx.get();
        }
        
        System.out.println("TX:" + ComFn.bytesToHexString(bt_addr, bt_addr.length)
                            + ComFn.bytesToHexString(bt_dat, bt_dat.length));
        
        if(CommSerialPort.mutycomm_Type_Msp430 == dt_multy_comm_type) {
            try {
                serialPort.setSerialPortParams(comm_bautrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_MARK);
            } catch (UnsupportedCommOperationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            outputStream.write(bt_addr);
            outputStream.flush();
            try {
                Thread.sleep(2);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            
            try {
                serialPort.setSerialPortParams(comm_bautrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_SPACE);
            } catch (UnsupportedCommOperationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        outputStream.write(bt_dat);
        outputStream.flush();
        
        if(++comm_tx_cnt > 32500) {
            comm_tx_cnt = 0;
        }
        //System.out.println(comm_tx_cnt);
    }
    
    private boolean checkCommBuf(ByteBuffer buf_t) {
        boolean res = false;
        if(buf_t.limit() >= 5) {
            buf_t.position(buf_t.limit()-2);
            int crc = buf_t.getShort() & 0xFFFF;
            crc = ((crc>>8) | (crc<<8)) & 0xFFFF;
            int crc_cal = Crc16.CalCRC16(buf_t, buf_t.limit()-2) & 0xFFFF;
            
            if(crc_cal == crc) {
                res = true;
            }
            
        } else {
            res = false;
        }
        
        return res;
    }
    
    public void readMsg(ByteBuffer bbf_rx) throws IOException, InterruptedException {
        bbf_rx.clear();
        int time_out = 0;
        byte[] rx_buf_t = new byte[560];
        while(true) {
            if(inputStream.available() > 0) {
                time_out = 0;
                int rx_cnt_t = inputStream.read(rx_buf_t);
                if((bbf_rx.position()+rx_cnt_t) < (bbf_rx.capacity()-1)) {
                    bbf_rx.put(rx_buf_t, 0, rx_cnt_t);
                } else {
                    break;
                }
            } else {
                Thread.sleep(5);
                time_out++;
                if(0 == bbf_rx.position()) {
                    if(time_out > 200) {
                        break;
                    }
                } else {
                    if(time_out >= 10) {
                        break;
                    }
                }
            }
        }
        bbf_rx.flip();
    }
    
    private ByteBuffer makeDfuBuf(ByteBuffer buf_t, byte cmd, int prog_addr, byte[] bt_data) {
        buf_t.order(ByteOrder.LITTLE_ENDIAN);
        buf_t.clear();
        //---------------------------------------------------//
        //-------------- BOOTLOADER_CMD_GETINF --------------//
        buf_t.put((byte) dt_target_addr);
        for(int n=0; n<3; n++) {
            buf_t.put((byte) 0xAA);
        }
        buf_t.put(cmd);
        buf_t.put((byte) 0x00);
        buf_t.putShort((short) prog_addr);
        buf_t.putShort((short) 0);
        
        if(BOOTLOADER_CMD_WRITE == cmd) {
            for(int n=0; n<BOOTLOADER_BUF_LEN; n++) {
                buf_t.put(bt_data[n]);
            }
        }
        
        int pos = buf_t.position();
        int crc = Crc16.CalCRC16(buf_t, pos) & 0xFFFF;
        buf_t.putShort(8, (short) (crc));
        buf_t.position(pos);
        buf_t.flip();
        
        return buf_t;
    }
    
    private boolean checkDfuBuf(ByteBuffer buf_t, byte cmd, int prog_addr) {
        boolean res = true;
        if(buf_t.limit() >= 10) {
            buf_t.position(0);
            int dev_addr = buf_t.get() & 0xFF;
            //buf_t.position(3);
            for(int n=0; n<3; n++) {
                buf_t.get();
            }
            byte dfu_cmd = buf_t.get();
            byte dfu_res = buf_t.get();
            int p_addr = buf_t.getShort()& 0xFFFF;
            int crc = buf_t.getShort() & 0xFFFF;
            buf_t.position(8);
            buf_t.putShort((short) 0);
            int crc_source = Crc16.CalCRC16(buf_t, buf_t.limit()) & 0xFFFF;
            /*************************************************/
            if(crc_source != crc) {
                res = false;
            }
            if(dev_addr != dt_target_addr) {
                res = false;
            }
            if(dfu_cmd != cmd) {
                res = false;
            }
            if(0 != dfu_res ) {
                res = false;
            }
            if((BOOTLOADER_CMD_GETINF!=cmd) && (BOOTLOADER_CMD_QUIT!=cmd) && (p_addr!=prog_addr)) {
                res = false;
            }
            if(true == res) {
                if(BOOTLOADER_CMD_GETINF == cmd) {
                    dt_dfu_FirmVersion = p_addr;
                }
            }
            /*************************************************/
        } else {
            res = false;
        }
        
        return res;
    }
    
    public void exitCommPortThread() {
        CommThreadRunning = false;
    }
    
    public void setCommCmd(short cmd, short cmd_reg_addr, short cmd_reg_cnt, short cmd_data, short cmd_data1) {
        dt_cmd = cmd;
        dt_cmd_regaddr = cmd_reg_addr;
        dt_cmd_regcnt = cmd_reg_cnt;
        dt_cmd_data = cmd_data;
        dt_cmd_data1 = cmd_data1;
        if(UART_MonomerLowSign_RegAddr == dt_cmd_regaddr) {
            mBianHao_TestRes = -1;
        }
    }
    
    public void setCommCmd_gb(short cmd, short cmd_reg_addr, short cmd_reg_cnt, 
                                short cmd_data, short cmd_data1, boolean guangbo_t) {
        dt_cmd = cmd;
        dt_cmd_regaddr = cmd_reg_addr;
        dt_cmd_regcnt = cmd_reg_cnt;
        dt_cmd_data = cmd_data;
        dt_cmd_data1 = cmd_data1;
        
        if(UART_MonomerLowSign_RegAddr == dt_cmd_regaddr) {
            mBianHao_TestRes = -1;
        }
        
        System.out.println("msp: dt_cmd_guangbo, cmd:" + dt_cmd);
    }
    
    public void clearResDataBuf() {
        dt_res = -1;
        dt_rescurr = -1;
        dt_res_asyscode = -1;
    }
    
    public void setUpdateCmd(boolean update_en, boolean read_ckeck_en, String filepath, int dfu_bitrate) {
        dt_update_en = update_en;
        dt_update_file = filepath;
        dt_dfu_bitrate = dfu_bitrate;
    }
    
    public void setNormalCommState(boolean stat) {
        dt_normal_comm_en = stat;
    }
    
    private void processRxData(ByteBuffer bbf_rx, boolean check_en) {
        if(false == checkCommBuf(bbf_rx)) {
            if(true == check_en) {
                comm_OK_cnt = 0;
            }
            //if(comm_Err_cnt < 1000) {
            //    comm_Err_cnt += 1;
            //}
            
            if((comm_OK_cnt<1) && (0==dt_res)) {
                dt_new_tag = false;
            }
            
            return;
        }
        
        if(bbf_rx.limit() > 16) {
            bbf_rx.position(0);
            dt_addr = bbf_rx.get();
            bbf_rx.get();
            bbf_rx.get();
            int tmp_val = bbf_rx.getShort()&0xFFFF;
            if((tmp_val & 0x8000) > 0) {
                dt_junhengcurr = (short) ((short)(tmp_val&(~0x8000)) * (-1));
            } else {
                dt_junhengcurr = (short)(tmp_val);
            }
            dt_vol = bbf_rx.getShort();
            dt_tmp = bbf_rx.getShort();
            dt_abn_curr = bbf_rx.getShort();
            dt_res = (int) (bbf_rx.getShort()&0xFFFF);
            dt_addr = bbf_rx.getShort();
            dt_version = bbf_rx.getShort();
            
            short vol_type = bbf_rx.getShort();
            mBianHaoPIN_Low = ((vol_type&(1<<8)) > 0);
            dt_voltype = (short) (vol_type & 0x7F);
            //System.out.println("dt_voltype:" + dt_voltype);
            
            dt_rescurr = (int)(bbf_rx.getShort()&0xFFFF);
            if(bbf_rx.hasRemaining()) {
                dt_res_asyscode = (int)(bbf_rx.getShort()&0xFFFF);
            }
            
            if(bbf_rx.hasRemaining()) {
                dt_res_conn = (int) (bbf_rx.getShort()&0xFFFF);
            }
            if(bbf_rx.hasRemaining()) {
                dt_lockpage_count = (int)(bbf_rx.getShort()&0xFFFF);
            }
            if(bbf_rx.hasRemaining()) {
                dt_ssb = (int)(bbf_rx.getShort()&0xFFFF);
            }
            
            if(++comm_rx_cnt > 32500) {
                comm_rx_cnt = 0;
            }
            if(comm_OK_cnt < 1000) {
                comm_OK_cnt += 1;
            }
            //System.out.println("comm_OK_cnt: " + comm_OK_cnt);
            if((comm_OK_cnt > 3) && (comm_OK_cnt < 5)) {
                if(0 == dt_res) {
                    dt_new_tag = true;
                    mBianHao_TestRes = -1;
                } else {
                    dt_new_tag = false;
                }
            }
        }
    }
    
    public void run() {
        int null_comm_cmd_cnt = 0;
        while(true == CommThreadRunning) {
            
            if(false == dt_update_en) {
                try {
                    if(false == dt_normal_comm_en) {
                        Thread.sleep(200);
                        continue;
                    }
                    
                    boolean cmd_set_addr = ((CMD_TYPE_WRITE==dt_cmd) && (UART_MonomerSetAddr_RegAddr==dt_cmd_regaddr));
                    boolean cmd_Set_bianhao_Low = ((CMD_TYPE_WRITE==dt_cmd) && (UART_MonomerLowSign_RegAddr==dt_cmd_regaddr));
                    boolean cmd_Set_bianhao_High = ((CMD_TYPE_WRITE==dt_cmd) && (UART_MonomerClrTmpAddr_RegAddr==dt_cmd_regaddr));
                    
                    if(true == dt_update_en) {
                        continue;
                    }
                    if(null_comm_cmd_cnt < 30) {
                        null_comm_cmd_cnt++;
                    }
                    if(null_comm_cmd_cnt < 2) {
                        //makeCommTxDataNull();
                        makeCommTxData();
                    } else {
                        makeCommTxData();
                    }
                    sendMsg(CommTxBuffer);
                    readMsg(CommRxBuffer);
                    
                    byte[] bt_t_rx = new byte[CommRxBuffer.limit()];
                    CommRxBuffer.get(bt_t_rx);
                    System.out.println("RX:"+ComFn.bytesToHexString(bt_t_rx, bt_t_rx.length));
                    
                    processRxData(CommRxBuffer, (false==cmd_Set_bianhao_Low)&&(false==cmd_Set_bianhao_High)&&(false==cmd_set_addr));
                    Thread.sleep(300);
                    
                    if(true == cmd_set_addr) {
                        makeCommTxData();
                        sendMsg(CommTxBuffer);
                        readMsg(CommRxBuffer);
                        processRxData(CommRxBuffer, true);
                        
                        String wav_f = "6133.wav";
                        dt_set_addr_res_inf = "ÉèÖñàºÅΪ" + dt_addr + "³É¹¦  @ " + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                        if(dt_addr != (mAddrSettedNow+1)) {
                            wav_f = "4577.wav";
                            dt_set_addr_res_inf = "ÉèÖñàºÅΪ" + (mAddrSettedNow+1) + "ʧ°Ü!!!!!!! @ " 
                                                    + Com.get_DTF(new Date(), Com.DTF_YMDhms);
                        }
                        mAddrSettedNow = dt_addr;
                        new PlaySound(wav_f);
                    }
                    
                    if(true == cmd_Set_bianhao_Low) {
                        makeCommTxData();
                        sendMsg(CommTxBuffer);
                        readMsg(CommRxBuffer);
                        processRxData(CommRxBuffer, true);
                        
                        if(true == mBianHaoPIN_Low) {
                            this.setCommCmd(CMD_TYPE_WRITE, UART_MonomerClrTmpAddr_RegAddr, (short)1, (short)0x00, (short)0x00);
                        } else {
                            mBianHao_TestRes = 1;
                        }
                    }
                    if(true == cmd_Set_bianhao_High) {
                        makeCommTxData();
                        sendMsg(CommTxBuffer);
                        readMsg(CommRxBuffer);
                        processRxData(CommRxBuffer, true);
                        
                        if(false == mBianHaoPIN_Low) {
                            mBianHao_TestRes = 0;
                        } else {
                            mBianHao_TestRes = 2;
                        }
                    }
                    
                    Thread.sleep(200);
                } catch (InterruptedException | IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    exitCommPortThread();
                    JOptionPane.showMessageDialog(null, "MSP_Monomer CommPort ´®¿ÚÒì³£: " + e.getMessage());
                }
            } else {
                //while(true == dt_update_en) {
                    Ecb_Aes my_aes = new Ecb_Aes();
                    String dfu_text_inf = "";
                    File f = new File(dt_update_file);
                    //---------------------------------------------------------------------//
                    //-------------------------- FILE CRC CHECK ---------------------------//
                    long file_len_hd = f.length();
                    ByteBuffer bbf_data = ByteBuffer.allocate((int) file_len_hd);
                    bbf_data.order(ByteOrder.BIG_ENDIAN);
                    
                    try {
                        FileInputStream fis_hd = new FileInputStream(f);
                        byte[] buf_hd_cipher = new byte[(int) (file_len_hd)];
                        fis_hd.read(buf_hd_cipher);
                        fis_hd.close();
                        
                        byte[] buf_hd = new byte[(int) (file_len_hd)];
                        my_aes.ecb_decrypt(buf_hd_cipher, buf_hd, buf_hd_cipher.length);
                        bbf_data.put(buf_hd);
                        bbf_data.flip();
                        int crc32_source = bbf_data.getInt();
                        
                        CRC32 crc32 = new CRC32();
                        crc32.update(bbf_data);
                        
                        if(crc32_source != (int)crc32.getValue()) {
                            dfu_text_inf += "File CRC Check Error!";
                            dt_show_msg.setText(dfu_text_inf);
                            JOptionPane.showMessageDialog(null, "ÊäÈëÎļþ´íÎó»ò¸ñʽÒì³£!");
                            dt_update_en = false;
                            continue;
                        }
                        
                        bbf_data.position(Integer.BYTES);
                        byte[] buf_hd_inf = new byte[64-4];
                        bbf_data.get(buf_hd_inf);
                        int len = 0;
                        for(int n=0; n<buf_hd_inf.length; n++) {
                            if(buf_hd_inf[n] > 0) {
                                len += 1;
                            }
                        }
                        dfu_text_inf += "DFU File CRC Check OK, File Inf: " + new String(buf_hd_inf, 0, len, "UTF-8") + "\n";
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    //----------------------------------------------------------------------------//
                    //-------------------------- BOOTLOADER_CMD_GETINF ---------------------------//
                    dfu_text_inf += "DFU DEV ADDR: " + dt_target_addr + "\n";
                    dt_show_msg.setText(dfu_text_inf);
                    dt_update_en = true;
                    try {
                        makeCommTxData();
                        sendMsg(CommTxBuffer);
                        readMsg(CommRxBuffer);
                        Thread.sleep(100);
                        setCommBautrate(dt_dfu_bitrate);
                        int dot_count = 0;
                        while(true) {
                            //====================================//
                            if((false == dt_update_en) || (false == CommThreadRunning)) {
                                dfu_text_inf += "DFU Manual Stop . . .\n";
                                break;
                            }
                            //====================================//
                            Thread.sleep(250);
                            sendMsg(makeDfuBuf(DFUCommTxBuffer, BOOTLOADER_CMD_GETINF, 0, null));
                            readMsg(DFUCommRxBuffer);
                            //System.out.println("DFU_RX:"+ComFn.bytesToHexString(DFUCommRxBuffer.array(), DFUCommRxBuffer.limit()) + "\n");
                            if(true == checkDfuBuf(DFUCommRxBuffer, BOOTLOADER_CMD_GETINF, 0)) {
                                break;
                            }
                            String text_t = "";
                            for(int n=0; n<dot_count; n++) {
                                text_t += " .";
                            }
                            if(++dot_count >= 6) {
                                dot_count = 0;
                            }
                            
                            dt_show_msg.setText(dfu_text_inf + "Wait For Target MSP_DFU Ready" + text_t + "\n");
                        }
                        if(true == dt_update_en) {
                            dfu_text_inf += "DFU_Version: " + Long.toHexString(dt_dfu_FirmVersion).toUpperCase() + "\n";
                        }
                        if(true == dt_update_en) {
                            dfu_text_inf += "Target DFU Ready OK!\n";
                        }
                    } catch (IOException | InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    //----------------------------------------------------------------------------//
                    //----------------------------------------------------------------------------//
                    bbf_data.position(64);
                    byte[] txt_bytes = new byte[bbf_data.limit()-64];
                    bbf_data.get(txt_bytes);
                    long file_len = txt_bytes.length/3;
                    
                    ArrayList<byte[]> al_dfu_data = new ArrayList<byte[]>();
                    ArrayList<Integer> al_dfu_addr = new ArrayList<Integer>();
                    dfu_text_inf = getMSP430BinCode(txt_bytes, file_len, dfu_text_inf, al_dfu_data, al_dfu_addr);
                    //--------------------------- BOOTLOADER_CMD_WRITE----------------------------//
                    //----------------------------------------------------------------------------//
                    boolean prog_ok = false;
                    //byte[] buf_to_flash = new byte[BOOTLOADER_BUF_LEN];
                    try {
                        Thread.sleep(100);
                        int prog_addr = 0;
                        int percent = 0;
                        int dfu_dat_index = 0;
                        while(dfu_dat_index < al_dfu_data.size()) {
                            
                            //====================================//
                            if((false == dt_update_en) || (false == CommThreadRunning)) {
                                dfu_text_inf += "DFU Manual Stop . . .\n";
                                break;
                            }
                            //====================================//
                            
                            byte[] buf_to_flash = al_dfu_data.get(dfu_dat_index);
                            prog_addr = al_dfu_addr.get(dfu_dat_index);
                            dfu_dat_index += 1;
                            
                            //System.out.println(ComFn.bytesToHexString(buf_to_flash, BOOTLOADER_BUF_LEN) + "\n");
                            for(int cnt_t=0; cnt_t<5; cnt_t++) {
                                sendMsg(makeDfuBuf(DFUCommTxBuffer, BOOTLOADER_CMD_WRITE, prog_addr, buf_to_flash));
                                readMsg(DFUCommRxBuffer);
                                //System.out.println(ComFn.bytesToHexString(DFUCommRxBuffer.array(), DFUCommRxBuffer.limit()) + "\n");
                                if(true == checkDfuBuf(DFUCommRxBuffer, BOOTLOADER_CMD_WRITE, prog_addr)) {
                                    prog_ok = true;
                                    break;
                                } else {
                                    prog_ok = false;
                                    Thread.sleep(100);
                                }
                            }
                            if(false == prog_ok) {
                                dfu_text_inf += "DFU Write Error!\n";
                                break;
                            }
                            
                            if(percent < (dfu_dat_index*100)/al_dfu_data.size()) {
                                percent = (int) ((dfu_dat_index*100)/al_dfu_data.size());
                                if(percent > 100) {
                                    percent = 100;
                                }
                                dt_show_msg.setText(dfu_text_inf + String.format("DFU Write: %d%% Done.\n", percent));
                            }
                        }
                        dfu_text_inf += "DFU Write: 100% Done!\n";
                    } catch (IOException | InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    //---------------------------------------------------------------------------------//
                    
                    if((true == dt_update_en) && (true == CommThreadRunning)) {
                        if(false == prog_ok) {
                            Toolkit.getDefaultToolkit().beep();
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            Toolkit.getDefaultToolkit().beep();
                            dt_update_en = false;
                        } else {
                            if(dt_target_addr < dt_dfu_addr_end) {
                                dt_target_addr += 1;
                                this.setCommCmd(SPCommMon_MSP.CMD_TYPE_WRITE, SPCommMon_MSP.UART_MonomerDFU_RegAddr, (short)0x55AA, (short)0xAA55, (short) 0x7F7F);
                            } else {
                                dt_update_en = false;
                                boolean restart_ck = false;
                                for(int cnt_t=0; cnt_t<3; cnt_t++) {
                                    try {
                                        sendMsg(makeDfuBuf(DFUCommTxBuffer, BOOTLOADER_CMD_QUIT, 0, null));
                                        readMsg(DFUCommRxBuffer);
                                    } catch (IOException e1) {
                                        // TODO Auto-generated catch block
                                        e1.printStackTrace();
                                    } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    if(true == checkDfuBuf(DFUCommRxBuffer, BOOTLOADER_CMD_QUIT, 0)) {
                                        restart_ck = true;
                                        break;
                                    } else {
                                        try {
                                            Thread.sleep(200);
                                        } catch (InterruptedException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                                    }
                                }
                                if(true == restart_ck) {
                                    dfu_text_inf += "Restart Target Done.\n";
                                    new PlaySound("6133.wav");
                                } else {
                                    dfu_text_inf += "Restart Target Faild...\n";
                                }
                                dt_show_msg.setText(dfu_text_inf);
                            }
                        }
                    }
                    //-----------------------------------------------------------------------------//
                    dt_show_msg.setText(dfu_text_inf + "DFU Process Stopped!\n");
                    //-----------------------------------------------------------------------------//
                    setCommBautrate(comm_bautrate);
                    //-----------------------------------------------------------------------------//
                    //-----------------------------------------------------------------------------//
                    for(int n=0; n<2; n++) {
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        makeCommTxDataNull();
                        try {
                            sendMsg(CommTxBuffer);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    //-----------------------------------------------------------------------------//
                    //-----------------------------------------------------------------------------//
                //}
            }
        }
        /******************************************/
        serialPort.close();
        /******************************************/
        System.out.println("Monomer MSP CommPort is quit!");
    }
    /*********************************************************************************************/
    String getMSP430BinCode(byte[] txt_bytes, long file_len, String dfu_text_inf_t,
                    ArrayList<byte[]> al_dfu_data, ArrayList<Integer> al_dfu_addr) {
        String dfu_text_inf = dfu_text_inf_t;
        try {
            int prog_addr = 0;
            int percent = 0;
            BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(txt_bytes)));
            boolean bbf_is_end = false;
            int data_addr = 0;
            
            while(!bbf_is_end) {
                int data_type = 0;
                int data_cnt = 0;
                
                byte[] buf_to_flash = new byte[BOOTLOADER_BUF_LEN];
                
                for(int n=0; n<buf_to_flash.length; n++) {
                    buf_to_flash[n] = (byte) 0xFF;
                }
                //-----------------------------------------------------------------------//
                while (true) {
                    String line = "";
                    if((line = br.readLine()) == null) {
                        bbf_is_end = true;
                        break;
                    }
                    //System.out.println("line:" + line);
                    if((line.contains("q")) || (line.length() < 3)) {
                        bbf_is_end = true;
                        break;
                    }
                    if(line.contains("@d000") || line.contains("@9000")) {
                        BOOT_APP_ADDR = Integer.parseInt(line.substring(1, line.length()), 16);
                        if(((dt_dfu_FirmVersion/10) >= 2040) && ((dt_dfu_FirmVersion/10) < 2141)) {
                            BOOT_APPVECTOR_ADDR = 0xFBDC;
                            BOOT_APPVECTOR_tag = "@fb";
                            BOOT_APPVECTOR_tagfull = "@fbdc";
                        } else {
                            if(0xD000 == BOOT_APP_ADDR) {
                                BOOT_APPVECTOR_ADDR = 0xFDE0;
                                BOOT_APPVECTOR_tag = "@fd";
                                BOOT_APPVECTOR_tagfull = "@fde2";
                            } else {
                                BOOT_APPVECTOR_ADDR = 0xFBDC;
                                BOOT_APPVECTOR_tag = "@fb";
                                BOOT_APPVECTOR_tagfull = "@fbdc";
                            }
                        }
                        //System.out.println("BOOT_APP_ADDR:" + Integer.parseInt(line.substring(1, line.length()), 16));
                        data_type = 0;
                        data_addr = 0;
                        continue;
                    }
                    
                    if(line.contains(BOOT_APPVECTOR_tag)/*line.contains("@fd") || line.contains("@fb")*/) {
                        data_type = 1;
                        data_cnt = 0;
                        data_addr = Integer.parseInt(line.substring(1, line.length()), 16) - BOOT_APPVECTOR_ADDR;//0xfbdc;//0xfde0;
                        //System.out.println(Integer.parseInt(line.substring(1, line.length()), 16));
                        if(line.contains(BOOT_APPVECTOR_tagfull)/*line.contains("@fde2") || line.contains("@fbdc")*/) {
                            break;
                        } else {
                            continue;
                        }
                    }
                    
                    byte[] dat = ComFn.hexStr2Byte(line);
                    //System.out.println("data:" + ComFn.bytesToHexString(dat, dat.length));
                    for(int n=0; n<dat.length; n++) {
                        int index = data_cnt+n+data_addr;
                        if(index < buf_to_flash.length) {
                            buf_to_flash[index] = dat[n];
                        }
                    }
                    if(1 == data_type) {
                        data_addr += dat.length;
                    }
                    //System.out.println(ComFn.bytesToHexString(dat, dat.length));
                    if(0 == data_type) {
                        data_cnt += dat.length;
                        if(data_cnt >= BOOTLOADER_BUF_LEN) {
                            break;
                        }
                    } else {
                        prog_addr = BOOT_APPVECTOR_ADDR-BOOT_APP_ADDR;
                    }
                }
                //-----------------------------------------------------------------------//
                al_dfu_data.add(buf_to_flash);
                al_dfu_addr.add(prog_addr);
                
                prog_addr += BOOTLOADER_BUF_LEN;
                if(percent < (prog_addr*100)/file_len) {
                    percent = (int) ((prog_addr*100)/file_len);
                    if(percent > 100) {
                        percent = 100;
                    }
                    dt_show_msg.setText(dfu_text_inf + String.format("DFU Write: %d%% Done.\n", percent));
                }
                //-----------------------------------------------------------------------//
            }
            dfu_text_inf += "DFU data release: 100% Done!\n";
            dt_show_msg.setText(dfu_text_inf);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        //---------------------------------------------------------------------------------//
        
        return dfu_text_inf;
    }
    /*********************************************************************************************/
}