| | |
| | | package com.dy.common.mw.protocol.p206V2; |
| | | |
| | | import com.dy.common.mw.channel.tcp.TcpIoSessionAttrIdIsRtuAddr; |
| | | import com.dy.common.mw.protocol.p206V1.ProtocolConstantV206V1; |
| | | 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 CommonV2 { |
| | | |
| | | /** |
| | | * 在Io会话中设置协议名称及版本号 |
| | | * @param ioSession 会话 |
| | | */ |
| | | public void setThisProtocolArr2IoSession(IoSession ioSession){ |
| | | ioSession.setAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolName, ProtocolConstantV206V1.protocolName) ; |
| | | ioSession.setAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolName, ProtocolConstantV206V1.protocolVer) ; |
| | | } |
| | | /** |
| | | * 检查头 |
| | | * @param bs 上行字节数组 |
| | |
| | | && bs[ProtocolConstantV206V2.headFlag1Index] == ProtocolConstantV206V2.P_Head_Byte |
| | | && bs[ProtocolConstantV206V2.headFlag2Index] == ProtocolConstantV206V2.P_Head_Byte){ |
| | | Short vs = this.parseVersion(bs) ; |
| | | if(vs.shortValue() == ProtocolConstantV206V2.protocolVer.shortValue()){ |
| | | if(vs.shortValue() == ProtocolConstantV206V2.protocolVer){ |
| | | return new Boolean[]{true, true}; |
| | | }else{ |
| | | return new Boolean[]{false, true}; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 分析功能码 |
| | | * @param bs 上行字节数组 |
| | | * @return 功能码 |
| | | */ |
| | | 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 上行字节数组 |
| | | * @return 通过null,未通过返回原因 |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 校验和检查 |
| | | * @param bs 上行字节数组 |
| | | * @param p206TrueUgFalse 206协议为true,升级协议为false |
| | | * @return 通过null,未通过返回原因 |
| | | * @throws Exception 异常 |
| | | */ |
| | | 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{ |
| | | 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 ; |
| | | } |
| | | } |
| | | } |
| | | /* |
| | | 构造控制域 |
| | | D7 D6 D5~D4 D3~D0 |