当前位置:网站首页>C语言练习:ESP32 BLE低功耗蓝牙服务端数据打包和客户端数据解析
C语言练习:ESP32 BLE低功耗蓝牙服务端数据打包和客户端数据解析
2022-06-12 00:06:00 【Naiva】
问题描述:
BLE 低功耗蓝牙由服务端和客户端组成,现有服务端获取PWM数据、电源ADC数据通过数据打包后由蓝牙传输到客户端进行数据解析,最后将解析过后的pwm和adc数据转换成int类型做处理,并且将信息显示在客户端的屏幕上。
为什么需要打包数据?
因为低功耗BLE蓝牙一个服务有多个标签,也叫特征值,一个标签可以(读、写、通知等等 ) 串行通信传输到客户端设备,节省蓝牙传输通道的资源。
解析字符型数据
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/** * @brief 解析字符型数据 :从一段字符串中解析期望的字符串 解 * @param pBuff: 要解析的字符串地址 * @param pLeft: 目标字符串左边的字符串 * @param pRight: 目标字符串右边的字符串 * @param pRes: 接收字符数据的地址 * @retval 0:成功 -1:失败 */
int DataParsingChar(const char *pBuff, const char *pLeft, const char *pRight,char *pRes)
{
char *pBegin = NULL;
char *pEnd = NULL;
pBegin = strstr(pBuff, pLeft);
pEnd = strstr(pBegin + strlen(pLeft), pRight);
if (pEnd == NULL || pBegin == NULL || pBegin > pEnd) {
return -1;
} else {
pBegin += strlen(pLeft);
memcpy(pRes, pBegin, pEnd - pBegin);
return 0;
}
}
int main(int argc, char **argv)
{
/* 原始数据 */
char *name = "LiHua";
int age = 20;
int number = 10086;
char buff[100] = {
0};
char re_name[10] = {
0};
char re_age[10] = {
0};
char re_number[10] = {
0};
/* 数据打包 */
sprintf(buff,"name:%s, age:%d, number:%d.",name, age, number);
/* 数据解析 */
DataParsingChar(buff, "name:", ",", re_name);
DataParsingChar(buff, "age:", ",", re_age);
DataParsingChar(buff, "number:", ".", re_number);
printf("buff : %s\n", buff);
printf("re_name : %s\n", re_name);
printf("re_age : %s\n", re_age);
printf("re_number : %s\n", re_number);
return 0;
}
执行结果如下:

解析整型数据
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/** * @brief 从一段字符串中提取出整型数据 * @param pBuff: 要解析的字符串地址 * @param pLeft: 目标数据左边的字符串 * @param pRight: 目标数据右边的字符串 * @param pRes: 接收整型数据的地址 * @retval 0:成功 -1:失败 */
int DataParsingInt(const char *pBuff, const char *pLeft, const char *pRight, int *pRes)
{
char *pBegin = NULL;
char *pEnd = NULL;
char *pTemp = NULL;
pBegin = strstr(pBuff, pLeft); // 字符串 pBuff中查找第一次出现字符串 pLeft的位置,不包含终止符 '\0'
pEnd = strstr(pBegin + strlen(pLeft), pRight); // 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符
if (pEnd == NULL || pBegin == NULL || pBegin > pEnd) {
return -1;
} else {
pBegin += strlen(pLeft);
pTemp = (char *)malloc(pEnd - pBegin); // malloc 动态分配内存 (不清楚传输的字符串长度)
memcpy(pTemp, pBegin, pEnd - pBegin); // 从存储区 pBegin复制 pEnd - pBegin 个字节到存储区 pTemp
*pRes = atoi(pTemp); // 字符串转换为整型变量 int类型
free(pTemp);
return 0;
}
}
int main(int argc, char **argv)
{
/* 原始数据 */
int left = 123;
int right = 456;
char buff[100] = {
0};
int re_left = 0;
int re_right = 0;
/* 数据打包 */
sprintf(buff, "[%d,%d]", left, right);
/* 数据解析 */
DataParsingInt(buff, "[", ",", &re_left);
DataParsingInt(buff, ",", "]", &re_right);
printf("buff : %s\n", buff);
printf("re_left : %d\n", re_left);
printf("re_right : %d\n", re_right);
printf("re_right - re_left = %d\n", re_right - re_left);
return 0;
}

参考资料:
- [1] C语言简单数据解析: https://blog.csdn.net/weixin_45861321/article/details/114632122
- [2] C 库函数 - strstr()
- [3] C 库函数 - strlen()
- [4] C 库函数 - memcpy()
- [5] C 库函数 - malloc()
边栏推荐
- require. context
- Gin solves cross domain problems
- voc数据格式转为coco数据格式
- Mingdeyang ADC series development board-ad9653 daughter board multi-channel high resolution and high sampling rate
- DOM知識點總結
- Cube technology interpretation | detailed explanation of cube applet Technology
- 图及图的遍历
- CD流程
- How to optimize plantuml flow diagram (sequence diagram)
- Antigen products enter the family, and Chinese medical device enterprises usher in a new blue ocean
猜你喜欢
![Display product details [project mall]](/img/51/1ead9d9dde9bca6a9acea9667328ac.png)
Display product details [project mall]

统一认证中心 Oauth2 高可用坑

Mysql5 and mysql8 are installed at the same time

Pleasant and burden free cross process communication

Flex flexible layout tutorial and understanding of the main axis cross axis: Grammar

IP编址概述

Chisel environment setup (win10 + vscode)

In order to stimulate inspiration and creativity, Shanghai daoning united with XMIND to bring you full-featured mind mapping and brainstorming software

Introduction and installation steps of sonarqube

win10文件夹状态红叉表示的是什么
随机推荐
Setting alias alias and @ reference note
Delete the receiving address [project mall]
Here we go! Dragon lizard community enters PKU classroom
[day 5 of JUC learning] reference atomic classes and attribute modifiers
Introduction and installation steps of sonarqube
Graph and graph traversal
[C language] data type storage, original code, inverse code and complement code
[day 6 of JUC learning] concurrent calculator and thread random factor
I2C read / write process
(dp+ longest common subsequence) acwing 897 Longest common subsequence
SAP SD create / modify price list
Binary sort tree
(linear DP) acwing 898 Number triangle
(linear DP | monotone queue) acwing 895 Longest ascending subsequence
Custom JSP tag - > concept - > lifecycle
如何优化PlantUML流程图(时序图)
11. conditional statement if, switch
(dp+ group backpack) acwing 9 Group knapsack problem
Handwritten simple promise
win10文件夹状态红叉表示的是什么