电脑端通过串口发送特定格式数据帧,包含指令和数据。
数据格式:{ {指令1:数据1;指令2:数据2;......}}
STM32f103通过UART1与电脑通讯,接收数据后进行解析,并返回解析结果进行测试。
1、定义 int extractCommands()解析函数
// 结构体用于存储指令和数据
struct CommandData {
char command[MAX_CMD_LENGTH];//指令
unsigned long data; //数据
}
// 提取指令和数据的函数
//数据包格式:{
{xx:xx;xx:xx;}}//以{
{}}作为起止,以“;”分组,以“:”区分该组指令和数据
//入口参数:
// char *input --接收到的字符串数据帧
// struct CommandData *commands--返回CommandData 结构体数据,
int extractCommands(char *input, struct CommandData *commands) {
char *token;
int count = 0;
const char *start = strstr(input, "{
{");
const char *end = strstr(input, "}}");
if (!start || !end || end <= start)
return -1; // 没有找到合法的开始和结束标志
// 去掉前面�????"{
{"和后面的"}}"
start &#