发送端数据,使用的是openmv

这是接收端数据,后两位经常会接收错误,前两位无异常

void USART1_IRQHandler(void)
{
static uint8_t RxState = 0; // 状态机变量,跟踪接收状态
static uint8_t Serial_RxIndex = 0; // 存储接收到的字符索引
if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET) // 检查接收中断
{
char receivedChar = USART_ReceiveData(USART1); // 读取接收到的数据
// 状态机处理接收到的数据
switch (RxState)
{
case 0: // 初始状态
if (receivedChar == '[')
{
Serial_RxIndex = 0; // 如果是 '[', 则开始新的接收
RxState = 1; // 切换到接收状态
}
break;
case 1: // 接收数据状态
if (receivedChar == ']')
{
Serial_RxBuffer[Serial_RxIndex] = '\0'; // 字符串结束符
Serial_DataReady = 1; // 设置数据准备标志
RxState = 0; // 切换回初始状态
}
else if (Serial_RxIndex < BUFFER_SIZE - 1) // 确保不会溢出
{
Serial_RxBuffer[Serial_RxIndex++] = receivedChar; // 加入接收缓冲区
}
else
{
// 缓冲区满,清空接收数据并重置状态
Serial_RxIndex = 0; // 清空索引以重新开始接收
RxState = 0; // 返回初始状态,丢弃接收到的数据
}
break;
default:
RxState = 0; // 处理意外状态,重置状态
break;
}
USART_ReceiveData(USART1);
USART_ClearITPendingBit(USART1, USART_IT_RXNE); // 清除
}
}