AppFuncLib.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * @Author : ChenJie
  3. * @Date : 2022-01-21 09:28:20
  4. * @Version : V3.0
  5. * @LastEditors : ChenJie
  6. * @LastEditTime : 2022-01-27 18:24:36
  7. * @Description : file content
  8. * @FilePath : \S32K144_BLE\src\AppFuncLib.c
  9. */
  10. /*
  11. * AppFuncLib.c
  12. *应用层函数库
  13. * Created on: 2022年1月21日
  14. * Author: QiXiang_CHENJIE
  15. */
  16. #include "AppFuncLib.h"
  17. uint16 ATstrdel(char *str)
  18. {
  19. char *p = str;
  20. bool flag = false;
  21. while (*str)
  22. {
  23. if (*str > 0x20)
  24. {
  25. *(p) = *str;
  26. p = p + 1;
  27. flag = false;
  28. }
  29. else
  30. {
  31. if (!flag)
  32. {
  33. *(p) = ',';
  34. p = p + 1;
  35. flag = true;
  36. }
  37. }
  38. str++;
  39. }
  40. *p = '\0';
  41. return 0;
  42. }
  43. uint16 mstrlen(const char *s)
  44. {
  45. uint16 out = 0;
  46. const char *ss = s;
  47. while (*ss)
  48. ss++;
  49. out = (ss - s);
  50. return out;
  51. }
  52. int mstrncmp(const char *s1, const char *s2, int n)
  53. {
  54. const unsigned char *c1 = (const unsigned char *)s1;
  55. const unsigned char *c2 = (const unsigned char *)s2;
  56. unsigned char ch;
  57. int d = 0;
  58. while (n--)
  59. {
  60. d = (int)(ch = *c1++) - (int)*c2++;
  61. if (d || !ch)
  62. break;
  63. }
  64. return d;
  65. }
  66. unsigned char HexToChar(unsigned char bHex)
  67. {
  68. if ((bHex >= 0) && (bHex <= 9))
  69. bHex += 0x30;
  70. else if ((bHex >= 10) && (bHex <= 15)) //大写字母
  71. bHex += 0x37;
  72. else
  73. bHex = 0xff;
  74. return bHex;
  75. }
  76. unsigned char CharToHex(unsigned char bChar)
  77. {
  78. if ((bChar >= 0x30) && (bChar <= 0x39))
  79. bChar -= 0x30;
  80. else if ((bChar >= 0x41) && (bChar <= 0x46)) //大写字母
  81. bChar -= 0x37;
  82. else if ((bChar >= 0x61) && (bChar <= 0x66)) //小写字母
  83. bChar -= 0x57;
  84. else
  85. bChar = 0xff;
  86. return bChar;
  87. }
  88. uint8 AtStrCompare(const char *a, const char *b)
  89. {
  90. uint8 out = 1;
  91. while (1)
  92. {
  93. if (*a == '\0' || *b == '\0') //判断其中是否有字符串结束
  94. {
  95. if (strlen(a) == strlen(b))
  96. {
  97. out = 1;
  98. break;
  99. }
  100. else
  101. {
  102. out = 0;
  103. break;
  104. }
  105. }
  106. else
  107. {
  108. if (*a != *b)
  109. {
  110. out = 0;
  111. break;
  112. }
  113. else if (*a == '=' && *b == '=')
  114. {
  115. out = 1;
  116. break;
  117. }
  118. }
  119. a++;
  120. b++;
  121. }
  122. return out;
  123. }
  124. unsigned short CRC16_Modbus ( unsigned char *pdata, int len)
  125. {
  126. unsigned short crc=0xFFFF;
  127. int i, j;
  128. for ( j=0; j<len;j++)
  129. {
  130. crc=crc^pdata[j];
  131. for ( i=0; i<8; i++)
  132. {
  133. if( ( crc&0x0001) >0)
  134. {
  135. crc=crc>>1;
  136. crc=crc^ 0xa001;
  137. }
  138. else
  139. crc=crc>>1;
  140. }
  141. }
  142. return crc;
  143. }
  144. uint16 crc_chk(uint8 *data, uint8 length)
  145. {
  146. uint8 j;
  147. uint16 reg_crc = 0xFFFF;
  148. while (length--)
  149. {
  150. reg_crc ^= *data++;
  151. for (j = 0; j < 8; j++)
  152. {
  153. if (reg_crc & 0x01)
  154. {
  155. reg_crc = (reg_crc >> 1) ^ 0xA001;
  156. }
  157. else
  158. {
  159. reg_crc = reg_crc >> 1;
  160. }
  161. }
  162. }
  163. return reg_crc;
  164. }