修改升级数据中的CRC16验证算法,注意其是大端模块,与其他部分不一样
| | |
| | | } |
| | | |
| | | public static byte[] createCrcTail4Ug(byte[] bsNoTail) throws Exception { |
| | | short crc = new CRC16().CRC16_table(bsNoTail, 0, bsNoTail.length -1) ; |
| | | int crc = new CRC16().CRC16_table(bsNoTail, 0, bsNoTail.length -1) ; |
| | | byte[] crcBs = new byte[4] ; |
| | | ByteUtil.int2Bytes_BE(crcBs, crc, 0); |
| | | byte[] bytes = new byte[ProtocolConstantV206V1.UG_lenTail] ; |
| | | ByteUtil.short2Bytes_LE(bytes, crc, 0); |
| | | bytes[0] = crcBs[3] ; |
| | | bytes[1] = crcBs[4] ; |
| | | bytes[2] = ProtocolConstantV206V1.P_Tail_Byte ; |
| | | return bytes ; |
| | | } |
| | |
| | | package com.dy.common.mw.protocol.p206V2.parse.global; |
| | | |
| | | import com.dy.common.mw.protocol.p206V1.ProtocolConstantV206V1; |
| | | import com.dy.common.mw.protocol.p206V2.ProtocolConstantV206V2; |
| | | import com.dy.common.util.*; |
| | | |
| | |
| | | |
| | | public static byte[] createCrcTail4Ug(byte[] bsNoTail) throws Exception { |
| | | int crc = new CRC16().CRC16_table(bsNoTail, 0, bsNoTail.length -1) ; |
| | | byte[] crcBs = new byte[4] ; |
| | | ByteUtil.int2Bytes_BE(crcBs, crc, 0); |
| | | byte[] bytes = new byte[ProtocolConstantV206V2.UG_lenTail] ; |
| | | ByteUtilUnsigned.short2Bytes_LE(bytes, crc, 0); |
| | | bytes[2] = ProtocolConstantV206V2.P_Tail_Byte ; |
| | | bytes[0] = crcBs[3] ; |
| | | bytes[1] = crcBs[4] ; |
| | | bytes[2] = ProtocolConstantV206V1.P_Tail_Byte ; |
| | | return bytes ; |
| | | } |
| | | } |
| | |
| | | package com.dy.common.mw.protocol.p206V202404.parse.global; |
| | | |
| | | import com.dy.common.mw.protocol.p206V1.ProtocolConstantV206V1; |
| | | import com.dy.common.mw.protocol.p206V2.ProtocolConstantV206V2; |
| | | import com.dy.common.mw.protocol.p206V202404.CommonV202404; |
| | | import com.dy.common.mw.protocol.p206V202404.ProtocolConstantV206V202404; |
| | | import com.dy.common.util.*; |
| | |
| | | } |
| | | |
| | | public static void createLen(byte[] bsNoTail) throws Exception { |
| | | int len = bsNoTail.length - ProtocolConstantV206V1.lenHead2ctrl; |
| | | bsNoTail[ProtocolConstantV206V1.dataLenIndex] = (byte)len ; |
| | | int len = bsNoTail.length - ProtocolConstantV206V202404.lenHead2ctrl; |
| | | bsNoTail[ProtocolConstantV206V202404.dataLenIndex] = (byte)len ; |
| | | } |
| | | |
| | | public static void createLen4PUg(byte[] bytes) throws Exception { |
| | |
| | | } |
| | | |
| | | public static byte[] createCrcTail(byte[] bsNoTail) throws Exception { |
| | | int crc = new CRC8_for_2_0().CRC8(bsNoTail, ProtocolConstantV206V1.ctrlIndex, bsNoTail.length -1) ; |
| | | byte[] bytes = new byte[ProtocolConstantV206V1.lenTail] ; |
| | | int crc = new CRC8_for_2_0().CRC8(bsNoTail, ProtocolConstantV206V202404.ctrlIndex, bsNoTail.length -1) ; |
| | | byte[] bytes = new byte[ProtocolConstantV206V202404.lenTail] ; |
| | | bytes[0] = (byte)crc ; |
| | | bytes[1] = ProtocolConstantV206V1.P_Tail_Byte ; |
| | | bytes[1] = ProtocolConstantV206V202404.P_Tail_Byte ; |
| | | return bytes ; |
| | | } |
| | | |
| | | public static byte[] createCrcTail4Ug(byte[] bsNoTail) throws Exception { |
| | | int crc = new CRC16().CRC16_table(bsNoTail, 0, bsNoTail.length -1) ; |
| | | byte[] bytes = new byte[ProtocolConstantV206V2.UG_lenTail] ; |
| | | ByteUtilUnsigned.short2Bytes_LE(bytes, crc, 0); |
| | | bytes[2] = ProtocolConstantV206V2.P_Tail_Byte ; |
| | | byte[] crcBs = new byte[4] ; |
| | | ByteUtil.int2Bytes_BE(crcBs, crc, 0); |
| | | byte[] bytes = new byte[ProtocolConstantV206V202404.UG_lenTail] ; |
| | | bytes[0] = crcBs[3] ; |
| | | bytes[1] = crcBs[4] ; |
| | | bytes[2] = ProtocolConstantV206V202404.P_Tail_Byte ; |
| | | return bytes ; |
| | | } |
| | | |
| | |
| | | * @return CRC |
| | | */ |
| | | @SuppressWarnings("unused") |
| | | public short CRC16_table(byte[] bs, int startIndex, int endIndex) { |
| | | short x ; |
| | | short crc = (short)0xffff; |
| | | public int CRC16_table(byte[] bs, int startIndex, int endIndex) { |
| | | int x ; |
| | | int crc = 0xffff; |
| | | if(bs != null && bs.length >= endIndex + 1){ |
| | | for (int i = startIndex ; i <= endIndex; i++){ |
| | | x = (short)(crc ^ bs[i]) ; |
| | | crc = (short)((crc >> 8) ^ crc16_rev_table[x & 0x00FF]) ; |
| | | x = (crc ^ bs[i]) ; |
| | | crc = ((crc >> 8) ^ crc16_rev_table[x & 0x00FF]) ; |
| | | } |
| | | } |
| | | return crc ; |
| | |
| | | } |
| | | |
| | | public static void main(String[] args) throws Exception { |
| | | //String hex = "AA0024AA |
| | | // 16015301 |
| | | // 15059980 |
| | | // 00011000 |
| | | // 00014A00 |
| | | // 5D4D5D4D |
| | | // 00004000 |
| | | // AAC616" ; |
| | | int s = 62430 ; |
| | | //String hex = "AA0018AA16015301150599800001100000014A005D4D5D4D00004000AA7E16" ; |
| | | String hex = "AA0018AA16015301150599800001100000014A005D4D5D4D00004000" ; |
| | | byte[] bs = ByteUtil.hex2Bytes(hex) ; |
| | | for(int i = 0 ; i < bs.length - 2 ; i++){ |
| | | short crc = new CRC16().CRC16_table(bs, i , bs.length - 1) ; |
| | | byte[] bytes = new byte[ProtocolConstantV206V1.UG_lenTail] ; |
| | | ByteUtil.short2Bytes_LE(bytes, crc, 0); |
| | | bytes[2] = ProtocolConstantV206V1.P_Tail_Byte ; |
| | | System.out.println(ByteUtil.bytes2Hex(bytes, true)) ; |
| | | } |
| | | // System.out.println("==============================") ; |
| | | // for(int i = 4 ; i < bs.length ; i++){ |
| | | // short crc = new CRC16().CRC16_table(bs, 0 , i) ; |
| | | // byte[] bytes = new byte[ProtocolConstantV206V1.UG_lenTail] ; |
| | | // ByteUtil.short2Bytes_LE(bytes, crc, 0); |
| | | // bytes[2] = ProtocolConstantV206V1.P_Tail_Byte ; |
| | | // System.out.println(ByteUtil.bytes2Hex(bytes, true)) ; |
| | | // } |
| | | int crc = new CRC16().CRC16_table(bs, 0 , bs.length - 1) ; |
| | | byte[] crcBs = new byte[4] ; |
| | | ByteUtil.int2Bytes_BE(crcBs, crc, 0); |
| | | System.out.println(ByteUtil.bytes2Hex(crcBs, true)) ; |
| | | } |
| | | |
| | | } |