当前位置:网站首页>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++;
}
}
}
边栏推荐
- 用C语言开发NES游戏(CC65)07、控制器
- SQL injection less24 (secondary injection)
- To build agile teams, these methods are indispensable
- Zhou Hongyi talks about Internet thinking: users, not customers
- Deployment之滚动更新策略。
- MySQL之知识点(十三)
- Huawei releases harmonyos 3 and all scene new products, and the smart experience goes further
- 遭受痛苦和创伤后的四种本真姿态 齐泽克
- 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
- 用C语言开发NES游戏(CC65)11、Metatiles
猜你喜欢

AsiaInfo technology released antdb7.0, a "Telecom grade" core transaction database, to help government and enterprises "trust" to create the future!

用C语言开发NES游戏(CC65)07、控制器

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

要想组建敏捷团队,这些方法不可少

Open source office (ospo) unveils Secrets
![[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](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[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

Uninstall Navicat: genuine MySQL official client, really fragrant!

Minimally invasive electrophysiology has passed the registration: a listed enterprise with annual revenue of 190million minimally invasive mass production

Redis实现分布式锁

Uniapp wechat applet realizes the function of connecting low-power Bluetooth printing
随机推荐
8000 字讲透 OBSA 原理与应用实践
用C语言开发NES游戏(CC65)06、精灵
微创电生理通过注册:年营收1.9亿 微创批量生产上市企业
【Try to Hack】内网基础
Is it overtime to be on duty? Take up legal weapons to protect your legitimate rights and interests. It's time to rectify the working environment
开源社区三十年 | 2022 开放原子全球开源峰会开源社区三十年专题活动圆满召开
Developing NES games with C language (cc65) 04. Complete background
New Oriental's single quarter revenue was 524million US dollars, a year-on-year decrease of 56.8%, and 925 learning centers were reduced
Laravel $object->updated_ At returns the carbon object. How to return the normal time format
PHP date calculation operation processing, the current date plus one day and the specified date minus one day
SQL injection LESS18 (header injection + error injection)
Is it difficult for cloud native machine learning to land? Lingqueyun helps enterprises quickly apply mlops
Introduction to several methods of keeping two decimal places in PHP
【一知半解】零值拷贝
Arduino Pro Mini atmega328p connect and light the first LED (at the same time, record the problem of burning failure stk500_recv)
SQL injection less24 (secondary injection)
C for循环内定义int i变量出现的重定义问题
Tik tok "founder" Yang Luyu, farewell byte?
牛客网二叉树题解
SuperMap arsurvey license module division