From 6cf345bc8fe98d393320b89182f9293171aa19ff Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期二, 22 四月 2025 16:39:55 +0800 Subject: [PATCH] 1、根据江海协议中开阀类型变化,修改相应代码; 2、江海协议RTU模拟器启动参数注释。 --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V202404/CommonV202404.java | 389 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 389 insertions(+), 0 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 new file mode 100644 index 0000000..f21a76e --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V202404/CommonV202404.java @@ -0,0 +1,389 @@ +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 涓婅瀛楄妭鏁扮粍 + * @return true鏄�,false鍚� + * @throws Exception 寮傚父 + */ + public Boolean isThisProtocolHead(byte[] bs) throws Exception{ + if(bs == null){ + return null ; + }else if(bs.length >= ProtocolConstantV206V202404.ctrlIndex + && bs[0] == ProtocolConstantV206V202404.P_Head_Byte){ + if(bs[2] == ProtocolConstantV206V202404.P_Head_Byte){ + 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锛屽垯閫氳繃鎵╁睍绗簩涓紑濮嬪瓧绗�0x69鐨勪綆3浣嶄綔涓洪暱L鐨勯珮浣嶆墿灞� + //0xF8浜岃繘鍒讹細 11111000 + if(bs[3] == (byte)0x80 || bs[3] == (byte)0x81){ + return true ; + }else{ + return false ; + } + }else{ + return false ; + } + }else{ + return false ; + } + } + + /** + * 妫�鏌ュ崗璁被鍨� + * @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 ; + } + } + + /** + * 妫�鏌ュ熬 + * @param bs 涓婅瀛楄妭鏁扮粍 + * @throws Exception 寮傚父 + */ + public void checkTail(byte[] bs) throws Exception{ + if(bs.length < 1 || bs[bs.length - 1] != ProtocolConstantV206V202404.P_Tail_Byte){ + throw new Exception("涓婅鏁版嵁灏句笉姝g‘锛�") ; + } + } + + + /** + * 鍒嗘瀽甯ч暱搴� + * @param bs 涓婅瀛楄妭鏁扮粍 + * @return 鏁版嵁闀垮害 + * @throws Exception 寮傚父 + */ + public int parseFrameLen(byte[] bs)throws Exception{ + if(bs[2] != ProtocolConstantV206V202404.P_Head_Byte + && (byte)(bs[2] & 0xF8) != ProtocolConstantV206V202404.P_Head_Byte){ + int lenH = bs[2] & 0x07 ; + int lenL = ByteUtilUnsigned.byte2Byte(bs, ProtocolConstantV206V202404.dataLenIndex) ; + return (lenH * 100) + (lenL + ProtocolConstantV206V202404.lenHead2ctrl + ProtocolConstantV206V202404.lenTail) ; + }else{ + int len = ByteUtilUnsigned.byte2Byte(bs, ProtocolConstantV206V202404.dataLenIndex) ; + return len + ProtocolConstantV206V202404.lenHead2ctrl + ProtocolConstantV206V202404.lenTail ; + } + } + + + /** + * 鍒嗘瀽鐢ㄦ埛鏁版嵁鍩熷瓧鑺傛暟 + * @param bs 涓婅瀛楄妭鏁扮粍 + * @return 鏁版嵁闀垮害 + * @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 ; + } + + + /** + * 鍒嗘瀽Rtu鍦板潃 + * @param bs 涓婅瀛楄妭鏁扮粍 + * @return 鎺у埗鍣ㄥ湴鍧� + * @throws Exception 寮傚父 + */ + public String parseRtuAddr(byte[] bs)throws Exception{ + String rtuAddrBCD = "" + ByteUtil.BCD2Long_BE(bs, ProtocolConstantV206V202404.rtuAddr1Index_start, ProtocolConstantV206V202404.rtuAddr1Index_end) ; + String rtuAddrStr = "" + ByteUtilUnsigned.bytes2Short_LE(bs, ProtocolConstantV206V202404.rtuAddr2Index_start) ; + while(rtuAddrStr.length() < 5){ + rtuAddrStr = "0" + rtuAddrStr ; + } + return rtuAddrBCD + rtuAddrStr ; + } + + + /** + * 鍒嗘瀽Rtu鍦板潃 + * @param bs 涓婅瀛楄妭鏁扮粍 + * @param index 鍚浣� + * @return 鎺у埗鍣ㄥ湴鍧� + * @throws Exception 寮傚父 + */ + public String parseRtuAddr(byte[] bs, int index)throws Exception{ + String rtuAddrBCD = "" + ByteUtil.BCD2Long_BE(bs, index, index + ProtocolConstantV206V202404.rtuAddr1Index_end - ProtocolConstantV206V202404.rtuAddr1Index_start) ;//鍦板潃鏄ぇ绔ā寮� + String rtuAddrStr = "" + ByteUtilUnsigned.bytes2Short_LE(bs, index + 1 + ProtocolConstantV206V202404.rtuAddr1Index_end - ProtocolConstantV206V202404.rtuAddr1Index_start) ; + while(rtuAddrStr.length() < 5){ + rtuAddrStr = "0" + rtuAddrStr ; + } + return rtuAddrBCD + rtuAddrStr ; + } + + + /** + * 鍒嗘瀽鍔熻兘鐮� + * @param bs 涓婅瀛楄妭鏁扮粍 + * @return 鍔熻兘鐮� + */ + public String parseCode(byte[] bs){ + return ByteUtil.bytes2Hex(bs, false, ProtocolConstantV206V202404.codeIndex, 1) ; + } + + /** + * 鍒嗘瀽鍔熻兘鐮� + * @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锛屾湭閫氳繃杩斿洖鍘熷洜 + * @throws Exception 寮傚父 + */ + public String checkCrc_str(byte[] bs) throws Exception { + 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 ; + } + } + + /** + * 鏍¢獙鍜屾鏌� + * @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 ; + } + } + } + + /* + 鏋勯�犳帶鍒跺煙 + D7 D6 D5锝濪4 D3锝濪0 + 浼犺緭鏂瑰悜浣� DIR 鎷嗗垎鏍囧織浣� DIV 甯ц鏁颁綅 FCB 鍔熻兘鐮� + 1銆佷紶杈撴柟鍚戜綅(DIR) + DIR=0锛氳〃绀烘甯ф枃鏄敱涓績绔欏彂鍑虹殑涓嬭鎶ユ枃銆� + DIR=1锛氳〃绀烘甯ф枃鏄敱缁堢绔欏彂鍑虹殑涓婅鎶ユ枃銆� + 姣忓抚鎶ユ枃鐨勯�氫俊杩囩▼涓槸涓嶅彉鐨勩�� + 2銆佹媶鍒嗘爣蹇椾綅(DIV) + DIV=1锛氳〃绀烘鎶ユ枃宸茶鎷嗗垎涓鸿嫢骞插抚锛屾帴鏀跺悗闇�瑕佹嫾鎺ャ�傛鏃舵帶鍒跺煙鍚庡鍔犱竴涓瓧鑺傦紝涓烘媶鍒嗗抚璁版暟DIVS锛岄噰鐢˙IN鍊掕鏁�(255-1),1琛ㄧず鏈�鍚庝竴甯ф暟鎹�傚惎鍔ㄧ珯鍙戦�佹椂鑷姩鍔犱笂鍙戦�侊紝浠庡姩绔欒繑甯ф椂瀵瑰簲鍔犱笂纭銆� + DIV=0锛氳〃绀烘甯ф姤鏂囦负鍗曞抚銆� + 3銆佸抚璁℃暟浣�(FCB)锛� + FCB琛ㄧず姣忎釜绔欒繛缁殑鍙戦��/纭鎴栬姹�/鍝嶅簲鏈嶅姟鐨勫彉鍖栦綅銆侳CB浣嶇敤鏉ラ槻姝俊鎭紶杈撶殑涓㈠け鍜岄噸澶嶃�� + 鍚姩绔欏悜鍚屼竴浠庡姩绔欎紶杈撴柊鐨勫彂閫�/纭鎴栬姹�/鍝嶅簲浼犺緭鎶ュ姟鏃讹紝鍚姩绔欏皢璁剧疆FCB鍊硷紝鑻ヨ秴鏃舵湭鍒颁粠绔欑殑鎶ユ枃锛屾垨鎺ユ敹鍑虹幇宸敊锛屽垯鍚姩绔欏皢FCB鍑�1锛岄噸澶嶅師鏉ョ殑鍙戦��/纭鎴栬姹�/鍝嶅簲鏈嶅姟锛岀洿鍒癋CB鍑忎负0锛岃〃绀烘湰娆′紶杈撴湇鍔″け璐ャ�� + 浠庡姩绔欐敹鍒板惎鍔ㄧ珯FCB鍊间笉涓�0鐨勬姤鏂囧苟鎸夌収瑕佹眰纭鎴栧搷搴旀椂锛屽簲杩斿洖鐩稿簲鐨凢CB鍊笺�� + */ + public byte createCtrl(byte dir, byte funcCode){ + byte b = dir;//DIR = 1(0x80)锛岃〃绀烘甯ф姤鏂囨槸鐢辩粓绔彂鍑虹殑涓婅鎶ユ枃锛� + b = (byte)(b | funcCode) ; + //DIV = 1(0x20)锛岃〃绀烘鎶ユ枃宸茶鎷嗗垎涓鸿嫢骞插抚 + //FCB = 1(0x10)锛岃〃绀哄彧鍙戜竴娆� + b = (byte)(b |0x10) ; + //DIR = 0 涓嬭锛屽垯鍔熻兘鐮侀噰鐢�0 + return b ; + } + + /** + * 0x00:姝e父鍒峰崱寮�娉�/闃�鐢ㄦ按 + * 0x01:骞冲彴杩滅▼寮�娉�/闃�鐢ㄦ按 + * 0x02:鐢ㄦ埛杩滅▼寮�娉�/闃�鐢ㄦ按 + * 0x03:娴嬭瘯鍗★紝 + * 0x04:鍏跺畠 + * @param type + * @return + */ + public static String openValveType(byte type){ + return switch (type) { + case 0 -> "鍒峰崱寮�闃�"; + case 1 -> "涓績绔欏紑闃�"; + case 2 -> "鐢ㄦ埛杩滅▼寮�闃�"; + case 3 -> "娴嬭瘯鍗″紑闃�"; + case 4 -> "鍏跺畠寮�闃�"; + default -> "鏈煡"; + }; + } + /** + * 0x01锛氬埛鍗″紑闃� => p206V1 1 + * 0x02锛氫腑蹇冪珯寮�闃� => p206V1 3 + * 0x08锛氱敤鎴稟pp杩滅▼寮�闃� => p206V1 3 + * 0x0B锛氬贰妫�鍗″紑闃� => p206V1 11 + * @param type + * @return + */ + public static byte openType2P206V1(byte type){ + return switch (type) { + case 0 -> (byte)1 ; + case 1 -> (byte)3 ; + case 2 -> (byte)8 ; + case 3 -> (byte)11 ; + default -> (byte)-1; + }; + } + + /** + * 寰楀埌鍏冲紑闃�绫诲瀷鍚嶇О + * 0x00:鍒峰崱闂� + * 0x01:骞冲彴鍏� + * 0x02:APP鍏� + * 0x03:闈炴硶鍗″叧 + * 0x04:姘磋〃閫氳寮傚父鍏� + * 0x05:鐢佃〃寮傚父鍏� + * 0x06:鍓╀綑姘撮噺涓嶈冻鍏� + * 0x07:鍓╀綑閲戦涓�0鍏� + * 0x08:寮�娉�/闃�鍚庣閬撴病鏈夋祦閲忓叧 + * 0x09:鎺夌數鍐嶄笂鐢靛叧 + * 0x0a:姘磋〃鐬椂娴侀噺涓�0鍏� + * 0x0b:鍒峰崱寮�娉碉紝杩滅▼鍏� + * 0x0c:鐢垫睜浣庣數鍘嬪叧 + * @param type 瀛楄妭 + * @return 鍚嶇О + */ + public static String closeValveType(byte type){ + return switch (type) { + case 0 -> "鍒峰崱鍏抽榾"; + case 1 -> "骞冲彴鍏抽榾"; + case 2 -> "APP鍏抽榾"; + case 3 -> "闈炴硶鍗″叧闃�"; + case 4 -> "姘磋〃閫氳寮傚父鍏抽榾"; + case 5 -> "鐢佃〃寮傚父鍏抽榾"; + case 6 -> "鍓╀綑姘撮噺涓嶈冻鍏抽榾"; + case 7 -> "鍓╀綑閲戦涓�0鍏抽榾"; + case 8 -> "绠¢亾娌℃湁娴侀噺鍏抽榾"; + case 9 -> "鎺夌數鍐嶄笂鐢靛叧闃�"; + case 10 -> "姘磋〃鐬椂娴侀噺涓�0鍏抽榾"; + case 11 -> "鍒峰崱寮�杩滅▼鍏抽榾"; + case 12 -> "鐢垫睜浣庣數鍘嬪叧闃�"; + default -> "鏈煡"; + }; + } + /** + * 寰楀埌鍏冲紑闃�绫诲瀷鍚嶇О + * 0x00:鍒峰崱闂� => p206V1 2 + * 0x01:骞冲彴鍏� => p206V1 4 + * 0x02:APP鍏� => p206V1 4 + * 0x03:闈炴硶鍗″叧 => p206V1 12 + * 0x04:姘磋〃閫氳寮傚父鍏� => p206V1 6 + * 0x05:鐢佃〃寮傚父鍏� => p206V1 6 + * 0x06:鍓╀綑姘撮噺涓嶈冻鍏� => p206V1 5 + * 0x07:鍓╀綑閲戦涓�0鍏� => p206V1 5 + * 0x08:寮�娉�/闃�鍚庣閬撴病鏈夋祦閲忓叧 => p206V1 16 + * 0x09:鎺夌數鍐嶄笂鐢靛叧锛� => p206V1 7 + * 0x0a:姘磋〃鐬椂娴侀噺涓�0鍏筹紝 => p206V1 5 + * 0x0b:鍒峰崱寮�娉碉紝杩滅▼鍏炽�� => p206V1 9 + * 0x0c:鐢垫睜浣庣數鍘嬪叧銆� => p206V1 7 + * @param type 瀛楄妭 + * @return 鍚嶇О + */ + public static byte closeType2P206V1(byte type){ + return switch (type) { + case 0 -> (byte)2; + case 1,11 -> (byte)4; + case 2 -> (byte)9; + case 3 -> (byte)12; + case 4,5,10 -> (byte)6; + case 6,7 -> (byte)5; + case 8 -> (byte)16; + case 9,12 -> (byte)7; + default -> (byte)-1; + }; + } + + public static String ctrlDevType(String hex){ + return switch (hex) { + case "01" -> "娴嬫帶涓�浣撻榾"; + case "02" -> "琛ㄩ榾涓�浣撴満"; + case "57" -> "浜曠數鎺у埗鍣�"; + default -> "鏈煡"; + }; + } + + /* + * 鍒嗘瀽鐗堟湰鍙� + * @param bs 涓婅瀛楄妭鏁扮粍 + * @return 鐗堟湰鍙� + * @throws Exception 寮傚父 + public String parseVersion(byte[] bs)throws Exception{ + short ver = ByteUtilUnsigned.byte2Byte(bs, ProtocolConstantV206V202404.versionIndex) ; + char[] cs = ("" + ver).toCharArray() ; + StringBuilder vs = new StringBuilder() ; + for(byte i = 0 ; i < cs.length; i++){ + if(i == 0){ + vs.append(cs[i]) ; + }else{ + vs.append(".").append(cs[i]) ; + } + } + return vs.toString() ; + } + */ + +} -- Gitblit v1.8.0