当前位置:网站首页>C language exercise: esp32 ble Low Power Bluetooth server data packaging and client data analysis
C language exercise: esp32 ble Low Power Bluetooth server data packaging and client data analysis
2022-06-12 00:07:00 【Naiva】
Problem description :
BLE Low power Bluetooth consists of server and client , The existing server obtains PWM data 、 Power Supply ADC The data is packaged and transmitted to the client through Bluetooth for data analysis , Finally, the parsed pwm and adc Data to int Type to deal with , And display the information on the screen of the client .
Why Package data ?
Because of the low power consumption BLE Bluetooth a service has multiple tags , Also known as eigenvalue , A label can ( read 、 Write 、 Notice, etc ) Serial communication is transmitted to the client device , Save the resources of Bluetooth transmission channel .
Parsing character data
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/** * @brief Parsing character data : Parse the expected string from a string Explain * @param pBuff: The string address to parse * @param pLeft: The string to the left of the target string * @param pRight: The string to the right of the target string * @param pRes: Address to receive character data * @retval 0: success -1: Failure */
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)
{
/* Raw data */
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};
/* Data packaging */
sprintf(buff,"name:%s, age:%d, number:%d.",name, age, number);
/* Data analysis */
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;
}
The results are as follows :

Parsing integer data
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/** * @brief Extract integer data from a string * @param pBuff: The string address to parse * @param pLeft: The string to the left of the target data * @param pRight: The string to the right of the target data * @param pRes: Address to receive integer data * @retval 0: success -1: Failure */
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); // character string pBuff Find the first occurrence of the string pLeft The location of , Does not contain Terminator '\0'
pEnd = strstr(pBegin + strlen(pLeft), pRight); // Compute string str The length of , Until the empty end character , But does not include the null end character
if (pEnd == NULL || pBegin == NULL || pBegin > pEnd) {
return -1;
} else {
pBegin += strlen(pLeft);
pTemp = (char *)malloc(pEnd - pBegin); // malloc Dynamic memory allocation ( The length of the transmitted string is unknown )
memcpy(pTemp, pBegin, pEnd - pBegin); // From the storage area pBegin Copy pEnd - pBegin Bytes to storage pTemp
*pRes = atoi(pTemp); // String to integer variable int type
free(pTemp);
return 0;
}
}
int main(int argc, char **argv)
{
/* Raw data */
int left = 123;
int right = 456;
char buff[100] = {
0};
int re_left = 0;
int re_right = 0;
/* Data packaging */
sprintf(buff, "[%d,%d]", left, right);
/* Data analysis */
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;
}

Reference material :
- [1] C Language simple data analysis : https://blog.csdn.net/weixin_45861321/article/details/114632122
- [2] C Library function - strstr()
- [3] C Library function - strlen()
- [4] C Library function - memcpy()
- [5] C Library function - malloc()
边栏推荐
- Shell (32): configure SSH privacy free
- CD流程
- 926. Flip String to Monotone Increasing
- I2C read / write process
- 926. Flip String to Monotone Increasing
- m-way search trees
- 11. conditional statement if, switch
- (dp+ longest common subsequence) acwing 897 Longest common subsequence
- Teach you to play with SSM framework
- CD process
猜你喜欢

gin解决跨域问题

Redis的主从复制、哨兵模式和集群

Jenkins基本配置

Here we go! Dragon lizard community enters PKU classroom

Lake Shore - supervaritemp low temperature thermostat

CD流程
![[flume] notes](/img/33/50f14b0267c5e502970cee0278aa71.jpg)
[flume] notes

2022 618 notebook shopping guide

平衡二叉树(AVL 树)

Do you want to take the postgraduate entrance examination? Will you be able to find a good job after graduate school?
随机推荐
明德扬ADC系列开发板-Ad9653子板 多通道 高分辨率 高采样率
11. conditional statement if, switch
Redis的主从复制、哨兵模式和集群
Unified certification center oauth2 high availability pit
Here we go! Dragon lizard community enters PKU classroom
EFCore中数据表的两种配置方式
(dp+ group backpack) acwing 9 Group knapsack problem
I2C read / write process
将数组分成和相等的三个部分[问题分析]
Achievements in science and Technology (XV)
sonarqube介绍和安装步骤
wps表格怎么取消智能表格样式
苹果手机wps如何改字体大小
[day 7 of JUC learning] reentrantlock and reentrantreadwritelock
SF14 | supertrend "super trend" indicator magic change and upgrade (source code)
Jenkins basic configuration
Mingdeyang FPGA development board xilinx-k7 core board kinex7k325 410t industrial grade
What is webstorage? And cookies
如何优化PlantUML流程图(时序图)
预解析与作用域