From 842237345ac469b02e9add8f9fd8bae5d4f7cdac Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期二, 25 二月 2025 14:33:15 +0800
Subject: [PATCH] Merge branch 'master' of http://8.140.179.55:20000/r/pipIrr-SV

---
 pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/softUpgrade/parse/HexFileParse.java |   77 +++++++++++++++++++++++++++++++-------
 1 files changed, 62 insertions(+), 15 deletions(-)

diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/softUpgrade/parse/HexFileParse.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/softUpgrade/parse/HexFileParse.java
index e2f09db..437d4a7 100644
--- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/softUpgrade/parse/HexFileParse.java
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/softUpgrade/parse/HexFileParse.java
@@ -2,8 +2,10 @@
 
 import cn.hutool.core.util.HexUtil;
 import com.dy.common.util.ByteUtil;
+import com.dy.common.util.CRC16;
 
 import java.io.*;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Stream;
 
@@ -16,18 +18,19 @@
 
     private static final int hexFileMinLine = 4 ;
     private static final int lineHeadEndIndex = 9 ;
-    private static final int bytesSplitUnit = 512 ;
+    private static final int lineValidCharCount = 43 ;//姣忚鏈夋晥瀛楃锛堝啋鍙蜂笌鍗佸叚杩涘埗锛変釜鏁帮紝渚嬪鈥�:1096C000DC96010010020020203E00000441000052鈥濓紝浣嗗�掓暟绗�3琛屼笉鍙楅檺鍒�
+    private static final int bytesSplitUnit512 = 512 ;
 
     public static void main(String[] args) throws Exception{
-        boolean fromBytes = true ;
+        boolean fromBytes = false ;
         HexFileParse obj = new HexFileParse() ;
         if(!fromBytes){
-            String filePath = "D:\\hexFile\\YuanMu_edit241007_01.hex";
+            String filePath = "D:\\hexFile\\YuanMu_20250116_01.hex";
             //File file = new File(filePath) ;
             HexFileVo vo =  obj.doParse(filePath) ;
             System.out.println(vo.toString());
         }else{
-            String filePath = "D:\\hexFile\\YuanMu_edit241007_01.hex";
+            String filePath = "D:\\hexFile\\YuanMu_20250116_01.hex";
             File file = new File(filePath) ;
             byte[] fileContent = new byte[(int)file.length()] ;
             FileInputStream fileInputStream = new FileInputStream(file) ;
@@ -65,7 +68,8 @@
         HexFileVo vo = new HexFileVo() ;
         this.checkAndCalculate(vo, lineList);
         this.parse(vo, lineList);
-        this.split(vo);
+        this.splitBytesByUnit512(vo);
+        this.calculateCrc(vo);
         return vo ;
     }
 
@@ -85,7 +89,8 @@
         HexFileVo vo = new HexFileVo() ;
         this.checkAndCalculate(vo, lineList);
         this.parse(vo, lineList);
-        this.split(vo);
+        this.splitBytesByUnit512(vo);
+        this.calculateCrc(vo);
         return vo ;
     }
 
@@ -106,7 +111,9 @@
         if(vo.totalLines < hexFileMinLine){
             throw new Exception("Hex鏂囦欢鍐呭琛屾暟" + vo.totalLines + "涓嶆纭�(鏈�灏忚鏁�" + hexFileMinLine +")") ;
         }
-        vo.calculateBytes = ((vo.totalLines - 4) * 32) / 1024 * 512 ;
+        vo.calculateBytes = ((((vo.totalLines - 4) * 32) / 1024)
+                + ((((vo.totalLines - 4) * 32) % 1024) > 0?1 : 0))
+                * 512 ;
     }
 
     /**
@@ -136,12 +143,16 @@
                         if(!line.startsWith(":")){
                             vo.errors.add("绗�" + counter + "琛屽唴瀹逛笉鏄互:寮�澶�") ;
                         }else{
-                            line = line.substring(lineHeadEndIndex, line.length() - 2) ;
-                            if(vo.bytes.length == 0){
-                                vo.bytes = HexUtil.decodeHex(line) ;
+                            if(counter != vo.totalLines - 3 && line.length() != lineValidCharCount){
+                                //鏃犳晥鏁版嵁锛岃垗寮冩湰琛屾暟鎹�
                             }else{
-                                byte[] bytes = HexUtil.decodeHex(line) ;
-                                vo.bytes = ByteUtil.bytesMerge(vo.bytes, bytes) ;
+                                line = line.substring(lineHeadEndIndex, line.length() - 2) ;
+                                if(vo.bytes.length == 0){
+                                    vo.bytes = HexUtil.decodeHex(line) ;
+                                }else{
+                                    byte[] bytes = HexUtil.decodeHex(line) ;
+                                    vo.bytes = ByteUtil.bytesMerge(vo.bytes, bytes) ;
+                                }
                             }
                         }
                     }
@@ -153,23 +164,59 @@
 
     /**
      * 浠�512涓哄崟浣嶅垎鍓叉暟鎹寘
+     * @param bytes
+     */
+    public List<byte[]> splitBytesByUnit512(byte[] bytes){
+        List<byte[]> listByte512 = new ArrayList<>();
+        if(bytes != null && bytes.length > 0){
+            int index = 0 ;
+            while (true){
+                if(index < bytes.length){
+                    byte[] bs = ByteUtil.bytesSplit(bytes, index, bytesSplitUnit512) ;
+                    listByte512.add(bs) ;
+                }else{
+                    break ;
+                }
+                index += bytesSplitUnit512;
+            }
+        }
+        return listByte512 ;
+    }
+
+    /**
+     * 浠�512涓哄崟浣嶅垎鍓叉暟鎹寘
      * @param vo
      */
-    private void split(HexFileVo vo){
+    private void splitBytesByUnit512(HexFileVo vo){
         if(vo.bytes != null && vo.bytes.length > 0){
             vo.realBytes += vo.bytes.length ;
             int index = 0 ;
             while (true){
                 if(index < vo.bytes.length){
-                    byte[] bytes = ByteUtil.bytesSplit(vo.bytes, index, bytesSplitUnit) ;
+                    byte[] bytes = ByteUtil.bytesSplit(vo.bytes, index, bytesSplitUnit512) ;
                     vo.listByte512.add(bytes) ;
                     vo.totalBytes512 += bytes.length ;
                 }else{
                     break ;
                 }
-                index += bytesSplitUnit ;
+                index += bytesSplitUnit512;
             }
         }
     }
 
+    /**
+     * 璁$畻CRC16
+     * @param vo
+     */
+    private void calculateCrc(HexFileVo vo){
+        int crc = 0xffff ;
+        if(vo.listByte512 != null && vo.listByte512.size() > 0){
+            CRC16 crc16 = new CRC16() ;
+            for(byte[] bs : vo.listByte512){
+                crc =  crc16.CRC16_table(bs.length, bs, crc) ;
+            }
+        }
+        vo.bytesCrc16 = crc ;
+    }
+
 }

--
Gitblit v1.8.0