From 4fe39e9df868a8fdd40549f169adbea2e8bd622a Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期三, 27 十一月 2024 15:34:09 +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/mw/protocol/p206V202404/CommonV202404.java |   87 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 86 insertions(+), 1 deletions(-)

diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V202404/CommonV202404.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V202404/CommonV202404.java
index e5a19ef..dd0eedd 100644
--- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V202404/CommonV202404.java
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V202404/CommonV202404.java
@@ -1,11 +1,25 @@
 package com.dy.common.mw.protocol.p206V202404;
 
+import com.dy.common.mw.channel.tcp.TcpIoSessionAttrIdIsRtuAddr;
+import com.dy.common.mw.protocol.p206V1.ProtocolConstantV206V1;
+import com.dy.common.mw.protocol.p206V2.ProtocolConstantV206V2;
 import com.dy.common.util.ByteUtil;
 import com.dy.common.util.ByteUtilUnsigned;
+import com.dy.common.util.CRC16;
 import com.dy.common.util.CRC8_for_2_0;
+import org.apache.mina.core.session.IoSession;
 
 
 public class CommonV202404 {
+
+    /**
+     * 鍦↖o浼氳瘽涓缃崗璁悕绉板強鐗堟湰鍙�
+     * @param ioSession 浼氳瘽
+     */
+    public void setThisProtocolArr2IoSession(IoSession ioSession){
+        ioSession.setAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolName, ProtocolConstantV206V1.protocolName) ;
+        ioSession.setAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolName, ProtocolConstantV206V1.protocolVer) ;
+    }
     /**
      * 妫�鏌ュご
      * @param bs 涓婅瀛楄妭鏁扮粍
@@ -39,6 +53,27 @@
         }
     }
 
+    /**
+     * 妫�鏌ュ崗璁被鍨�
+     * @param bs 涓婅瀛楄妭鏁扮粍
+     * @return 鍗忚绫诲瀷
+     * @throws Exception 寮傚父
+     */
+    public Boolean protocolType_p206TrueUgFalse(byte[] bs){
+        if(bs == null){
+            return null ;
+        }else if(bs.length >= (ProtocolConstantV206V202404.ctrlIndex)
+                && bs[0] == ProtocolConstantV206V202404.P_Head_Byte
+                && bs[2] == ProtocolConstantV206V202404.P_Head_Byte){
+            return true ;
+        }else if(bs.length >= (ProtocolConstantV206V202404.UG_codeIndex)
+                && bs[0] == ProtocolConstantV206V202404.UG_P_Head_Byte
+                && bs[3] == ProtocolConstantV206V202404.UG_P_Head_Byte){
+            return false ;
+        }else{
+            return null ;
+        }
+    }
 
     /**
      * 妫�鏌ュ熬
@@ -77,8 +112,19 @@
      * @return 鏁版嵁闀垮害
      * @throws Exception 寮傚父
      */
-    public int parseDataLen(byte[] bs)throws Exception{
+    public int parseDataLen4P202404(byte[] bs)throws Exception{
         return parseFrameLen(bs) ;
+    }
+
+    /**
+     * 鍒嗘瀽鐢ㄦ埛鏁版嵁鍩熷瓧鑺傛暟(鍗囩骇鍗忚)
+     * @param bs 涓婅瀛楄妭鏁扮粍
+     * @return 鏁版嵁闀垮害
+     * @throws Exception 寮傚父
+     */
+    public int parseDataLen4Ug(byte[] bs)throws Exception{
+        int len = ByteUtilUnsigned.bytes2Short_LE(bs, ProtocolConstantV206V2.UG_dataLenIndex_start) ;
+        return len - ProtocolConstantV206V2.UG_lenCmd - ProtocolConstantV206V2.UG_lenRtuAddr ;
     }
 
 
@@ -125,6 +171,18 @@
     }
 
     /**
+     * 鍒嗘瀽鍔熻兘鐮�
+     * @param bs 涓婅瀛楄妭鏁扮粍
+     * @return 鍔熻兘鐮�
+     */
+    public String parseCode(byte[] bs, boolean p202404TrueUgFalse){
+        if(p202404TrueUgFalse) {
+            return ByteUtil.bytes2Hex(bs, false, ProtocolConstantV206V202404.codeIndex, 1);
+        }else{
+            return ByteUtil.bytes2Hex(bs, false, ProtocolConstantV206V202404.UG_codeIndex, 2);
+        }
+    }
+    /**
      * 鏍¢獙鍜屾鏌�
      * @param bs  涓婅瀛楄妭鏁扮粍
      * @return 閫氳繃null锛屾湭閫氳繃杩斿洖鍘熷洜
@@ -140,6 +198,33 @@
         }
     }
 
+    /**
+     * 鏍¢獙鍜屾鏌�
+     * @param bs  涓婅瀛楄妭鏁扮粍
+     * @param p202404TrueUgFalse 202404鍗忚涓簍rue锛屽崌绾у崗璁负false
+     * @return 閫氳繃null锛屾湭閫氳繃杩斿洖鍘熷洜
+     * @throws Exception 寮傚父
+     */
+    public String checkCrc_str(byte[] bs, boolean p202404TrueUgFalse) throws Exception {
+        if(p202404TrueUgFalse){
+            byte crcCompute = (byte)new CRC8_for_2_0().CRC8(bs, ProtocolConstantV206V202404.ctrlIndex, bs.length - 3) ;
+            byte crcInBs = bs[bs.length - 2] ;
+            if(crcCompute == crcInBs){
+                return null ;
+            }else{
+                return "璁$畻CRC鏄�:" + crcCompute + "锛屼笂浼燙RC鏄�" + crcInBs ;
+            }
+        }else{
+            short crcCompute = new CRC16().CRC(bs, 0, bs.length - 4) ;
+            short crcInBs = ByteUtil.bytes2Short_BE(bs,bs.length - 3) ;
+            //int crcInBs = ByteUtilUnsigned.bytes2Short_BE(bs, bs.length - 3) ;
+            if(crcCompute == crcInBs){
+                return null ;
+            }else{
+                return "璁$畻CRC鏄�:" + crcCompute + "锛屼笂浼燙RC鏄�" + crcInBs ;
+            }
+        }
+    }
 
     /*
     鏋勯�犳帶鍒跺煙

--
Gitblit v1.8.0