/app/src/main/java/com/viomi/oven/device/CRC8.java

https://bitbucket.org/viomi_team/viomi_oven · Java · 122 lines · 45 code · 9 blank · 68 comment · 6 complexity · 0263827509de9d8551ae6e19d9190515 MD5 · raw file

  1. package com.viomi.oven.device;
  2. import com.viomi.oven.util.LogUtils;
  3. /**
  4. * CRC16相关计算 Modbus
  5. * encode: utf-8
  6. */
  7. public class CRC8 {
  8. static byte[] crc8_tab = {(byte) 0, (byte) 94, (byte) 188, (byte) 226, (byte) 97, (byte) 63, (byte) 221, (byte) 131, (byte) 194, (byte) 156, (byte) 126, (byte) 32, (byte) 163, (byte) 253, (byte) 31, (byte) 65, (byte) 157, (byte) 195, (byte) 33, (byte) 127, (byte) 252, (byte) 162, (byte) 64, (byte) 30, (byte) 95, (byte) 1, (byte) 227, (byte) 189, (byte) 62, (byte) 96, (byte) 130, (byte) 220, (byte) 35, (byte) 125, (byte) 159, (byte) 193, (byte) 66, (byte) 28, (byte) 254, (byte) 160, (byte) 225, (byte) 191, (byte) 93, (byte) 3, (byte) 128, (byte) 222, (byte) 60, (byte) 98, (byte) 190, (byte) 224, (byte) 2, (byte) 92, (byte) 223, (byte) 129, (byte) 99, (byte) 61, (byte) 124, (byte) 34, (byte) 192, (byte) 158, (byte) 29, (byte) 67, (byte) 161, (byte) 255, (byte) 70, (byte) 24,
  9. (byte) 250, (byte) 164, (byte) 39, (byte) 121, (byte) 155, (byte) 197, (byte) 132, (byte) 218, (byte) 56, (byte) 102, (byte) 229, (byte) 187, (byte) 89, (byte) 7, (byte) 219, (byte) 133, (byte) 103, (byte) 57, (byte) 186, (byte) 228, (byte) 6, (byte) 88, (byte) 25, (byte) 71, (byte) 165, (byte) 251, (byte) 120, (byte) 38, (byte) 196, (byte) 154, (byte) 101, (byte) 59, (byte) 217, (byte) 135, (byte) 4, (byte) 90, (byte) 184, (byte) 230, (byte) 167, (byte) 249, (byte) 27, (byte) 69, (byte) 198, (byte) 152, (byte) 122, (byte) 36, (byte) 248, (byte) 166, (byte) 68, (byte) 26, (byte) 153, (byte) 199, (byte) 37, (byte) 123, (byte) 58, (byte) 100, (byte) 134, (byte) 216, (byte) 91, (byte) 5, (byte) 231, (byte) 185, (byte) 140, (byte) 210, (byte) 48, (byte) 110, (byte) 237,
  10. (byte) 179, (byte) 81, (byte) 15, (byte) 78, (byte) 16, (byte) 242, (byte) 172, (byte) 47, (byte) 113, (byte) 147, (byte) 205, (byte) 17, (byte) 79, (byte) 173, (byte) 243, (byte) 112, (byte) 46, (byte) 204, (byte) 146, (byte) 211, (byte) 141, (byte) 111, (byte) 49, (byte) 178, (byte) 236, (byte) 14, (byte) 80, (byte) 175, (byte) 241, (byte) 19, (byte) 77, (byte) 206, (byte) 144, (byte) 114, (byte) 44, (byte) 109, (byte) 51, (byte) 209, (byte) 143, (byte) 12, (byte) 82, (byte) 176, (byte) 238, (byte) 50, (byte) 108, (byte) 142, (byte) 208, (byte) 83, (byte) 13, (byte) 239, (byte) 177, (byte) 240, (byte) 174, (byte) 76, (byte) 18, (byte) 145, (byte) 207, (byte) 45, (byte) 115, (byte) 202, (byte) 148, (byte) 118, (byte) 40, (byte) 171, (byte) 245, (byte) 23, (byte) 73, (byte) 8,
  11. (byte) 86, (byte) 180, (byte) 234, (byte) 105, (byte) 55, (byte) 213, (byte) 139, (byte) 87, (byte) 9, (byte) 235, (byte) 181, (byte) 54, (byte) 104, (byte) 138, (byte) 212, (byte) 149, (byte) 203, (byte) 41, (byte) 119, (byte) 244, (byte) 170, (byte) 72, (byte) 22, (byte) 233, (byte) 183, (byte) 85, (byte) 11, (byte) 136, (byte) 214, (byte) 52, (byte) 106, (byte) 43, (byte) 117, (byte) 151, (byte) 201, (byte) 74, (byte) 20, (byte) 246, (byte) 168, (byte) 116, (byte) 42, (byte) 200, (byte) 150, (byte) 21, (byte) 75, (byte) 169, (byte) 247, (byte) 182, (byte) 232, (byte) 10, (byte) 84, (byte) 215, (byte) 137, (byte) 107, 53};
  12. /**
  13. * 计算数组的CRC8校验值
  14. *
  15. * @param data 需要计算的数组
  16. * @return CRC8校验值
  17. */
  18. public static byte calcCrc8(byte[] data) {
  19. return calcCrc8(data, 0, data.length, (byte) 0);
  20. }
  21. /**
  22. * 计算CRC8校验值
  23. *
  24. * @param data 数据
  25. * @param offset 起始位置
  26. * @param len 长度
  27. * @return 校验值
  28. */
  29. public static byte calcCrc8(byte[] data, int offset, int len) {
  30. return calcCrc8(data, offset, len, (byte) 0);
  31. }
  32. /**
  33. * 计算CRC8校验值
  34. *
  35. * @param data 数据
  36. * @param offset 起始位置q
  37. * @param len 长度
  38. * @param preval 之前的校验值
  39. * @return 校验值
  40. */
  41. public static byte calcCrc8(byte[] data, int offset, int len, byte preval) {
  42. byte ret = preval;
  43. for (int i = offset; i < (offset + len); ++i) {
  44. ret = crc8_tab[(0x00ff & (ret ^ data[i]))];
  45. }
  46. return ret;
  47. }
  48. // 测试
  49. public static void main(String[] args) {
  50. byte crc = CRC8.calcCrc8(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
  51. System.out.println("" + Integer.toHexString(0x00ff & crc));
  52. }
  53. /*
  54. //u8_temp = cal_crc8((UINT8*) &Uart0_TAp_RxData, Uart0_TAp_RxData.len+2 );
  55. unsigned char cal_crc8(unsigned char *ptr, unsigned char len)
  56. {
  57. unsigned char crc = 0;
  58. unsigned char i;
  59. while(len--!=0)
  60. {
  61. for(i=0x80;i!=0;i>>=1)
  62. {
  63. if(crc&0x80)
  64. {
  65. crc <<=1;
  66. crc^=0x31;
  67. }
  68. else
  69. crc<<=1;
  70. if(*ptr&i)
  71. crc^=0x31;
  72. }
  73. ptr++;
  74. }
  75. return crc;
  76. }
  77. */
  78. public static int findCrc(byte[] data) {
  79. //int CRC = 0;
  80. //int genPoly = 0x31;
  81. //for (int i = 0; i < data.length; i++) {
  82. // CRC ^= data[i];
  83. // for (int j = 0; j < 8; j++) {
  84. // if ((CRC & 0x80) != 0) {
  85. // CRC = (CRC << 1) ^ genPoly;
  86. // } else {
  87. // CRC <<= 1;
  88. // }
  89. // }
  90. //}
  91. //CRC &= 0xff;//保证CRC余码输出为2字节。
  92. //LogUtils.d("CRC8" + " the findCrc is:" + CRC);
  93. //return CRC;
  94. return findCrc(data,0,data.length);
  95. }
  96. public static int findCrc(byte[] data, int offset, int len) {
  97. int CRC = 0;
  98. int genPoly = 0x31;
  99. for (int i = 0; i < len; i++) {
  100. CRC ^= data[i + offset];
  101. for (int j = 0; j < 8; j++) {
  102. if ((CRC & 0x80) != 0) {
  103. CRC = (CRC << 1) ^ genPoly;
  104. } else {
  105. CRC <<= 1;
  106. }
  107. }
  108. }
  109. CRC &= 0xff;//保证CRC余码输出为2字节。
  110. LogUtils.d("CRC8" + " the findCrc is:" + CRC);
  111. return CRC;
  112. }
  113. }