【Arduino】串口数据收发处理

该博客主要介绍了如何使用NANO开发板进行串口数据的接收和处理。通过定义缓冲区大小、数据长度等参数,实现数据帧的校验、指令解析和相应任务的触发。当接收到特定指令时,会执行对应的任务,如发送特定的数据包。同时,还包含了串口发送函数和数据预处理函数,确保了数据的正确性和系统的稳定性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 利用NANO开发板编写

#define MAX_BUFFERSIZE 128 //自定义的缓冲buff长度
char uart_Receive_Buff [MAX_BUFFERSIZE]; //缓冲buff
unsigned int uart_Receive_Length = 0; //接收字节长度
unsigned long preUartTime = 0; //记录读取最好一个字节的时间点
unsigned int frame_Length = 5; //数据长度
unsigned int frame_Check_Begin = 1; //校验起始位
unsigned int frame_Inst_One = 2; //指令1
unsigned int frame_Inst_Two = 3; //指令2
unsigned int frame_Check_Length = 3; //校验长度
unsigned int frame_state = 0; //校验指令
unsigned int frame_inst1 = 0; //校验指令1
unsigned int frame_inst2 = 0; //校验指令2
bool task1_flag = false; //任务1标志位
bool task2_flag = false; //任务2标志位
bool task3_flag = false; //任务3标志位

/*
  校验函数
*/
char checksum(char *p, int len)
{
  char sum = 0;
  while(len--)
  {
    sum += *p;
    p++;
  }
  return sum; 
}
/*
  串口发送函数
*/
void sendUART(const char *p, size_t len) {
  Serial.write(p, len);
}
/*
  预处理串口数据函数
*/
void uartDataProcess(char *p , int len) 
{
  if ((len != frame_Length)&&(p != 0x34))    //不是目标长度
  {
    char testframebuf[1] = {0xFF};
    sendUART(testframebuf, 1);
  }
  else
  {
    if(checksum(&p[frame_Check_Begin],frame_Check_Length)!=p[len-1])
    { 
      char testframebuf1[2] = {0xFF ,0xFF};
      sendUART(testframebuf1, 2);
    }
    else
    {
      Serial.write(p, len);
      instructDataProcess(p , len);
    }
  }
}
/*
  指令处理函数
*/
void instructDataProcess(char *p , int len) 
{
  frame_state = p[frame_Check_Begin];
  frame_inst1 = p[frame_Inst_One];
  frame_inst2 = p[frame_Inst_Two];
  if(frame_state == 1)
  {
    if(frame_inst1 == 1)//0x34,0x01,0x01
    {
        char testframebuf2[3] = {0x01,0x01,0x00};
        sendUART(testframebuf2, 3);
        task1_flag = false;
    }
    else if(frame_inst1 == 2)//0x34,0x01,0x02
    {
        char testframebuf3[3] = {0x01,0x02,0x00};
        sendUART(testframebuf3, 3);
        task2_flag = false;
    }
    else if(frame_inst1 == 3)//0x34,0x01,0x03
    {
        char testframebuf4[3] = {0x01,0x03,0x00};
        sendUART(testframebuf4, 3);
        task3_flag = false;
    }
  }
  else if(frame_state == 2)//0x34,0x02,0x01
  {
    if(frame_inst1 == 1)
    {
        char testframebuf5[3] = {0x02,0x01,0x01};
        sendUART(testframebuf5, 3);
        task1_flag = true;
    }
    else if(frame_inst1 == 2)//0x34,0x02,0x02
    {
        char testframebuf6[3] = {0x02,0x02,0x01};
        sendUART(testframebuf6, 3);
        task2_flag = true;
    }
    else if(frame_inst1 == 3)//0x34,0x02,0x03
    {
        char testframebuf7[3] = {0x02,0x03,0x01};
        sendUART(testframebuf7, 3);
        task3_flag = true;
    } 
  }  
}
/*
  指令处理函数
*/
void task1(void)
{
  char testframebuf8[3] = {0x11,0x11,0x11};
  sendUART(testframebuf8, 3);
}

/*
  指令处理函数
*/
void task2(void)
{
  char testframebuf9[3] = {0x22,0x22,0x22};
  sendUART(testframebuf9, 3);
}

/*
  指令处理函数
*/
void task3(void)
{
  char testframebuf10[3] = {0x33,0x33,0x33};
  sendUART(testframebuf10, 3);
}

/*
  串口数据接受进程
*/
void doUartReceiveInit()
{
  if (Serial.available())
  { 
    uart_Receive_Buff [uart_Receive_Length++] = Serial.read();//读取串口数据
    preUartTime = millis(); //读取接收数据时间
    if (uart_Receive_Length  >= MAX_BUFFERSIZE-1) 
    { 
      uart_Receive_Length  = MAX_BUFFERSIZE-2;
      preUartTime = preUartTime - 200;
    }
  }
  if (uart_Receive_Length  > 0 && (millis() - preUartTime >= 100))  //最后一个字节后超过100ms数据处理
  { 
    uart_Receive_Buff [uart_Receive_Length] = 0x00;//处理串口数据
    Serial.flush();
    uartDataProcess(uart_Receive_Buff, uart_Receive_Length);//执行串口数据处理函数
    uart_Receive_Length = 0;
  }
}
/*
  处理串口数据函数,可以在函数内编写处理函数数据的程序
*/
void setup() {
  Serial.begin(115200);
}
/*
  处理串口数据函数,可以在函数内编写处理函数数据的程序
*/
void loop()
{
  doUartReceiveInit();  //调用串口数据处理进程
  if(task1_flag == true)
  {
    task1();
  }
  else if(task2_flag == true)
  {
    task2();
  }
  else if(task3_flag == true)
  {
    task3();
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

米杰的声音

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值