项目使用的是STM32+ ESP8266(AT固件)VS2019调试。编译出现报错时,在项目/属性/(C/C++)/预处理器/预处理器定义/编辑/ 添加 _CRT_SECURE_NO_WARNINGS
串口接收的数据:
+MQTTSUBRECV:0,"/sys/a15e2EhA***/BP***/thing/service/property/set",263,{"method":"thing.service.property.set","id":"782857171","params":{"LocalTimer":[{"Timer":"47 20 * * 1,2,3,4,5,6,7","Enable":1,"FoodVol":20,"TimezoneOffset":28800,"IsValid":1},{"Timer":"","Enable":0,"FoodVol":0,"TimezoneOffset":-1,"IsValid":0}]},"version":"1.0.0"}
除去+MQTTSUBRECV:0,"/sys/a15e2EhA***/BP***/thing/service/property/set",263;JSON格式化后
{
"method": "thing.service.property.set",
"id": "782857171",
"params": {
"LocalTimer": [{
"Timer": "47 20 * * 1,2,3,4,5,6,7",
"Enable": 1,
"FoodVol": 20,
"TimezoneOffset": 28800,
"IsValid": 1
}, {
"Timer": "",
"Enable": 0,
"FoodVol": 0,
"TimezoneOffset": -1,
"IsValid": 0
}]
},
"version": "1.0.0"
}
主要提取params,LocalTimer,Timer,FoodVol,TimezoneOffset对象的数据。
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "cJSON.h"
#include <stdlib.h>
#define DEF_params "params"
#define DEF_FoodSwitch "FoodSwitch"
#define DEF_FoodVol "FoodVol"
#define DEF_version "version"
#define DEF_Timer "Timer"
#define DEF_Enable "Enable"
#define DEF_TimezoneOffset "TimezoneOffset" // 时区偏移
#define DEF_IsValid "IsValid" // 有效值
#define DEF_LocalTimer "LocalTimer" // 本地定时
static void CountDown_Analysis(char *str) // 倒计时解析
{
// char str[] = "FoodSwitch-1-0-600-1709651086202,UV-1-1-500-1709650954839";
char *str1 = strtok(str, ",");
char *str2 = strtok(NULL, "");
char *token1;
printf("7:%s\n", str1);
if (str1)
{
token1 = strtok(str1, "-");
printf("1:%s\n", token1); // FoodSwitch
FoodSwitch:
if (strstr(token1, DEF_FoodSwitch))
{
token1 = strtok(NULL, "-");
printf("2:%s\n", token1);
printf("2-1:%d\n", atoi(token1));
token1 = strtok(NULL, "-");
printf("3:%s\n", token1);
printf("3-1:%d\n", atoi(token1));
int data1_int = atoi(strtok(NULL, "-")); // 提取600
long long data1_long = atoll(strtok(NULL, "-")); // 提取1709651086202
printf("Data from str1: %d, %lld\n", data1_int, data1_long);
}
else
goto UV;
}
printf("8:%s\n", str2);
if (str2)
{
// 提取字符串中的数据
token1 = strtok(str2, "-");
printf("4:%s\n", token1); // UV
UV:
if (strstr(token1, "UV"))
{
token1 = strtok(NULL, "-");
printf("5:%s\n", token1);
printf("5-1:%d\n", atoi(token1));
token1 = strtok(NULL, "-");
printf("6:%s\n", token1);
printf("6-1:%d\n", atoi(token1));
int data2_int = atoi(strtok(NULL, "-")); // 提取500
long long data2_long = atoll(strtok(NULL, "-")); // 提取1709650954839
printf("Data from str2: %d, %lld\n", data2_int, data2_long);
}
else
goto FoodSwitch;
}
}
static void WeekGet(char *str, char *re) // scanf 有空格检测失败的,可以用gets(str);代替scanf("%s", str);
{
// char str[] = "35 15 * * 1,2,3,4,5,6,7";
// char str[] = "33 15 * * *";
char i = 0;
unsigned char Timer = 0;
char *token1;
char *str1 = strtok(str, " ");
printf("7:%s\n", str1); // 35 s
if (str1)
{
printf("7-1:%d\n", atoi(str1)); // 35 d
}
str1 = strtok(NULL, " ");
printf("8:%s\n", str1); // 15 s
if (str1)
{
printf("8-1:%d\n", atoi(str1)); // 15 d
str1 = strtok(NULL, " ");
printf("8-1:%s\n", str1); //*
str1 = strtok(NULL, " ");
printf("8-2:%s\n", str1); //*
}
do
{
token1 = strtok(NULL, ",");
if (token1)
{
i++;
switch ((int)atoi(token1))
{
case 1:
Timer |= 0x02;
break;
case 2:
Timer |= 0x04;
break;
case 3:
Timer |= 0x08;
break;
case 4:
Timer |= 0x10;
break;
case 5:
Timer |= 0x20;
break;
case 6:
Timer |= 0x40;
break;
case 7:
Timer |= 0x80;
break;
default:
Timer |= 0x01;
}
char k = atoi(token1);
j:
i++;
if (i == k)
{
printf("周%d 开\n", k);
}
else if (i <= 7)
{
printf("周%d 关\n", i);
}
else if (Timer == 1)
{
printf("执行一次 开\n");
}
else
{
printf("执行一次 关\n");
}
}
else
goto j;
} while (token1 || i <= 7);
printf("Timer=%d\n", Timer);
*re = Timer;
}
static void test1(char *str) // scanf 有空格检测失败的,可以用gets(str);代替scanf("%s", str);
{
// char* str = (char*)malloc(200 * sizeof(int));
// scanf("%s", str);
// gets(str);
// 提取JSON数据
char *json_start = strchr(str, '{');
char *json_end = strrchr(str, '}');
if (json_start && json_end && json_end > json_start)
{
// if (json_start && json_end ) {
// 计算JSON数据长度
int json_len = json_end - json_start + 1;
bool json_outbool;
int json_outint = 0;
float json_outfloat = 0;
char *json_outstr;
// 创建缓冲区,并拷贝JSON数据
char *json_str = (char *)malloc(json_len + 1);
// char* json_str = (char*)malloc(400);
strncpy(json_str, json_start, json_len);
json_str[json_len] = '\0';
// 解析JSON数据
cJSON* json = cJSON_Parse(json_str);
// cJSON *json = cJSON_Parse(json_start);
printf("json :%s\n", json);
if (json)
{
// 解析params元素
cJSON *params = cJSON_GetObjectItem(json, DEF_params);
if (params)
{
cJSON *LocalTimer = cJSON_GetObjectItem(params, DEF_LocalTimer); // 本地定时
if (LocalTimer)
{
int i;
unsigned char get = 0;
for (i = 0; i < cJSON_GetArraySize(LocalTimer); i++)
{
cJSON *timer_object = cJSON_GetArrayItem(LocalTimer, i);
printf("LocalTimer %d:\n", i + 1);
printf("Timer: %s\n", cJSON_GetObjectItem(timer_object, DEF_Timer)->valuestring);
printf("Enable: %d\n", cJSON_GetObjectItem(timer_object, DEF_Enable)->valueint);
printf("FoodVol: %d\n", cJSON_GetObjectItem(timer_object, DEF_FoodVol)->valueint);
printf("TimezoneOffset: %d\n", cJSON_GetObjectItem(timer_object, DEF_TimezoneOffset)->valueint);
printf("IsValid: %d\n", cJSON_GetObjectItem(timer_object, DEF_IsValid)->valueint);
if (i == 0)
{
WeekGet(cJSON_GetObjectItem(timer_object, DEF_Timer)->valuestring, &get);
printf("get=%d\n", get);
}
}
}
if (cJSON_GetObjectItem(json, DEF_version))
{
json_outstr = cJSON_GetObjectItem(json, DEF_version)->valuestring;
printf("%s: %s\n", DEF_version, json_outstr);
}
}
else
{
printf("未找到params元素\n");
}
// 释放内存
free(json_str);
cJSON_Delete(json);
}
else
{
printf("解析JSON数据失败\n");
}
}
else
{
printf("未在字符串中找到JSON数据\n");
}
}
int main()
{
unsigned char get = 0;
// char str[] = "35 15 * * 1,2,3,5,6,7";
// char str[] = "33 15 * * *";
// char *str = (char *)malloc(1000);
// scanf("%s", str);
// gets(str);
const char *str = "+MQTTSUBRECV:0,\"/sys/a15e2EhA**/BP**/thing/service/property/set\",263,{\"method\":\"thing.service.property.set\",\"id\":\"782857171\",\"params\":{\"LocalTimer\":[{\"Timer\":\"47 20 * * 1,2,3,4,5,6,7\",\"Enable\":1,\"FoodVol\":20,\"TimezoneOffset\":28800,\"IsValid\":1},{\"Timer\":\"\",\"Enable\":0,\"FoodVol\":0,\"TimezoneOffset\":-1,\"IsValid\":0}]},\"version\":\"1.0.0\"}";
char str1[] = "FoodSwitch-1-0-600-1709651086202,UV-1-1-500-1709650954839";
CountDown_Analysis(str1);
test1(str);
// WeekGet(str, &get);
// printf("get=%d", get);
// free(str);
return 0;
}
输出的结果
+MQTTSUBRECV:0,"/sys/a15e2EhA***/BP**/thing/service/property/set",263,{"method":"thing.service.property.set","id":"782857171","params":{"LocalTimer":[{"Timer":"47 20 * * 1,2,3,7","Enable":1,"FoodVol":20,"TimezoneOffset":28800,"IsValid":1},{"Timer":"","Enable":0,"FoodVol":0,"TimezoneOffset":-1,"IsValid":0}]},"version":"1.0.0"}
7:FoodSwitch-1-0-600-1709651086202
1:FoodSwitch
2:1
2-1:1
3:0
3-1:0
Data from str1: 600, 1709651086202
8:UV-1-1-500-1709650954839
4:UV
5:1
5-1:1
6:1
6-1:1
Data from str2: 500, 1709650954839
json :
LocalTimer 1:
Timer: 47 20 * * 1,2,3,7
Enable: 1
FoodVol: 20
TimezoneOffset: 28800
IsValid: 1
7:47
7-1:47
8:20
8-1:20
8-1:*
8-2:*
周1 开
周2 开
周3 开
周4 关
周5 关
周6 关
周7 开
执行一次 关
Timer=142
get=142
LocalTimer 2:
Timer:
Enable: 0
FoodVol: 0
TimezoneOffset: -1
IsValid: 0
version: 1.0.0
F:\VS2019\Project1.exe (进程 32456)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
程序链接:https://pan.baidu.com/s/1U72omwRUL6iJPLmeWvipZA?pwd=BPBP
提取码:BPBP