AppFuncLib.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * @Author : ChenJie
  3. * @Date : 2022-01-21 09:28:20
  4. * @Version : V3.0
  5. * @LastEditors : ChenJie
  6. * @LastEditTime : 2022-02-14 15:23:30
  7. * @Description : file content
  8. * @FilePath : \S32K146_4G\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. char *Myitoa(int value, char *result, int base)
  145. {
  146. // check that the base if valid
  147. if (base < 2 || base > 36)
  148. {
  149. *result = '\0';
  150. return result;
  151. }
  152. char *ptr = result, *ptr1 = result, tmp_char;
  153. int tmp_value;
  154. do
  155. {
  156. tmp_value = value;
  157. value /= base;
  158. *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)];
  159. } while (value);
  160. // Apply negative sign
  161. if (tmp_value < 0)
  162. *ptr++ = '-';
  163. *ptr-- = '\0';
  164. while (ptr1 < ptr)
  165. {
  166. tmp_char = *ptr;
  167. *ptr-- = *ptr1;
  168. *ptr1++ = tmp_char;
  169. }
  170. return result;
  171. }
  172. /************************************************************************
  173. * @brief 整数转字符串
  174. * @param[in] num 整数
  175. * @param[out] buf 字符串
  176. * @return 返回字符串长度
  177. ************************************************************************/
  178. inline int _itoa(int num, char buf[32])
  179. {
  180. return _i2a(num, buf, 10);
  181. }
  182. /************************************************************************
  183. * @brief 整数转字符串
  184. * @param[in] num 整数
  185. * @param[out] buf 字符串
  186. * @param[in] radix 进位制整数
  187. * @return 返回字符串长度
  188. ************************************************************************/
  189. int _i2a(int num, char buf[32], int radix)
  190. {
  191. static const char s[] = "0123456789abcdef";
  192. int n = num, R = radix;
  193. char *dst = buf;
  194. if (n < 0)
  195. {
  196. *dst++ = '-';
  197. n = -n;
  198. }
  199. if (n < 10)
  200. {
  201. *dst++ = s[n];
  202. *dst = 0;
  203. }
  204. else
  205. {
  206. char tmp[32], *p = tmp;
  207. while (n)
  208. {
  209. *p++ = s[n % R];
  210. n /= R;
  211. }
  212. while (--p != tmp)
  213. *dst++ = *p;
  214. *dst++ = *tmp;
  215. *dst = 0;
  216. }
  217. return dst - buf;
  218. }
  219. /************************************************************************
  220. * @brief 浮点数转字符串
  221. * @param[in] val 浮点数
  222. * @param[out] buf 字符串
  223. * @param[in] eps 精度(小数位)
  224. * @return 返回字符串长度
  225. ************************************************************************/
  226. int _ftoa(double val, char buf[32], int eps)
  227. {
  228. double f = val;
  229. char *p = buf;
  230. if (val < 0)
  231. {
  232. *p++ = '-';
  233. f = -f;
  234. }
  235. int n = f;
  236. int len = _itoa(n, p);
  237. return len + __ftoa(f - n, p + len, eps);
  238. }
  239. /************************************************************************
  240. * @brief 浮点数转字符串:范围(-1, 1)
  241. * @param[in] val 浮点数
  242. * @param[out] buf 字符串
  243. * @param[in] eps 精度(小数位)
  244. * @return 返回字符串长度
  245. ************************************************************************/
  246. int __ftoa(double val, char buf[32], int eps)
  247. {
  248. double f = val;
  249. char *p = buf;
  250. static const char s[] = "0123456789";
  251. if (f < 0)
  252. {
  253. *p++ = '-';
  254. f = -f;
  255. }
  256. *p++ = '.';
  257. for (int i = eps + 1, n; --i; ++p, f -= n)
  258. *p = s[n = f *= 10.0];
  259. *p = 0;
  260. return p - buf;
  261. }
  262. /************************************************************************
  263. * @brief 替换sprintf
  264. * @ref 可变长参数列表误区与陷阱——va_arg不可接受的类型
  265. * http://www.cppblog.com/ownwaterloo/archive/2009/04/21/80655.aspx
  266. ************************************************************************/
  267. int _sprintf(char *dst, const char *format, ...)
  268. {
  269. char *s = dst;
  270. const char *f = format;
  271. va_list ap, another;
  272. va_start(ap, format);
  273. va_copy(another, ap);
  274. while (*f)
  275. {
  276. int n = 1;
  277. if ('%' != *f)
  278. {
  279. *s = *f;
  280. }
  281. else
  282. {
  283. ++f;
  284. switch (*f)
  285. {
  286. case 's': // 字符串
  287. {
  288. const char *p = va_arg(ap, char *);
  289. n = strlen(p);
  290. memcpy(s, p, n);
  291. }
  292. break;
  293. case 'd':
  294. case 'u': // 整数
  295. {
  296. char buf[32];
  297. n = _itoa(va_arg(ap, int), buf);
  298. memcpy(s, buf, n);
  299. }
  300. break;
  301. case 'f': // 浮点数
  302. {
  303. char buf[32];
  304. n = _ftoa(va_arg(ap, double), buf, 6);
  305. memcpy(s, buf, n);
  306. }
  307. break;
  308. case 'x': // 16进制数
  309. {
  310. char buf[32];
  311. n = _i2a(va_arg(ap, int), buf, 16);
  312. memcpy(s, buf, n);
  313. }
  314. break;
  315. case 'c': // 字符
  316. {
  317. *s = va_arg(ap, int);
  318. }
  319. break;
  320. case '%': // 百分号
  321. {
  322. *s = '%';
  323. }
  324. break;
  325. default:
  326. {
  327. va_end(ap);
  328. int x = vsprintf(dst, format, another);
  329. va_end(another);
  330. return x;
  331. }
  332. break;
  333. }
  334. }
  335. ++f;
  336. s += n;
  337. }
  338. *s = 0;
  339. va_end(ap);
  340. return s - dst;
  341. }
  342. uint8 bcc_chk(uint8 *data, uint16 length)
  343. {
  344. uint8 bcc_chk_return = 0x00;
  345. uint16 count = 0;
  346. while (count < length)
  347. {
  348. bcc_chk_return ^= data[count];
  349. count++;
  350. }
  351. return bcc_chk_return;
  352. }
  353. uint16 crc_chk(uint8 *data, uint8 length)
  354. {
  355. uint8 j;
  356. uint16 reg_crc = 0xFFFF;
  357. while (length--)
  358. {
  359. reg_crc ^= *data++;
  360. for (j = 0; j < 8; j++)
  361. {
  362. if (reg_crc & 0x01)
  363. {
  364. reg_crc = (reg_crc >> 1) ^ 0xA001;
  365. }
  366. else
  367. {
  368. reg_crc = reg_crc >> 1;
  369. }
  370. }
  371. }
  372. return reg_crc;
  373. }