当前位置:网站首页>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++;
}
}
}
边栏推荐
- 【Try to Hack】内网基础
- Cache of laravel
- Let Arduino support nuvotom Xintang
- SQL injection less26 (filter spaces and comments, and use error injection without spaces)
- Fusion cloud native, enabling new mileage | 2022 open atom global open source summit cloud native sub forum successfully held
- 让arduino支持nuvotom新唐
- Exploration on cache design optimization of community like business
- 微创电生理通过注册:年营收1.9亿 微创批量生产上市企业
- 易观分析:以用户为中心提升手机银行用户体验,助力用户价值增长
- Tencent two sides: @bean and @component are used in the same class, what will happen?
猜你喜欢

行业落地呈现新进展 | 2022 开放原子全球开源峰会 OpenAtom OpenHarmony 分论坛圆满召开

金九银十 再不卷就来不及了

MySQL之知识点(十三)

Use json.stringify() to format data

Great! Jd.com developed the highly available website construction technology PDF recommended by the first brother. Prepare the water and chew it slowly

新零售电商O2O模式解析

Implementation method of mouse hover, click and double click in ue4/5

Tencent two sides: @bean and @component are used in the same class, what will happen?

GMT installation and use

How to realize more multimedia functions through the ffmpeg library and NaPi mechanism integrated in openharmony system?
随机推荐
How to realize more multimedia functions through the ffmpeg library and NaPi mechanism integrated in openharmony system?
Latex matrix is simple to use
SQL注入 Less23(过滤注释符)
IRBuilder
产学研用 共建开源人才生态 | 2022 开放原子全球开源峰会教育分论坛圆满召开
PHP ⽉ the simplest way to add and subtract ⽅
Solve the PHP prompt warning: division by zero in error
Uninstall Navicat: genuine MySQL official client, really fragrant!
To build agile teams, these methods are indispensable
Developing NES games with C language (cc65) 04. Complete background
Hongjiu fruit passed the hearing: five month operating profit of 900million Ali and China agricultural reclamation are shareholders
Knowledge points of MySQL (13)
Analysis of new retail e-commerce o2o model
C for循环内定义int i变量出现的重定义问题
Why do enterprises need the ability of enterprise knowledge management?
1331. 数组序号转换 : 简单模拟题
FlexPro软件:生产、研究和开发中的测量数据分析
PHP date calculation operation processing, the current date plus one day and the specified date minus one day
MarkDown简明语法手册
开源汇智创未来 | 2022 开放原子全球开源峰会 OpenAtom openEuler 分论坛圆满召开