liurunyu
2024-12-02 36268a2ea052f07ab28b9f1d4d6a654bf4fbbd13
修改升级协议数据中的CRC16算法为查表法
4个文件已修改
47 ■■■■■ 已修改文件
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V1/parse/global/GlCreate.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V2/parse/global/GlCreate.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V202404/parse/global/GlCreate.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/CRC16.java 35 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V1/parse/global/GlCreate.java
@@ -93,9 +93,9 @@
    }
    public static byte[] createCrcTail4Ug(byte[] bsNoTail) throws Exception {
        short crc = new CRC16().CRC(bsNoTail, 0, bsNoTail.length -1) ;
        short crc = new CRC16().CRC16_table(bsNoTail, 0, bsNoTail.length -1) ;
        byte[] bytes = new byte[ProtocolConstantV206V1.UG_lenTail] ;
        ByteUtil.short2Bytes_BE(bytes, crc, 0);
        ByteUtil.short2Bytes_LE(bytes, crc, 0);
        bytes[2] = ProtocolConstantV206V1.P_Tail_Byte ;
        return bytes ;
    }
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V2/parse/global/GlCreate.java
@@ -94,9 +94,9 @@
    }
    public static byte[] createCrcTail4Ug(byte[] bsNoTail) throws Exception {
        int crc = new CRC16().CRC(bsNoTail, 0, bsNoTail.length -1) ;
        int crc = new CRC16().CRC16_table(bsNoTail, 0, bsNoTail.length -1) ;
        byte[] bytes = new byte[ProtocolConstantV206V2.UG_lenTail] ;
        ByteUtilUnsigned.short2Bytes_BE(bytes, crc, 0);
        ByteUtilUnsigned.short2Bytes_LE(bytes, crc, 0);
        bytes[2] = ProtocolConstantV206V2.P_Tail_Byte ;
        return bytes ;
    }
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V202404/parse/global/GlCreate.java
@@ -167,9 +167,9 @@
    }
    public static byte[] createCrcTail4Ug(byte[] bsNoTail) throws Exception {
        int crc = new CRC16().CRC(bsNoTail, 0, bsNoTail.length -1) ;
        int crc = new CRC16().CRC16_table(bsNoTail, 0, bsNoTail.length -1) ;
        byte[] bytes = new byte[ProtocolConstantV206V2.UG_lenTail] ;
        ByteUtilUnsigned.short2Bytes_BE(bytes, crc, 0);
        ByteUtilUnsigned.short2Bytes_LE(bytes, crc, 0);
        bytes[2] = ProtocolConstantV206V2.P_Tail_Byte ;
        return bytes ;
    }
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/CRC16.java
@@ -50,9 +50,8 @@
    }
    
    
     private static final int[] crc16_rev_table = new int[]
        {   0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
    private static final int[] crc16_rev_table = new int[]{
        0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
            0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
            0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
            0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
@@ -87,18 +86,36 @@
            };
    /**
     * 16位的CRC值是无符号两字节整数,
     * @param bs 字节数组
     * @param startIndex 开始下标
     * @param endIndex 截止下标
     * @return CRC
     */
    @SuppressWarnings("unused")
    public short CRC16_table(byte[] bs, int startIndex, int endIndex) {
        short x ;
        short crc = (short)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]) ;
            }
        }
        return crc ;
    }
    /**
     * 16位的CRC值 ,查表法
     * @param bs 字节数组
     * @return CRC
     */
    @SuppressWarnings("unused")
    public int CRC_table(byte[] bs) {
        int x ;
        int crc = 0xffff;
    public short CRC16_table(byte[] bs) {
        short x ;
        short crc = (short)0xffff;
        if(bs != null && bs.length > 0){
            for(byte b : bs){
                x = crc ^ b ;
                crc = (crc >> 8) ^ crc16_rev_table[x & 0x00FF];
                x = (short)(crc ^ b) ;
                crc = (short)((crc >> 8) ^ crc16_rev_table[x & 0x00FF]) ;
            }
        }
        return crc ;