实现蓝牙协议解析

This commit is contained in:
EN 2024-07-13 15:59:44 +08:00
parent 00ee34a664
commit 5ed416bc55

View File

@ -1,23 +1,24 @@
#include "bluetooth.h"
#include "usart.h"
#include "control.h"
#include "stdio.h"
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 100
#define MESSAGE_SIZE 19
#define CMD_NUM 9
static uint8_t rxBuffer[BUFFER_SIZE], rxSize;
static uint8_t txBuffer[BUFFER_SIZE], txSize;
static int8_t rxBuffer[BUFFER_SIZE], txBuffer[BUFFER_SIZE];
static uint8_t cmdIndex;
void BLUETOOTH_Init(void)
{
rxSize = 0;
HAL_UART_Receive_IT(&huart2, rxBuffer, 1);
HAL_UARTEx_ReceiveToIdle_DMA(&huart2, rxBuffer, 100);
}
void receiveCmd()
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
if (huart == &huart2 && rxBuffer[0] == '$' && rxBuffer[Size - 1] == '#')
{
// 通用协议
if (rxBuffer[2] == ',')
@ -51,25 +52,14 @@ void receiveCmd()
// 模式选择
case 'M':
sscanf(rxBuffer + 5, "%*4s%lc%lc", num, num + 1);
sscanf(rxBuffer + 5, "%*4s%c%c", num, num + 1);
CONTROL_Mode(num[0] - '0', num[1] - '0');
break;
}
}
HAL_UART_Transmit(&huart1, rxBuffer, rxSize, 1000);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart == &huart2)
{
if (rxSize && rxBuffer[rxSize] == '#')
{
if (++rxSize > 10)
receiveCmd();
memcpy(txBuffer, rxBuffer, Size);
printf("%s", txBuffer);
HAL_UART_Transmit_IT(&huart2, txBuffer, Size);
BLUETOOTH_Init();
}
else if (rxSize++ == 0 && rxBuffer[0] == '$')
HAL_UART_Receive_IT(&huart2, rxBuffer + rxSize, 1);
}
}