liurunyu
2024-12-02 67c9b8c40d8b89408e522bc6df7347992dcdd245
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 {
   
@@ -138,6 +140,33 @@
      System.arraycopy(new byte[] {(byte)(c - ((c / 256) * 256)) }, 0, crc, 1, 1);
      return crc;
   }
   public static void main(String[] args) throws Exception {
      //String hex = "AA0024AA
      // 16015301
      // 15059980
      // 00011000
      // 00014A00
      // 5D4D5D4D
      // 00004000
      // AAC616" ;
      String hex = "AA0024AA16015301150599800001100000014A005D4D5D4D00004000" ;
      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)) ;
      }
   }
}