当前位置:网站首页>Realize serial port receiving data based on STM32 ring queue
Realize serial port receiving data based on STM32 ring queue
2022-07-31 19:44:00 【Li Xiaoyao】
关注、星标公众号,直达精彩内容
原文链接:https://blog.csdn.net/qq_36413982/article/details/103836946
说在前面
Those who code should learn data structures and have learned about queues.A circular queue is a special form of queue,It is widely used.Because there are so many articles about this,Theoretical knowledge can be seen by others,The following is well written:STM32进阶之串口环形缓冲区实现
代码实现
Ring queue data structure
typedef struct ringBuff{
unsigned int in; //写入的位置
unsigned int out; //读出的位置
unsigned char buffer[RING_BUFF_SIZE]; //数据域
}stRingBuff;
Write one byte of data to the queue
/**
- @brief: Write one byte of data to the circular queue
- @param[in]: None
- @retval[out]: None
- @note:
- @author: AresXu
- @version: v1.0.0
*/
char WriteOneByteToRingBuffer(stRingBuff *ringBuf,char data)
{
if (ringBuf == NULL)
{
printf("pointer is null\r\n");
return;
}
if(IsRingBufferFull(ringBuf)) //Before writing, check whether the queue is full
{
return FALSE;
}
ringBuf->buffer[ringBuf->in] = data;
ringBuf->in = (++ringBuf->in) % RING_BUFF_SIZE; //防止越界
return TRUE;
}
When writing data, it is necessary to judge whether the queue is full,When it is full, it cannot be written.
Determine if the queue is full
/**
- @brief: Check whether the ring queue is full
- @param[in]: None
- @retval[out]: None
- @note:
- @author: AresXu
- @version: v1.0.0
*/
bool IsRingBufferFull(stRingBuff *ringBuf)
{
if (ringBuf == NULL)
{
printf("pointer is null\r\n");
return;
}
if(((ringBuf->in+1) % RING_BUFF_SIZE) == ringBuf->out)
{
// printf("Ring buffer is Full\r\n");
return TRUE;
}
return FALSE;
}
当写满时,The read and write positions are also equal,Can't tell if it's full.There are two solutions to this situation:
The data structure adds a variable to count the number of data written
像这种((ringBuf->in+1) % RING_BUFF_SIZE) == ringBuf->out,Free up a byte to not write data

Read one byte of data
/**
- @brief: Read one byte of data from the circular queue
- @param[in]: None
- @retval[out]: None
- @note:
- @author: AresXu
- @version: v1.0.0
*/
char ReadOneByteFromRingBuffer(stRingBuff *ringBuf,char *data)
{
if (ringBuf == NULL)
{
printf("pointer is null\r\n");
return;
}
if(IsRingBufferEmpty(ringBuf)) //Check if the queue is empty before reading
{
return FALSE;
}
*data = ringBuf->buffer[ringBuf->out];
ringBuf->out = (++ringBuf->out) % RING_BUFF_SIZE; //防止越界
return TRUE;
}
判断队列是否为空 Empty when the write position and the read position are equal
/**
- @brief: Check if the ring queue is empty
- @param[in]: None
- @retval[out]: None
- @author: AresXu
- @version: v1.0.0
*/
bool IsRingBufferEmpty(stRingBuff *ringBuf)
{
if (ringBuf == NULL)
{
printf("pointer is null\r\n");
return;
}
if(ringBuf->in == ringBuf->out) //Empty when the write position and the read position are equal
{
// printf("Ring buffer is Empty\r\n");
return TRUE;
}
return FALSE;
}
Write multiple bytes to the queue
/**
* @brief: 寫lenbytes of data to the circular queue
* @param[in]: None
* @retval[out]: None
* @note:
* @author: AresXu
* @version: v1.0.0
*/
void WriteRingBuffer(stRingBuff *ringBuf,char *writeBuf,unsigned int len)
{
unsigned int i;
if (ringBuf == NULL)
{
printf("pointer is null\r\n");
return;
}
for(i = 0; i < len; i++)
{
WriteOneByteToRingBuffer(ringBuf,writeBuf[i]);
}
}
Read multiple bytes from the queue
/**
* @brief: Read from the circular queuelenbytes of data
* @param[in]: None
* @retval[out]: None
* @note:
* @author: AresXu
* @version: v1.0.0
*/
void ReadRingBuffer(stRingBuff *ringBuf,char *readBuf,unsigned int len)
{
unsigned int i;
if (ringBuf == NULL)
{
printf("pointer is null\r\n");
return;
}
for(i = 0; i < len; i++)
{
ReadOneByteFromRingBuffer(ringBuf,&readBuf[i]);
}
}
Get the length of data that has been written to the queue It is convenient to know how much data to read from the queue after receiving.
/**
* @brief: Get the length that has been written
* @param[in]: None
* @retval[out]: None
* @note:
* @author: AresXu
* @version: v1.0.0
*/
int GetRingBufferLength(stRingBuff *ringBuf)
{
if (ringBuf == NULL)
{
printf("pointer is null\r\n");
return;
}
return (ringBuf->in - ringBuf->out + RING_BUFF_SIZE) % RING_BUFF_SIZE;
}
画个图,You can know why by drawing and you can judge the length of the write.
到STM32上测试
Serial port receiving part:
static stRingBuff g_stRingBuffer = {0,0,0};
static u8 g_recvFinshFlag = 0;
stRingBuff *GetRingBufferStruct(void)
{
return &g_stRingBuffer;
}
u8 *IsUsart1RecvFinsh(void)
{
return &g_recvFinshFlag;
}
void USART1_IRQHandler(void) //串口1中断服务程序
{
u8 res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
res = USART_ReceiveData(USART1); //读取接收到的数据
WriteOneByteToRingBuffer(GetRingBufferStruct(),res);
}
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) //空闲中断
{
USART_ReceiveData(USART1); //清除空闲中断
g_recvFinshFlag = 1; //接收完成
}
}
主函数:
int main(void)
{
char readBuffer[100];
u16 t;
u16 len;
u16 times = 0;
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
LED_Init(); //LED端口初始化
KEY_Init(); //初始化与按键连接的硬件接口
while(1)
{
times++;
if(*IsUsart1RecvFinsh())
{
ReadRingBuffer(GetRingBufferStruct(),readBuffer,GetRingBufferLength(GetRingBufferStruct()));
printf("%s",readBuffer);
memset(readBuffer,0,100);
*IsUsart1RecvFinsh() = 0;
}
if(times%500==0)
LED0=!LED0;
delay_ms(1);
}
}
串口收发测试

版权声明:本文来源网络,免费传达知识,版权归原作者所有.如涉及作品版权问题,请联系我进行删除.
‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧ END ‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧
关注我的微信公众号,回复“加群”按规则加入技术交流群.
点击“阅读原文”查看更多分享,欢迎点分享、收藏、点赞、在看.
边栏推荐
- MySQL---Create and manage databases and data tables
- leetcode 665. Non-decreasing Array 非递减数列(中等)
- Go record - slice
- 常用的安全渗透测试工具(渗透测试工具)
- Unity 之 音频类型和编码格式介绍
- 基于STM32 环形队列来实现串口接收数据
- Shell script quick start to actual combat -02
- linux查看redis版本命令(linux查看mysql版本号)
- 35道MySQL面试必问题图解,这样也太好理解了吧
- -xms -xmx(information value)
猜你喜欢
Getting Started with Tkinter
Count characters in UTF-8 string function
The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
This 985 professor is on fire!After 10 years of Ph.D. supervisor, no one has graduated with a Ph.D.!
财务盈利、偿债能力指标
顺序表的实现
程序员如何学习开源项目,这篇文章告诉你
利用反射实现一个管理对象信息的简单框架
Tkinter 入门之旅
Shell script quick start to actual combat -02
随机推荐
c语言解析json字符串(json对象转化为字符串)
NVIDIA has begun testing graphics products with AD106 and AD107 GPU cores
嵌入式开发没有激情了,正常吗?
【AcWing】第 62 场周赛 【2022.07.30】
MySQL---单行函数
利用反射实现一个管理对象信息的简单框架
API for JD.com to obtain historical price information of commodities
Write a database document management tool based on WPF repeating the wheel (1)
MySQL---Create and manage databases and data tables
微信小程序的路由拦截
leetcode 665. Non-decreasing Array
Three. Introduction to js
京东按关键字搜索商品 API
multithreaded lock
Routing interception of WeChat applet
The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
Tkinter 入门之旅
关注!海泰方圆加入《个人信息保护自律公约》
给定一个ip地址,子网掩码怎么算网络号(如何获取ip地址和子网掩码)
STM32 full series development firmware installation guide under Arduino framework