AppFuncLib.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * @Author: chenjie
  3. * @Date: 2022-06-06
  4. * @LastEditTime: 2022-10-27
  5. * @LastEditors: chenjie
  6. * @Description:
  7. * @FilePath: \S32K146_4G\code\app\lib\AppFuncLib.c
  8. * Copyright (c) 2022 by chenjie, All Rights Reserved.
  9. */
  10. #include "AppFuncLib.h"
  11. #include "AppGlobalVar.h"
  12. #include "Hal_Fls.h"
  13. #include "AppTaskUart1.h"
  14. /**
  15. * @brief : 获取故障码函数,从故障数组中获取故障码,并将之前的故障码向前移动
  16. * @param {UINT16} *ErrorArray
  17. * @param {UINT8} Errorlen
  18. * @return {*}
  19. */
  20. uint16 GetErrorNum(uint16 *ErrorArray, uint8 Errorlen)
  21. {
  22. uint16 OutNum;
  23. OutNum = *(ErrorArray);
  24. for (uint8 i = 0; i < Errorlen - 1; i++)
  25. {
  26. *(ErrorArray + i) = *(ErrorArray + i + 1);
  27. if (*(ErrorArray + i + 1) == 0)
  28. break;
  29. }
  30. return OutNum;
  31. }
  32. uint8 PutErrorNum(uint16 *ErrorArray, uint8 Errorlen, uint16 ErrorNum)
  33. {
  34. for (uint8 i = 0; i < Errorlen; i++)
  35. {
  36. if (*(ErrorArray + i) == 0)
  37. {
  38. *(ErrorArray + i) = ErrorNum;
  39. return 0;
  40. }
  41. else
  42. {
  43. if (*(ErrorArray + i) == ErrorNum)
  44. {
  45. return 1;
  46. }
  47. else
  48. {
  49. continue;
  50. }
  51. }
  52. }
  53. return 2;
  54. }
  55. uint16 ATstrdel(char *str)
  56. {
  57. char *p = str;
  58. bool flag = false;
  59. while (*str)
  60. {
  61. if (*str > 0x20)
  62. {
  63. *(p) = *str;
  64. p = p + 1;
  65. flag = false;
  66. }
  67. else
  68. {
  69. if (!flag)
  70. {
  71. *(p) = ',';
  72. p = p + 1;
  73. flag = true;
  74. }
  75. }
  76. str++;
  77. }
  78. *p = '\0';
  79. return 0;
  80. }
  81. uint16 mstrlen(const char *s)
  82. {
  83. uint16 out = 0;
  84. const char *ss = s;
  85. while (*ss)
  86. ss++;
  87. out = (ss - s);
  88. return out;
  89. }
  90. int mstrncmp(const char *s1, const char *s2, int n)
  91. {
  92. const unsigned char *c1 = (const unsigned char *)s1;
  93. const unsigned char *c2 = (const unsigned char *)s2;
  94. unsigned char ch;
  95. int d = 0;
  96. while (n--)
  97. {
  98. d = (int)(ch = *c1++) - (int)*c2++;
  99. if (d || !ch)
  100. break;
  101. }
  102. return d;
  103. }
  104. unsigned char HexToChar(unsigned char bHex)
  105. {
  106. if ((bHex >= 0) && (bHex <= 9))
  107. bHex += 0x30;
  108. else if ((bHex >= 10) && (bHex <= 15)) //大写字母
  109. bHex += 0x37;
  110. else
  111. bHex = 0xff;
  112. return bHex;
  113. }
  114. unsigned char CharToHex(unsigned char bChar)
  115. {
  116. if ((bChar >= 0x30) && (bChar <= 0x39))
  117. bChar -= 0x30;
  118. else if ((bChar >= 0x41) && (bChar <= 0x46)) //大写字母
  119. bChar -= 0x37;
  120. else if ((bChar >= 0x61) && (bChar <= 0x66)) //小写字母
  121. bChar -= 0x57;
  122. else
  123. bChar = 0xff;
  124. return bChar;
  125. }
  126. uint8 AtStrCompare(const char *a, const char *b)
  127. {
  128. uint8 out = 1;
  129. while (1)
  130. {
  131. if (*a == '\0' || *b == '\0') //判断其中是否有字符串结束
  132. {
  133. if (strlen(a) == strlen(b))
  134. {
  135. out = 1;
  136. break;
  137. }
  138. else
  139. {
  140. out = 0;
  141. break;
  142. }
  143. }
  144. else
  145. {
  146. if (*a != *b)
  147. {
  148. out = 0;
  149. break;
  150. }
  151. else if (*a == '=' && *b == '=')
  152. {
  153. out = 1;
  154. break;
  155. }
  156. }
  157. a++;
  158. b++;
  159. }
  160. return out;
  161. }
  162. unsigned short CRC16_Modbus(unsigned char *pdata, int len)
  163. {
  164. unsigned short crc = 0xFFFF;
  165. int i, j;
  166. for (j = 0; j < len; j++)
  167. {
  168. crc = crc ^ pdata[j];
  169. for (i = 0; i < 8; i++)
  170. {
  171. if ((crc & 0x0001) > 0)
  172. {
  173. crc = crc >> 1;
  174. crc = crc ^ 0xa001;
  175. }
  176. else
  177. crc = crc >> 1;
  178. }
  179. }
  180. return crc;
  181. }
  182. char *Myitoa(int value, char *result, int base)
  183. {
  184. // check that the base if valid
  185. if (base < 2 || base > 36)
  186. {
  187. *result = '\0';
  188. return result;
  189. }
  190. char *ptr = result, *ptr1 = result, tmp_char;
  191. int tmp_value;
  192. do
  193. {
  194. tmp_value = value;
  195. value /= base;
  196. *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)];
  197. } while (value);
  198. // Apply negative sign
  199. if (tmp_value < 0)
  200. *ptr++ = '-';
  201. *ptr-- = '\0';
  202. while (ptr1 < ptr)
  203. {
  204. tmp_char = *ptr;
  205. *ptr-- = *ptr1;
  206. *ptr1++ = tmp_char;
  207. }
  208. return result;
  209. }
  210. /************************************************************************
  211. * @brief 整数转字符串
  212. * @param[in] num 整数
  213. * @param[out] buf 字符串
  214. * @return 返回字符串长度
  215. ************************************************************************/
  216. inline int _itoa(int num, char buf[32])
  217. {
  218. return _i2a(num, buf, 10);
  219. }
  220. /************************************************************************
  221. * @brief 整数转字符串
  222. * @param[in] num 整数
  223. * @param[out] buf 字符串
  224. * @param[in] radix 进位制整数
  225. * @return 返回字符串长度
  226. ************************************************************************/
  227. int _i2a(int num, char buf[32], int radix)
  228. {
  229. static const char s[] = "0123456789abcdef";
  230. int n = num, R = radix;
  231. char *dst = buf;
  232. if (n < 0)
  233. {
  234. *dst++ = '-';
  235. n = -n;
  236. }
  237. if (n < 10)
  238. {
  239. *dst++ = s[n];
  240. *dst = 0;
  241. }
  242. else
  243. {
  244. char tmp[32], *p = tmp;
  245. while (n)
  246. {
  247. *p++ = s[n % R];
  248. n /= R;
  249. }
  250. while (--p != tmp)
  251. *dst++ = *p;
  252. *dst++ = *tmp;
  253. *dst = 0;
  254. }
  255. return dst - buf;
  256. }
  257. /************************************************************************
  258. * @brief 浮点数转字符串
  259. * @param[in] val 浮点数
  260. * @param[out] buf 字符串
  261. * @param[in] eps 精度(小数位)
  262. * @return 返回字符串长度
  263. ************************************************************************/
  264. int _ftoa(double val, char buf[32], int eps)
  265. {
  266. double f = val;
  267. char *p = buf;
  268. if (val < 0)
  269. {
  270. *p++ = '-';
  271. f = -f;
  272. }
  273. int n = f;
  274. int len = _itoa(n, p);
  275. return len + __ftoa(f - n, p + len, eps);
  276. }
  277. /************************************************************************
  278. * @brief 浮点数转字符串:范围(-1, 1)
  279. * @param[in] val 浮点数
  280. * @param[out] buf 字符串
  281. * @param[in] eps 精度(小数位)
  282. * @return 返回字符串长度
  283. ************************************************************************/
  284. int __ftoa(double val, char buf[32], int eps)
  285. {
  286. double f = val;
  287. char *p = buf;
  288. static const char s[] = "0123456789";
  289. if (f < 0)
  290. {
  291. *p++ = '-';
  292. f = -f;
  293. }
  294. *p++ = '.';
  295. for (int i = eps + 1, n; --i; ++p, f -= n)
  296. *p = s[n = f *= 10.0];
  297. *p = 0;
  298. return p - buf;
  299. }
  300. /************************************************************************
  301. * @brief 替换sprintf
  302. * @ref 可变长参数列表误区与陷阱——va_arg不可接受的类型
  303. * http://www.cppblog.com/ownwaterloo/archive/2009/04/21/80655.aspx
  304. ************************************************************************/
  305. int _sprintf(char *dst, const char *format, ...)
  306. {
  307. char *s = dst;
  308. const char *f = format;
  309. va_list ap, another;
  310. va_start(ap, format);
  311. va_copy(another, ap);
  312. while (*f)
  313. {
  314. int n = 1;
  315. if ('%' != *f)
  316. {
  317. *s = *f;
  318. }
  319. else
  320. {
  321. ++f;
  322. switch (*f)
  323. {
  324. case 's': // 字符串
  325. {
  326. const char *p = va_arg(ap, char *);
  327. n = strlen(p);
  328. memcpy(s, p, n);
  329. }
  330. break;
  331. case 'd':
  332. case 'u': // 整数
  333. {
  334. char buf[32];
  335. n = _itoa(va_arg(ap, int), buf);
  336. memcpy(s, buf, n);
  337. }
  338. break;
  339. case 'f': // 浮点数
  340. {
  341. char buf[32];
  342. n = _ftoa(va_arg(ap, double), buf, 6);
  343. memcpy(s, buf, n);
  344. }
  345. break;
  346. case 'x': // 16进制数
  347. {
  348. char buf[32];
  349. n = _i2a(va_arg(ap, int), buf, 16);
  350. memcpy(s, buf, n);
  351. }
  352. break;
  353. case 'c': // 字符
  354. {
  355. *s = va_arg(ap, int);
  356. }
  357. break;
  358. case '%': // 百分号
  359. {
  360. *s = '%';
  361. }
  362. break;
  363. default:
  364. {
  365. va_end(ap);
  366. int x = vsprintf(dst, format, another);
  367. va_end(another);
  368. return x;
  369. }
  370. break;
  371. }
  372. }
  373. ++f;
  374. s += n;
  375. }
  376. *s = 0;
  377. va_end(ap);
  378. return s - dst;
  379. }
  380. uint8 bcc_chk(uint8 *data, uint16 length)
  381. {
  382. uint8 bcc_chk_return = 0x00;
  383. uint16 count = 0;
  384. while (count < length)
  385. {
  386. bcc_chk_return ^= data[count];
  387. count++;
  388. }
  389. return bcc_chk_return;
  390. }
  391. uint16 crc_chk(uint8 *data, uint8 length)
  392. {
  393. uint8 j;
  394. uint16 reg_crc = 0xFFFF;
  395. while (length--)
  396. {
  397. reg_crc ^= *data++;
  398. for (j = 0; j < 8; j++)
  399. {
  400. if (reg_crc & 0x01)
  401. {
  402. reg_crc = (reg_crc >> 1) ^ 0xA001;
  403. }
  404. else
  405. {
  406. reg_crc = reg_crc >> 1;
  407. }
  408. }
  409. }
  410. return reg_crc;
  411. }