| | |
| | | } |
| | | |
| | | 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 ; |
| | | } |
| | |
| | | } |
| | | |
| | | 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 ; |
| | | } |
| | |
| | | } |
| | | |
| | | 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 ; |
| | | } |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | 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, |
| | |
| | | }; |
| | | |
| | | /** |
| | | * 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 ; |