当前位置:网站首页>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
- 图书馆自动预约脚本
- FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be depreca
- 十三. 实战——常用依赖的作用
- FlexPro软件:生产、研究和开发中的测量数据分析
- Zadig v1.13.0 believes in the power of openness, and workflow connects all values
- [cute new problem solving] climb stairs
- Minimally invasive electrophysiology has passed the registration: a listed enterprise with annual revenue of 190million minimally invasive mass production
- Distributed timer
猜你喜欢

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

卸载 Navicat:正版 MySQL 官方客户端,真香!

Basic use of JSON server

用C语言开发NES游戏(CC65)08、背景 碰撞

03 pyechars 直角坐标系图表(示例代码+效果图)

Kuzaobao: summary of Web3 encryption industry news on July 13

HMS core audio editing service supports 7 kinds of audio effects to help one-stop audio processing

leetcode:704二分查找

leetcode:数组

MarkDown简明语法手册
随机推荐
[cute new problem solving] climb stairs
Developing NES games with C language (cc65) 09, scrolling
聚变云原生,赋能新里程 | 2022 开放原子全球开源峰会云原生分论坛圆满召开
Uninstall Navicat: genuine MySQL official client, really fragrant!
GMT installation and use
新东方单季营收5.24亿美元同比降56.8% 学习中心减少925间
用C语言开发NES游戏(CC65)07、控制器(和精灵碰撞)
MSP430 开发中遇到的坑(待续)
奥浦迈生物通过注册:半年营收1.47亿 国寿成达与达晨是股东
用C语言开发NES游戏(CC65)11、Metatiles
Redis实现分布式锁
一台电脑上 多个项目公用一个 公私钥对拉取gerrit服务器代码
The usage and Simulation Implementation of vector in STL
Zadig v1.13.0 believes in the power of openness, and workflow connects all values
设计一个线程池
leetcode:704二分查找
Unity 安装 Device Simulator
Anhui Jingzhun: Beidou satellite synchronous clock | Beidou synchronous clock | NTP network clock server
[nuxt 3] (XII) project directory structure 3
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