123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- /*
- * @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; j<len;j++)
- {
- crc=crc^pdata[j];
- for ( i=0; i<8; i++)
- {
- if( ( crc&0x0001) >0)
- {
- 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;
- }
|