From 70aeac289c132085e91d59dc82bc1facd41889fa Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期一, 24 二月 2025 15:33:53 +0800 Subject: [PATCH] 有关表阀一体机(王江海制定)通信协议: 1、根据新版本协议,解析部分更新; 2、根据测试一体阀协议数据处理需求与逻辑,进行表阀一体机数据处理。 --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V202404/CommonV202404.java | 124 ++++++++++++++++++++++++++++++++++------- 1 files changed, 103 insertions(+), 21 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 42c9190..9f25e16 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 涓婅瀛楄妭鏁扮粍 @@ -15,14 +29,22 @@ public Boolean isThisProtocolHead(byte[] bs) throws Exception{ if(bs == null){ return null ; - }else if(bs.length >= (ProtocolConstantV206V202404.ctrlIndex - 1) + }else if(bs.length >= ProtocolConstantV206V202404.ctrlIndex && bs[0] == ProtocolConstantV206V202404.P_Head_Byte){ if(bs[2] == ProtocolConstantV206V202404.P_Head_Byte){ - return true ; + if(bs[3] == (byte)0x80 || bs[3] == (byte)0x81){ + return true ; + }else{ + return false ; + } }else if((byte)(bs[2] & 0xF8) == ProtocolConstantV206V202404.P_Head_Byte){ - //濡傛灉鎺у埗鍩熴�佸湴鍧�鍩熴�佺敤鎴锋暟鎹煙(搴旂敤灞�)鐨勫瓧鑺傛�绘暟澶т簬255锛屽垯閫氳繃鎵╁睍绗簩涓紑濮嬪瓧绗�0x68鐨勪綆3浣嶄綔涓洪暱L鐨勯珮浣嶆墿灞� + //濡傛灉鎺у埗鍩熴�佸湴鍧�鍩熴�佺敤鎴锋暟鎹煙(搴旂敤灞�)鐨勫瓧鑺傛�绘暟澶т簬255锛屽垯閫氳繃鎵╁睍绗簩涓紑濮嬪瓧绗�0x69鐨勪綆3浣嶄綔涓洪暱L鐨勯珮浣嶆墿灞� //0xF8浜岃繘鍒讹細 11111000 - return true ; + if(bs[3] == (byte)0x80 || bs[3] == (byte)0x81){ + return true ; + }else{ + return false ; + } }else{ return false ; } @@ -32,23 +54,24 @@ } /** - * 妫�鏌ュご + * 妫�鏌ュ崗璁被鍨� * @param bs 涓婅瀛楄妭鏁扮粍 + * @return 鍗忚绫诲瀷 * @throws Exception 寮傚父 */ - public void checkHead(byte[] bs) throws Exception{ - if(bs.length < ProtocolConstantV206V202404.lenHead2Code - || bs[0] != ProtocolConstantV206V202404.P_Head_Byte){ - throw new 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{ - if(bs.length >= ProtocolConstantV206V202404.lenHead2Code){ - if(bs[2] != ProtocolConstantV206V202404.P_Head_Byte - && (byte)(bs[2] & 0xF8) != ProtocolConstantV206V202404.P_Head_Byte){ - throw new Exception("涓婅鏁版嵁甯уご涓嶆纭紒") ; - } - }else{ - throw new Exception("涓婅鏁版嵁甯уご涓嶆纭紒") ; - } + return null ; } } @@ -89,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 ; } @@ -137,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锛屾湭閫氳繃杩斿洖鍘熷洜 @@ -152,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 ; + } + } + } /* 鏋勯�犳帶鍒跺煙 @@ -172,9 +245,9 @@ public byte createCtrl(byte dir, byte funcCode){ byte b = dir;//DIR = 1(0x80)锛岃〃绀烘甯ф姤鏂囨槸鐢辩粓绔彂鍑虹殑涓婅鎶ユ枃锛� b = (byte)(b | funcCode) ; - //DIV = 1(0x40)锛岃〃绀烘鎶ユ枃宸茶鎷嗗垎涓鸿嫢骞插抚 - //FCB = 1(0x08)锛岃〃绀哄彧鍙戜竴娆� - b = (byte)(b |0x08) ; + //DIV = 1(0x20)锛岃〃绀烘鎶ユ枃宸茶鎷嗗垎涓鸿嫢骞插抚 + //FCB = 1(0x10)锛岃〃绀哄彧鍙戜竴娆� + b = (byte)(b |0x10) ; //DIR = 0 涓嬭锛屽垯鍔熻兘鐮侀噰鐢�0 return b ; } @@ -200,6 +273,15 @@ }; } + public static String ctrlDevType(String hex){ + return switch (hex) { + case "01" -> "娴嬫帶涓�浣撻榾"; + case "02" -> "琛ㄩ榾涓�浣撴満"; + case "57" -> "浜曠數鎺у埗鍣�"; + default -> "鏈煡"; + }; + } + /* * 鍒嗘瀽鐗堟湰鍙� * @param bs 涓婅瀛楄妭鏁扮粍 -- Gitblit v1.8.0