| | |
| | | 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 { |
| | | |
| | | /** |
| | | * 在Io会话中设置协议名称及版本号 |
| | | * @param ioSession 会话 |
| | | */ |
| | | public void setThisProtocolArr2IoSession(IoSession ioSession){ |
| | | ioSession.setAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolName, ProtocolConstantV206V1.protocolName) ; |
| | | ioSession.setAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolName, ProtocolConstantV206V1.protocolVer) ; |
| | | } |
| | | /** |
| | | * 检查头 |
| | | * @param bs 上行字节数组 |
| | |
| | | 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 ; |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * 检查头 |
| | | * 检查协议类型 |
| | | * @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 ; |
| | | } |
| | | } |
| | | |
| | |
| | | * @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 ; |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 分析功能码 |
| | | * @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,未通过返回原因 |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 校验和检查 |
| | | * @param bs 上行字节数组 |
| | | * @param p202404TrueUgFalse 202404协议为true,升级协议为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 + ",上传CRC是" + 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 + ",上传CRC是" + crcInBs ; |
| | | } |
| | | } |
| | | } |
| | | |
| | | /* |
| | | 构造控制域 |
| | |
| | | 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 ; |
| | | } |
| | |
| | | }; |
| | | } |
| | | |
| | | public static String ctrlDevType(String hex){ |
| | | return switch (hex) { |
| | | case "01" -> "测控一体阀"; |
| | | case "02" -> "表阀一体机"; |
| | | case "57" -> "井电控制器"; |
| | | default -> "未知"; |
| | | }; |
| | | } |
| | | |
| | | /* |
| | | * 分析版本号 |
| | | * @param bs 上行字节数组 |