| | |
| | | |
| | | import com.dy.common.util.ByteUtil; |
| | | import com.dy.common.util.ByteUtilUnsigned; |
| | | import com.dy.common.util.CRC8_for_2_0; |
| | | |
| | | |
| | | public class CommonV1_0_1 { |
| | |
| | | |
| | | |
| | | /** |
| | | * 分析数据字节数 |
| | | * 分析帧长度 |
| | | * @param bs 上行字节数组 |
| | | * @return 数据长度 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public int parseFrameLen(byte[] bs)throws Exception{ |
| | | int len = ByteUtilUnsigned.byte2Byte(bs, ProtocolConstantV206V1_0_0.dataLenIndex) ; |
| | | return len + ProtocolConstantV206V1_0_0.lenHead2ctrl + ProtocolConstantV206V1_0_0.lenTail ; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 分析用户数据域字节数 |
| | | * @param bs 上行字节数组 |
| | | * @return 数据长度 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public int parseDataLen(byte[] bs)throws Exception{ |
| | | return ByteUtilUnsigned.bytes2Short_BE(bs, ProtocolConstantV206V1_0_0.dataLenIndex) ; |
| | | int len = ByteUtilUnsigned.byte2Byte(bs, ProtocolConstantV206V1_0_0.dataLenIndex) ; |
| | | return len + ProtocolConstantV206V1_0_0.lenHead2ctrl + ProtocolConstantV206V1_0_0.lenTail ; |
| | | } |
| | | |
| | | |
| | |
| | | public String parseRtuAddr(byte[] bs)throws Exception{ |
| | | String rtuAddrBCD = "" + ByteUtil.BCD2Long_BE(bs, ProtocolConstantV206V1_0_0.rtuAddr1Index_start, ProtocolConstantV206V1_0_0.rtuAddr1Index_end) ; |
| | | String rtuAddrStr = "" + ByteUtilUnsigned.bytes2Short_BE(bs, ProtocolConstantV206V1_0_0.rtuAddr2Index_start) ; |
| | | while(rtuAddrStr.length() < 6){ |
| | | while(rtuAddrStr.length() < 4){ |
| | | rtuAddrStr = "0" + rtuAddrStr ; |
| | | } |
| | | return rtuAddrBCD + rtuAddrStr ; |
| | |
| | | return ByteUtil.bytes2Hex(bs, false, ProtocolConstantV206V1_0_0.codeIndex, 1) ; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 校验和检查 |
| | | * @param bs 上行字节数组 |
| | | * @return 通过true,未通过false |
| | | */ |
| | | public boolean checkCrc(byte[] bs) { |
| | | byte he = 0 ; |
| | | for(int i = 0 ; i <= bs.length - 3 ; i++){ |
| | | he = (byte)(he + bs[i]) ; |
| | | } |
| | | return bs[bs.length - 2] == he ; |
| | | } |
| | | |
| | | /** |
| | | * 校验和检查 |
| | | * @param bs 上行字节数组 |
| | |
| | | * @throws Exception 异常 |
| | | */ |
| | | public String checkCrc_str(byte[] bs) throws Exception { |
| | | byte[] he = new byte[1] ; |
| | | for(int i = 0 ; i <= bs.length - 3 ; i++){ |
| | | he[0] = (byte)(he[0] + bs[i]) ; |
| | | } |
| | | short hes = ByteUtilUnsigned.byte2Byte(he, 0); |
| | | short heOrg = ByteUtilUnsigned.byte2Byte(bs, bs.length - 2); |
| | | if(hes == heOrg){ |
| | | byte crcCompute = (byte)new CRC8_for_2_0().CRC8(bs, ProtocolConstantV206V1_0_0.ctrlIndex, bs.length - 3) ; |
| | | byte crcInBs = bs[bs.length - 2] ; |
| | | if(crcCompute == crcInBs){ |
| | | return null ; |
| | | }else{ |
| | | return "计算校验和是:" + hes + ",上传校验和是" + heOrg ; |
| | | return "计算CRC是:" + crcCompute + ",上传CRC是" + crcInBs ; |
| | | } |
| | | } |
| | | |
| | |
| | | D7 D6 D5~D4 D3~D0 |
| | | 传输方向位 DIR 拆分标志位 DIV 帧计数位 FCB 功能码 |
| | | */ |
| | | public byte createCtrl(byte funcCode){ |
| | | byte b = 0 ; |
| | | public byte createCtrl(byte dir, byte funcCode){ |
| | | byte b = dir;//(byte)0x80//控制域:DIR=1,表示此帧报文是由终端发出的上行报文; |
| | | b = (byte)(b | funcCode) ; |
| | | //FCB == 3 |
| | | b = (byte)(b | (byte)60) ; |
| | | b = (byte)(b | (byte)0x18) ; |
| | | //DIV = 0 |
| | | //DIR = 0 |
| | | return b ; |