/* * @Author : ChenJie * @Date : 2022-01-21 09:28:20 * @Version : V3.0 * @LastEditors : ChenJie * @LastEditTime : 2022-01-27 18:24:36 * @Description : file content * @FilePath : \S32K144_BLE\src\AppFuncLib.c */ /* * AppFuncLib.c *应用层函数库 * Created on: 2022年1月21日 * Author: QiXiang_CHENJIE */ #include "AppFuncLib.h" uint16 ATstrdel(char *str) { char *p = str; bool flag = false; while (*str) { if (*str > 0x20) { *(p) = *str; p = p + 1; flag = false; } else { if (!flag) { *(p) = ','; p = p + 1; flag = true; } } str++; } *p = '\0'; return 0; } uint16 mstrlen(const char *s) { uint16 out = 0; const char *ss = s; while (*ss) ss++; out = (ss - s); return out; } int mstrncmp(const char *s1, const char *s2, int n) { const unsigned char *c1 = (const unsigned char *)s1; const unsigned char *c2 = (const unsigned char *)s2; unsigned char ch; int d = 0; while (n--) { d = (int)(ch = *c1++) - (int)*c2++; if (d || !ch) break; } return d; } unsigned char HexToChar(unsigned char bHex) { if ((bHex >= 0) && (bHex <= 9)) bHex += 0x30; else if ((bHex >= 10) && (bHex <= 15)) //大写字母 bHex += 0x37; else bHex = 0xff; return bHex; } unsigned char CharToHex(unsigned char bChar) { if ((bChar >= 0x30) && (bChar <= 0x39)) bChar -= 0x30; else if ((bChar >= 0x41) && (bChar <= 0x46)) //大写字母 bChar -= 0x37; else if ((bChar >= 0x61) && (bChar <= 0x66)) //小写字母 bChar -= 0x57; else bChar = 0xff; return bChar; } uint8 AtStrCompare(const char *a, const char *b) { uint8 out = 1; while (1) { if (*a == '\0' || *b == '\0') //判断其中是否有字符串结束 { if (strlen(a) == strlen(b)) { out = 1; break; } else { out = 0; break; } } else { if (*a != *b) { out = 0; break; } else if (*a == '=' && *b == '=') { out = 1; break; } } a++; b++; } return out; } unsigned short CRC16_Modbus ( unsigned char *pdata, int len) { unsigned short crc=0xFFFF; int i, j; for ( j=0; j0) { crc=crc>>1; crc=crc^ 0xa001; } else crc=crc>>1; } } return crc; } uint16 crc_chk(uint8 *data, uint8 length) { uint8 j; uint16 reg_crc = 0xFFFF; while (length--) { reg_crc ^= *data++; for (j = 0; j < 8; j++) { if (reg_crc & 0x01) { reg_crc = (reg_crc >> 1) ^ 0xA001; } else { reg_crc = reg_crc >> 1; } } } return reg_crc; }