From 4b067b4db5c1f2a068d6e27cc138118810785e2d Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期三, 07 五月 2025 10:47:13 +0800
Subject: [PATCH] 1、创建命令生成器模块,实现部分命令数据生成,还需继续实现其他命令; 2、完善代码。
---
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/softUpgrade/parse/HexFileParse.java | 222 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 222 insertions(+), 0 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
new file mode 100644
index 0000000..437d4a7
--- /dev/null
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/softUpgrade/parse/HexFileParse.java
@@ -0,0 +1,222 @@
+package com.dy.common.softUpgrade.parse;
+
+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;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2024/11/2 8:51
+ * @Description
+ */
+public class HexFileParse {
+
+ private static final int hexFileMinLine = 4 ;
+ private static final int lineHeadEndIndex = 9 ;
+ private static final int lineValidCharCount = 43 ;//姣忚鏈夋晥瀛楃锛堝啋鍙蜂笌鍗佸叚杩涘埗锛変釜鏁帮紝渚嬪鈥�:1096C000DC96010010020020203E00000441000052鈥濓紝浣嗗�掓暟绗�3琛屼笉鍙楅檺鍒�
+ private static final int bytesSplitUnit512 = 512 ;
+
+ public static void main(String[] args) throws Exception{
+ boolean fromBytes = false ;
+ HexFileParse obj = new HexFileParse() ;
+ if(!fromBytes){
+ 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_20250116_01.hex";
+ File file = new File(filePath) ;
+ byte[] fileContent = new byte[(int)file.length()] ;
+ FileInputStream fileInputStream = new FileInputStream(file) ;
+ fileInputStream.read(fileContent) ;
+ HexFileVo vo = obj.doParse(fileContent) ;
+ System.out.println(vo.toString());
+ }
+ }
+ /**
+ * 瑙f瀽Hex鏂囦欢
+ * @param path
+ * @return
+ * @throws Exception
+ */
+ public HexFileVo doParse(String path) throws Exception {
+ File file = new File(path) ;
+ return this.doParse(file) ;
+ }
+
+ /**
+ * 瑙f瀽Hex鏂囦欢
+ * @param file
+ * @return
+ * @throws Exception
+ */
+ public HexFileVo doParse(File file) throws Exception{
+ if(file == null){
+ throw new Exception("Hex鏂囦欢瀵硅薄涓簄ull") ;
+ }
+ if(!file.exists()){
+ throw new Exception("Hex鏂囦欢涓嶅瓨鍦�") ;
+ }
+ Stream<String> linesStream = new BufferedReader(new FileReader(file)).lines() ;
+ List<String> lineList = linesStream.toList() ;
+ HexFileVo vo = new HexFileVo() ;
+ this.checkAndCalculate(vo, lineList);
+ this.parse(vo, lineList);
+ this.splitBytesByUnit512(vo);
+ this.calculateCrc(vo);
+ return vo ;
+ }
+
+ /**
+ * 瑙f瀽Hex鏂囦欢
+ * @param fileContent
+ * @return
+ * @throws Exception
+ */
+ public HexFileVo doParse(byte[] fileContent) throws Exception{
+ if(fileContent == null || fileContent.length == 0){
+ throw new Exception("Hex鏂囦欢瀵硅薄涓簄ull") ;
+ }
+ ByteArrayInputStream byteInputStream = new ByteArrayInputStream(fileContent);
+ Stream<String> linesStream = new BufferedReader(new InputStreamReader(byteInputStream)).lines() ;
+ List<String> lineList = linesStream.toList() ;
+ HexFileVo vo = new HexFileVo() ;
+ this.checkAndCalculate(vo, lineList);
+ this.parse(vo, lineList);
+ this.splitBytesByUnit512(vo);
+ this.calculateCrc(vo);
+ return vo ;
+ }
+
+ /**
+ * 鍚堟硶鎬ф鏌ワ紝骞惰绠楁暟鎹寘鏁�
+ * @param vo
+ * @param lineList
+ * @throws Exception
+ */
+ private void checkAndCalculate(HexFileVo vo, List<String> lineList) throws Exception{
+ if(lineList == null){
+ throw new Exception("Hex鏂囦欢鍐呭涓虹┖") ;
+ }
+ vo.totalLines = lineList.size() ;
+ if(vo.totalLines <= 0){
+ throw new Exception("Hex鏂囦欢鍐呭涓虹┖") ;
+ }
+ if(vo.totalLines < hexFileMinLine){
+ throw new Exception("Hex鏂囦欢鍐呭琛屾暟" + vo.totalLines + "涓嶆纭�(鏈�灏忚鏁�" + hexFileMinLine +")") ;
+ }
+ vo.calculateBytes = ((((vo.totalLines - 4) * 32) / 1024)
+ + ((((vo.totalLines - 4) * 32) % 1024) > 0?1 : 0))
+ * 512 ;
+ }
+
+ /**
+ * 瑙f瀽Hex鏂囦欢鍐呭锛岀粍鍚堟垚涓�涓ぇ鏁版嵁鍖�
+ * @param vo
+ * @param lineList
+ * @throws Exception
+ */
+ private void parse(HexFileVo vo, List<String> lineList) throws Exception{
+ int counter = 0 ;
+ for(String line : lineList){
+ if(counter == 0){
+ // 鏂囦欢澶�
+ }else if(counter == vo.totalLines - 2){
+ // 鏂囦欢灏惧�掓暟绗�2琛�
+ }else if(counter == vo.totalLines - 1){
+ // 鏂囦欢灏惧�掓暟绗�1琛�
+ }else{
+ // 鏂囦欢鍐呭
+ if(line == null){
+ vo.errors.add("绗�" + counter + "琛屽唴瀹逛负绌�") ;
+ }else{
+ line = line.trim() ;
+ if(line.length() == 0){
+ vo.errors.add("绗�" + counter + "琛屽唴瀹逛负绌�") ;
+ }else{
+ if(!line.startsWith(":")){
+ vo.errors.add("绗�" + counter + "琛屽唴瀹逛笉鏄互:寮�澶�") ;
+ }else{
+ if(counter != vo.totalLines - 3 && line.length() != lineValidCharCount){
+ //鏃犳晥鏁版嵁锛岃垗寮冩湰琛屾暟鎹�
+ }else{
+ 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) ;
+ }
+ }
+ }
+ }
+ }
+ }
+ counter ++ ;
+ }
+ }
+
+ /**
+ * 浠�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 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, bytesSplitUnit512) ;
+ vo.listByte512.add(bytes) ;
+ vo.totalBytes512 += bytes.length ;
+ }else{
+ break ;
+ }
+ 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