当前位置:网站首页>stm32 回环结构接收串口数据并处理
stm32 回环结构接收串口数据并处理
2022-07-28 11:40:00 【yi_tianchou】
使用stm32 通过串口对接一款数据采集终端,对终端发送一条指令,串口会返回不定量的数据,
刚开始使用串口接收中断的方式处理数据,发现处理速度较慢,出现丢帧情况。
进而改成回环结构接收数据。
typedef struct
{
short write_point;//写指针
u8 Reader_RX_Buffer_Second[4096];//存储空间
short read_point;//读指针
u8 loop;//如果写指针到末端的时候,重头开始写表示
}READER_UART_REC_STRUCT_T;
串口接收方法:
void DEBUG_Reader_IRQHandler(void)
{
u8_t res;
if(USART_GetITStatus(DEBUG_Reader, USART_IT_RXNE) != RESET)//接收到数据
{
res =USART_ReceiveData(DEBUG_Reader);//;读取接收到的数据USART2->DR
if(reader_uart_rec_t.loop<1 && reader_uart_rec_t.write_point>=reader_uart_rec_t.read_point)
{
reader_uart_rec_t.Reader_RX_Buffer_Second[reader_uart_rec_t.write_point++] = res;
if(reader_uart_rec_t.write_point == 4095)
{
reader_uart_rec_t.write_point = 0;
reader_uart_rec_t.loop = 1;
}
}else if(reader_uart_rec_t.write_point<reader_uart_rec_t.read_point)
{
reader_uart_rec_t.Reader_RX_Buffer_Second[reader_uart_rec_t.write_point++] = res;
}
USART_ClearITPendingBit(DEBUG_Reader, USART_IT_RXNE);
}
}
数据解析方法:
main()
{
//我的协议最小数据长度为6
if(reader_uart_rec_t.write_point>(reader_uart_rec_t.read_point+5) || reader_uart_rec_t.loop>0)
{
//数据处理方法
AnalyData(&reader_uart_rec_t);
}
}
typedef struct
{
u8 head;
u8 len;
u8 address;
u8 cmd;
}RECEIVE_EPC_DATA_HEAD_T;
void AnalyData(READER_UART_REC_STRUCT_T* reader_uart_rec)
{
RECEIVE_DATA_HEAD_T head;
if(reader_uart_rec->loop == 1)
{
len = 4095+reader_uart_rec->write_point - reader_uart_rec->read_point;
}else
{
len = reader_uart_rec->write_point - reader_uart_rec->read_point;
}
while(len>0)
{
len--;
if(reader_uart_rec->read_point >= 4096)
{
reader_uart_rec->read_point = 0;
reader_uart_rec->loop = 0;
}
//0xA0 是协议报头 循环查找报头,报头不对则移动指针,正确则判断符不符合协议头,然后再处理数据
if(reader_uart_rec->Reader_RX_Buffer_Second[reader_uart_rec->read_point] == 0xA0)
{
memcpy(&head,&reader_uart_rec->Reader_RX_Buffer_Second[reader_uart_rec->read_point],sizeof(RECEIVE_DATA_HEAD_T));
switch(head.cmd)
{
case 0x1:
//处理数据
if((reader_uart_rec->read_point+ head.len+2)>4095)
{
reader_uart_rec->read_point = reader_uart_rec->read_point+ head.len+2-4095;
reader_uart_rec->loop = 0;
}else
{
reader_uart_rec->read_point = reader_uart_rec->read_point+ head.len+2;
}
len = len - head.len-1;
break;
default:
reader_uart_rec->read_point++;//如果协议不在处理范围,可能是错误数据或者出现了丢帧,则read下标后移
break;
}
}else
{
//如果数据不符合包头则read下标后移继续找报头
reader_uart_rec->read_point++;
}
}
}
边栏推荐
- Developing NES game (cc65) 03 and VRAM buffer with C language
- 用C语言开发NES游戏(CC65)06、精灵
- Sub database and sub table may not be suitable for your system. Let's talk about how to choose sub database and sub table and newsql
- On Governance and innovation | the openanolis sub forum of the 2022 open atom global open source summit was successfully held
- Four authentic postures after suffering and trauma, Zizek
- 金九银十 再不卷就来不及了
- 30 years of open source community | 2022 open atom global open source summit 30 years of special activities of open source community were successfully held
- Develop NES game (cc65) 07 and controller with C language (collision with spirit)
- The usage and Simulation Implementation of vector in STL
- 数字经济时代的开源数据库创新 | 2022 开放原子全球开源峰会数据库分论坛圆满召开
猜你喜欢

Is it difficult for cloud native machine learning to land? Lingqueyun helps enterprises quickly apply mlops

leetcode:数组

What SaaS architecture design does a software architect need to know?

How to realize more multimedia functions through the ffmpeg library and NaPi mechanism integrated in openharmony system?

新东方单季营收5.24亿美元同比降56.8% 学习中心减少925间

Uniapp wechat applet realizes the function of connecting low-power Bluetooth printing

Jinshanyun rushes to the dual main listing of Hong Kong stocks: the annual revenue of 9billion is a project supported by Lei Jun

Come to tdengine Developer Conference and have an insight into the future trend of data technology development

DIY system home page, your personalized needs PRO system to meet!

【萌新解题】爬楼梯
随机推荐
Zhou Hongyi talks about Internet thinking: users, not customers
Zadig v1.13.0 believes in the power of openness, and workflow connects all values
用C语言开发NES游戏(CC65)08、背景 碰撞
How can a novice quickly complete the establishment of a website? Come to the free "fitting room" experience
SQL注入 Less24(二次注入)
Yan Ji lost Beijing again, and more than half of the stores in the country were closed
Developing NES game (cc65) 07 and controller with C language
Knowledge points of MySQL (13)
云原生机器学习落地难?灵雀云助力企业快速应用 MLOps
SQL注入 Less23(过滤注释符)
Open source office (ospo) unveils Secrets
What SaaS architecture design does a software architect need to know?
8000 字讲透 OBSA 原理与应用实践
SQL注入 Less18(头部注入+报错注入)
用arduino开发ESP8266 搭建开发环境
With the continuous waves of infringement, the U.S. patent and trademark office began to study the impact of NFT on copyright
Anhui Jingzhun: Beidou satellite synchronous clock | Beidou synchronous clock | NTP network clock server
Uninstall Navicat: genuine MySQL official client, really fragrant!
IRBuilder
[dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou