当前位置:网站首页>STM32 loopback structure receives and processes serial port data
STM32 loopback structure receives and processes serial port data
2022-07-28 12:41:00 【yi_ tianchou】
Use stm32 Connect a data acquisition terminal through serial port , Send an instruction to the terminal , The serial port will return non quantitative data ,
Just started to use the serial port to receive interrupts to process data , It is found that the processing speed is slow , Frame loss occurs .
Then it is changed into a loopback structure to receive data .
typedef struct
{
short write_point;// The write pointer
u8 Reader_RX_Buffer_Second[4096];// Storage space
short read_point;// Read pointer
u8 loop;// If the writing pointer reaches the end , Start writing again to indicate
}READER_UART_REC_STRUCT_T;
Serial port receiving method :
void DEBUG_Reader_IRQHandler(void)
{
u8_t res;
if(USART_GetITStatus(DEBUG_Reader, USART_IT_RXNE) != RESET)// Data received
{
res =USART_ReceiveData(DEBUG_Reader);//; Read received data 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);
}
}
Data analysis method :
main()
{
// The minimum data length of my protocol is 6
if(reader_uart_rec_t.write_point>(reader_uart_rec_t.read_point+5) || reader_uart_rec_t.loop>0)
{
// Data processing method
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 Is the protocol header Loop through headers , If the header is wrong, move the pointer , If it is correct, the identifier does not conform to the protocol header , And then process the data
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:
// Processing data
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++;// If the agreement is not within the scope of processing , It may be wrong data or frame loss , be read The subscript backwards
break;
}
}else
{
// If the data does not conform to the Baotou read Move the subscript back and continue to find the header
reader_uart_rec->read_point++;
}
}
}
边栏推荐
- Unity 安装 Device Simulator
- Brief discussion on open source OS distribution
- 金山云冲刺港股拟双重主要上市:年营收90亿 为雷军力挺项目
- Developing NES games with C language (cc65) 08. Background collision
- 用C语言开发NES游戏(CC65)08、背景 碰撞
- Developing NES games with C language (cc65) 11. Metatiles
- 合并表格行---三层for循环遍历数据
- How does musk lay off staff?
- Fusion cloud native, enabling new mileage | 2022 open atom global open source summit cloud native sub forum successfully held
- HMS core audio editing service supports 7 kinds of audio effects to help one-stop audio processing
猜你喜欢

用C语言开发NES游戏(CC65)11、Metatiles

Foam exploded three times, why did Luo Yonghao put all his eggs in one basket to do ar?

01 pyechars 特性、版本、安装介绍

C for循环内定义int i变量出现的重定义问题

用C语言开发NES游戏(CC65)10、游戏循环

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

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

Use json.stringify() to format data

【萌新解题】爬楼梯

FlexPro软件:生产、研究和开发中的测量数据分析
随机推荐
用C语言开发NES游戏(CC65)11、Metatiles
利用依赖包直接实现分页、SQL语句
用C语言开发NES游戏(CC65)07、控制器(和精灵碰撞)
Uniapp wechat applet realizes the function of connecting low-power Bluetooth printing
03 pyechars 直角坐标系图表(示例代码+效果图)
一台电脑上 多个项目公用一个 公私钥对拉取gerrit服务器代码
Configure jupyter remote server
HMS core audio editing service supports 7 kinds of audio effects to help one-stop audio processing
Developing NES games with C language (cc65) 08. Background collision
Baidu map API adds information window circularly. The window only opens at the last window position and the window information content is the same solution
用C语言开发NES游戏(CC65)03、VRAM缓冲区
1331. Array sequence number conversion: simple simulation question
Some API interfaces purchased by Yiwu hope to bring you some help
GMT installation and use
Yan Ji lost Beijing again, and more than half of the stores in the country were closed
Industry, University, research and application jointly build an open source talent ecosystem | the 2022 open atom global open source summit education sub forum was successfully held
The openatom openharmony sub forum was successfully held, and ecological and industrial development entered a new journey
Most of the interfaces of Tiktok are already available, and more interfaces are still open. Please look forward to it
SuperMap itablet license module division
[try to hack] UDF raises rights