| | |
| | | package com.dy.common.mw.protocol.p206V1; |
| | | |
| | | import com.dy.common.mw.channel.tcp.TcpIoSessionAttrIdIsRtuAddr; |
| | | 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 CommonV1 { |
| | | |
| | | |
| | | /** |
| | | * 检查头 |
| | | * @param bs 上行字节数组 |
| | | * @return true是,false否 |
| | | * @return [【0】->true:是本协议P206,false:不是本协议; 【1】->true:是水资源协议,false:是升级协议] |
| | | * @throws Exception 异常 |
| | | */ |
| | | public Boolean isThisProtocolHead(byte[] bs) throws Exception{ |
| | | public Boolean[] isThisProtocolHead(byte[] bs) throws Exception{ |
| | | if(bs == null){ |
| | | return null ; |
| | | }else if(bs.length >= (ProtocolConstantV206V1.ctrlIndex) |
| | | && bs[0] == ProtocolConstantV206V1.P_Head_Byte |
| | | && bs[2] == ProtocolConstantV206V1.P_Head_Byte){ |
| | | //String vs = this.parseVersion(bs) ; |
| | | //return vs.equals(ProtocolConstantV206V1.version); |
| | | return true ; |
| | | return new Boolean[]{true, true} ; |
| | | }else if(bs.length >= (ProtocolConstantV206V1.UG_codeIndex) |
| | | && bs[0] == ProtocolConstantV206V1.UG_P_Head_Byte |
| | | && bs[3] == ProtocolConstantV206V1.UG_P_Head_Byte){ |
| | | return new Boolean[]{true, false} ; |
| | | }else{ |
| | | return false ; |
| | | return new Boolean[]{false, false} ; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 检查头 |
| | | * @param bs 上行字节数组 |
| | | * @throws Exception 异常 |
| | | * 检查协议类型 |
| | | * @param bs 上行字节数组 |
| | | * @return 协议类型 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public void checkHead(byte[] bs) throws Exception{ |
| | | if(bs.length < ProtocolConstantV206V1.onLineDataMinLength |
| | | || bs[0] != ProtocolConstantV206V1.P_Head_Byte |
| | | || bs[2] != ProtocolConstantV206V1.P_Head_Byte){ |
| | | throw new Exception("上行数据帧头不正确!") ; |
| | | public Boolean protocolType_p206TrueUgFalse(byte[] bs){ |
| | | if(bs == null){ |
| | | return null ; |
| | | }else if(bs.length >= (ProtocolConstantV206V1.ctrlIndex) |
| | | && bs[0] == ProtocolConstantV206V1.P_Head_Byte |
| | | && bs[2] == ProtocolConstantV206V1.P_Head_Byte){ |
| | | return true ; |
| | | }else if(bs.length >= (ProtocolConstantV206V1.UG_codeIndex) |
| | | && bs[0] == ProtocolConstantV206V1.UG_P_Head_Byte |
| | | && bs[3] == ProtocolConstantV206V1.UG_P_Head_Byte){ |
| | | return false ; |
| | | }else{ |
| | | return null ; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 检查尾 |
| | | * @param bs 上行字节数组 |
| | |
| | | /** |
| | | * 分析帧长度 |
| | | * @param bs 上行字节数组 |
| | | * @param p206TrueUgFalse 206协议为true,升级协议为false |
| | | * @return 数据长度 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public int parseFrameLen(byte[] bs)throws Exception{ |
| | | int len = ByteUtilUnsigned.byte2Byte(bs, ProtocolConstantV206V1.dataLenIndex) ; |
| | | return len + ProtocolConstantV206V1.lenHead2ctrl + ProtocolConstantV206V1.lenTail ; |
| | | public int parseFrameLen(byte[] bs, boolean p206TrueUgFalse)throws Exception{ |
| | | if(p206TrueUgFalse) { |
| | | int len = ByteUtilUnsigned.byte2Byte(bs, ProtocolConstantV206V1.dataLenIndex); |
| | | return len + ProtocolConstantV206V1.lenHead2ctrl + ProtocolConstantV206V1.lenTail; |
| | | }else{ |
| | | int len = ByteUtilUnsigned.bytes2Short_BE(bs, ProtocolConstantV206V1.UG_dataLenIndex_start); |
| | | return len + ProtocolConstantV206V1.UG_lenHead2Cmd + ProtocolConstantV206V1.UG_lenTail; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 分析用户数据域字节数 |
| | | * 分析用户数据域字节数(默认是非升级协议) |
| | | * @param bs 上行字节数组 |
| | | * @return 数据长度 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public int parseDataLen(byte[] bs)throws Exception{ |
| | | public int parseDataLen4P206(byte[] bs)throws Exception{ |
| | | int len = ByteUtilUnsigned.byte2Byte(bs, ProtocolConstantV206V1.dataLenIndex) ; |
| | | return len + ProtocolConstantV206V1.lenHead2ctrl + ProtocolConstantV206V1.lenTail ; |
| | | return len - ProtocolConstantV206V1.lenCtrl - ProtocolConstantV206V1.lenRtuAddr ; |
| | | } |
| | | |
| | | /** |
| | | * 分析用户数据域字节数(升级协议) |
| | | * @param bs 上行字节数组 |
| | | * @return 数据长度 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public int parseDataLen4Ug(byte[] bs)throws Exception{ |
| | | int len = ByteUtilUnsigned.bytes2Short_LE(bs, ProtocolConstantV206V1.UG_dataLenIndex_start) ; |
| | | return len - ProtocolConstantV206V1.UG_lenCmd - ProtocolConstantV206V1.UG_lenRtuAddr ; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 分析Rtu地址 |
| | | * @param bs 上行字节数组 |
| | | * @param p206TrueUgFalse 206协议为true,升级协议为false |
| | | * @return 控制器地址 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public String parseRtuAddr(byte[] bs)throws Exception{ |
| | | String rtuAddrBCD = "" + ByteUtil.BCD2Long_BE(bs, ProtocolConstantV206V1.rtuAddr1Index_start, ProtocolConstantV206V1.rtuAddr1Index_end) ; |
| | | String rtuAddrStr = "" + ByteUtilUnsigned.bytes2Short_LE(bs, ProtocolConstantV206V1.rtuAddr2Index_start) ; |
| | | while(rtuAddrStr.length() <= 5){ |
| | | rtuAddrStr = "0" + rtuAddrStr ; |
| | | public String parseRtuAddr(byte[] bs, boolean p206TrueUgFalse)throws Exception{ |
| | | if(p206TrueUgFalse){ |
| | | String rtuAddrBCD = "" + ByteUtil.BCD2Long_BE(bs, ProtocolConstantV206V1.rtuAddr1Index_start, ProtocolConstantV206V1.rtuAddr1Index_end) ; |
| | | String rtuAddrStr = "" + ByteUtilUnsigned.bytes2Short_LE(bs, ProtocolConstantV206V1.rtuAddr2Index_start) ; |
| | | while(rtuAddrStr.length() <= 5){ |
| | | rtuAddrStr = "0" + rtuAddrStr ; |
| | | } |
| | | return rtuAddrBCD + rtuAddrStr ; |
| | | }else{ |
| | | return ("" + ByteUtil.BCD2Long_BE(bs, ProtocolConstantV206V1.UG_rtuAddrIndex_start, ProtocolConstantV206V1.UG_rtuAddrIndex_end)) ; |
| | | } |
| | | return rtuAddrBCD + rtuAddrStr ; |
| | | } |
| | | |
| | | |
| | |
| | | * @return 控制器地址 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public String parseRtuAddr(byte[] bs, int index)throws Exception{ |
| | | String rtuAddrBCD = "" + ByteUtil.BCD2Long_BE(bs, index, index + 2) ;//地址是大端模式 |
| | | String rtuAddrStr = "" + ByteUtilUnsigned.bytes2Short_LE(bs, index + 3) ; |
| | | while(rtuAddrStr.length() <= 5){ |
| | | rtuAddrStr = "0" + rtuAddrStr ; |
| | | public String parseRtuAddr(byte[] bs, int index, boolean p206TrueUgFalse)throws Exception{ |
| | | if(p206TrueUgFalse){ |
| | | String rtuAddrBCD = "" + ByteUtil.BCD2Long_BE(bs, index, index + 2) ;//地址是大端模式 |
| | | String rtuAddrStr = "" + ByteUtilUnsigned.bytes2Short_LE(bs, index + 3) ; |
| | | while(rtuAddrStr.length() <= 5){ |
| | | rtuAddrStr = "0" + rtuAddrStr ; |
| | | } |
| | | return rtuAddrBCD + rtuAddrStr ; |
| | | }else{ |
| | | throw new Exception("升级协议无设置地址") ; |
| | | } |
| | | return rtuAddrBCD + rtuAddrStr ; |
| | | } |
| | | |
| | | |
| | |
| | | * @param bs 上行字节数组 |
| | | * @return 功能码 |
| | | */ |
| | | public String parseCode(byte[] bs){ |
| | | return ByteUtil.bytes2Hex(bs, false, ProtocolConstantV206V1.codeIndex, 1) ; |
| | | public String parseCode(byte[] bs, boolean p206TrueUgFalse){ |
| | | if(p206TrueUgFalse) { |
| | | return ByteUtil.bytes2Hex(bs, false, ProtocolConstantV206V1.codeIndex, 1); |
| | | }else{ |
| | | return ByteUtil.bytes2Hex(bs, false, ProtocolConstantV206V1.UG_codeIndex, 2); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 校验和检查 |
| | | * @param bs 上行字节数组 |
| | | * @param p206TrueUgFalse 206协议为true,升级协议为false |
| | | * @return 通过null,未通过返回原因 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public String checkCrc_str(byte[] bs) throws Exception { |
| | | byte crcCompute = (byte)new CRC8_for_2_0().CRC8(bs, ProtocolConstantV206V1.ctrlIndex, bs.length - 3) ; |
| | | byte crcInBs = bs[bs.length - 2] ; |
| | | if(crcCompute == crcInBs){ |
| | | return null ; |
| | | public String checkCrc_str(byte[] bs, boolean p206TrueUgFalse) throws Exception { |
| | | if(p206TrueUgFalse){ |
| | | byte crcCompute = (byte)new CRC8_for_2_0().CRC8(bs, ProtocolConstantV206V1.ctrlIndex, bs.length - 3) ; |
| | | byte crcInBs = bs[bs.length - 2] ; |
| | | if(crcCompute == crcInBs){ |
| | | return null ; |
| | | }else{ |
| | | return "计算CRC是:" + crcCompute + ",上传CRC是" + crcInBs ; |
| | | } |
| | | }else{ |
| | | return "计算CRC是:" + crcCompute + ",上传CRC是" + crcInBs ; |
| | | 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 ; |
| | | } |
| | | } |
| | | } |
| | | |