STM32 UART串口读取解析个人总结(基本找到标准解析写法)

 STM32 UART串口读取解析总结

参考这三篇博文

https://blog.csdn.net/sinat_16643223/article/details/119226462

 https://blog.csdn.net/sinat_16643223/article/details/118830297

https://blog.csdn.net/sinat_16643223/article/details/119307624

方式两种:终端和查询

大部分是中断,用中断写法我看了两个STM32的读取解析程序,大体差不多,基本是卡一个字节处理一个字节,不像PC上对缓冲区一堆字节处理。写法参考这两篇博文里的代码。 对比下面两段代码是不是差不多。

https://blog.csdn.net/sinat_16643223/article/details/119307624

flow_decode.c

#include<stdint.h>
#include"flow_decode.h"
 
UpixelsOpticalFlow updata;
 
int16_t flow_parse_char(uint8_t ch)
{
    int16_t ret = 1;
    static int16_t s  = 0, p = 0;
    static uint8_t Xor_r = 0x00, Xor_c = 0x00;
 
    switch(s)
    {
        case 0:
            if(ch == 0xFE)
            {
                s = 1;
                //printf("Got FE\n");
            }
        break;
        case 1:
            if(ch == 0x0A)
            {
                s = 2;
                p = 0;
                Xor_c = 0x00;
                //printf("Got 0A\n");
            }else
                s = 0;
        break;
        case 2:
            ((char *)&updata)[p++] = ch;
            Xor_c ^= ch;
            if(p == 10){
                s = 3;
                p = 0;
            }
        break;
        case 3: //crc
            s = 4;
            Xor_r = ch;
        break;
        case 4://end
            if(ch == 0x55){
               //printf("Got 0x55\n");
                if(Xor_r == Xor_c){
                    ret = 0;
                }
                else
                    ret = 2;
            }
            s = 0;
        break;
        default:
        break;
    }
    return ret;
}
 

  https://blog.csdn.net/sinat_16643223/article/details/118830297

还有一个例程代码可以看我这篇博文,也是和上面差不多的写法,看来基本可以确定STM32的串口中断法读取解析基本是这种样式写了。

https://blog.csdn.net/sinat_16643223/article/details/119308734

ACfly的GPS数据的串口的解析也是这样的,注意有的传感器驱动不是这么写的因为不是基于UART串口的,而是IIC或者其他的。

https://gitee.com/maxibooksiyi/maxi-ACfly-T265-baseon2020.9.22/blob/master/2020.9.22/Drivers/drv_GPS.cpp

这里也是用中断进行的STM32串口的收发,收到一个字节,就会产生一个接收中断。

 https://blog.csdn.net/sinat_16643223/article/details/119380455

https://mp.weixin.qq.com/s/vTmFTYnK8QwtdwMCnHml0g

thoneflow也就是思码光光流模块的PX4串口驱动的解析函数的写法也是这个思路!!!!!!仔细看挺简单的,和STM32中断方式进行串口解析的写法一样。parser就是解析的意思。

https://gitee.com/maxibooksiyi/PX4-Autopilot/blob/master/src/drivers/optical_flow/thoneflow/thoneflow_parser.cpp

 

  • 10
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个问题我可以回答。以下是一个使用 STM32 标准库的 UART FIFO 接收例程,可以读取多个字节: ```c #include "stm32f10x.h" #define RX_FIFO_SIZE 64 volatile uint8_t rx_fifo[RX_FIFO_SIZE]; volatile uint8_t rx_fifo_head = 0; volatile uint8_t rx_fifo_tail = 0; void USART1_IRQHandler(void) { if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { rx_fifo[rx_fifo_head] = USART_ReceiveData(USART1); rx_fifo_head = (rx_fifo_head + 1) % RX_FIFO_SIZE; } } void uart_receive_bytes(uint8_t *buf, uint32_t len) { uint32_t i; for (i = 0; i < len; i++) { while (rx_fifo_head == rx_fifo_tail); buf[i] = rx_fifo[rx_fifo_tail]; rx_fifo_tail = (rx_fifo_tail + 1) % RX_FIFO_SIZE; } } int main(void) { USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; // 初始化 USART1 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); // 配置 USART1 中断 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_Cmd(USART1, ENABLE); while (1) { uint8_t buf[10]; uart_receive_bytes(buf, 10); // 处理接收到的数据 } } ``` 这个例程使用了一个固定大小的 FIFO 缓冲区来存储接收到的数据。当有数据到达时,USART1_IRQHandler() 中断处理函数会把数据存储到 FIFO 中。当需要读取多个字节时,可以调用 uart_receive_bytes() 函数,传入一个缓冲区和需要读取的字节数。该函数会一直等待,直到 FIFO 中有足够的字节可供读取,然后把数据从 FIFO 中取出并存储到缓冲区中。 需要注意的是,这个例程可能存在一些问题,比如当 FIFO 缓冲区满时可能会丢失数据。在实际应用中,建议使用更为健壮的接收方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值