From bbe823785e44f1086fa160f01715eba968dc8c0f Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期一, 02 十二月 2024 16:16:47 +0800 Subject: [PATCH] 修改升级协议数据中CRC验证赋值部分 --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/CRC16.java | 24 ++++++++++++++++++------ 1 files changed, 18 insertions(+), 6 deletions(-) diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/CRC16.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/CRC16.java index 61e9eca..307e1af 100644 --- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/CRC16.java +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/CRC16.java @@ -1,6 +1,8 @@ package com.dy.common.util; +import com.dy.common.mw.protocol.p206V1.ProtocolConstantV206V1; + @SuppressWarnings("unused") public final class CRC16 { @@ -93,13 +95,13 @@ * @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 ; @@ -138,6 +140,16 @@ System.arraycopy(new byte[] {(byte)(c - ((c / 256) * 256)) }, 0, crc, 1, 1); return crc; } - + + public static void main(String[] args) throws Exception { + int s = 62430 ; + //String hex = "AA0018AA16015301150599800001100000014A005D4D5D4D00004000AA7E16" ; + String hex = "AA0018AA16015301150599800001100000014A005D4D5D4D00004000" ; + byte[] bs = ByteUtil.hex2Bytes(hex) ; + 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)) ; + } } -- Gitblit v1.8.0