当前位置:网站首页>Record 5 - the serial port of stm32f411ceu6 realizes the sending and receiving of fixed length data and variable length data
Record 5 - the serial port of stm32f411ceu6 realizes the sending and receiving of fixed length data and variable length data
2022-06-12 23:02:00 【weixin_ sixty-five million four hundred and eighty-nine thousan】
Catalog
1. Fixed length data transceiver
Mission
utilize UART Realize the sending of data of any length , After receiving the data, the single chip microcomputer sends the data to the computer as it is
Code implementation
/* USER CODE BEGIN PV */
uint8_t RxBuffer[10];// Define the buffer size of the receive area
/* USER CODE END PV */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_UART_Receive(&huart2,RxBuffer,5,0xFFFF);
HAL_UART_Transmit(&huart2,RxBuffer,5,0xFFFF);
}
/* USER CODE END 3 */
2. Variable length data sending and receiving
Mission
utilize UART Realize the sending of data of any length , After receiving the data, the single chip microcomputer adds a newline character at the end and sends it to the computer .
stm32cubemx To configure
UART And DMA The opening method is described in the previous corresponding section
For details, please see :
https://blog.csdn.net/weixin_65489379/article/details/122783396
Code implementation
main.c in
/* USER CODE BEGIN PV */
uint8_t RxBuffer[100];// Define the buffer size of the receive area
/* USER CODE END PV */
/* USER CODE BEGIN PD */
#include "string.h"// For later use strcat Concatenate two arrays
/* USER CODE END PD */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_DMA(&huart2,RxBuffer,50);// Open the DMA
__HAL_UART_ENABLE_IT(&huart2 ,UART_IT_IDLE);// Enable idle interrupt
/* USER CODE END 2 */
stm32f4xx_it.c in
Manually add the content of idle interrupt
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
extern uint8_t RxBuffer[100];
/* USER CODE END PV */
( stay C In language , Modifier extern Used before the declaration of a variable or function , To illustrate “ This variable / Functions are defined elsewhere , To quote... Here ")
void USART2_IRQHandler(void)
{
/* USER CODE BEGIN USART2_IRQn 0 */
if(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_IDLE) != RESET )// The idle interrupt flag bit obtained is not 0, This indicates that the idle interrupt is triggered
{
char str[]="\r\n";\\ The newline array contains carriage return plus newline
__HAL_UART_CLEAR_IDLEFLAG(&huart2); Clear the free interrupt flag bit
HAL_UART_DMAStop(&huart2);// Data transfer ended , stop it DMA receive
uint8_t len= 50- __HAL_DMA_GET_COUNTER(huart2.hdmarx);// total - Number of bytes left = The actual number of bytes received
strcat((char*)RxBuffer ,str );
HAL_UART_Transmit_DMA(&huart2,RxBuffer,len+2);// Resend the number of bytes received back to the computer
HAL_UART_Receive_DMA(&huart2,RxBuffer,50);// Turn it on again DMA receive
}
/* USER CODE END USART2_IRQn 0 */
Add :
DOS and Windows: need \r\n Only then can it be resolved to a valid carriage return line feed , Otherwise, there is only a return to the beginning of the line or a newline .
Unix and Mac OS X: take \n Resolve to a valid carriage return line feed .
Macintosh/OS 9: take \r Resolve to a valid carriage return line feed .
\r representative Carriage Return;
\n representative Line Feed.
effect
边栏推荐
- Es6+ new content
- 【LeetCode】剑指 Offer II 020. 回文子字符串的个数
- MySQL row to column, column to row, multiple columns to one row, one row to multiple columns
- Avoid using asp Net core 3.0 to inject services for startup classes
- Use of map() function in JS
- Pytorch common parameter initialization methods: [uniform distribution, normal (Gaussian) distribution, Xavier, Kaiming, orthogonal matrix, sparse matrix, constant, identity matrix, zero filling]
- 【LeetCode】5. 最长回文子串
- Matters of parent-child class construction method in inheritance
- China's elastic belt market trend report, technical dynamic innovation and market forecast
- Plusieurs camarades de classe de Tsinghua sont partis...
猜你喜欢
Mysql concat_ws、concat函数使用
Alcohol detector based on 51 single chip microcomputer
80 lines of code to realize simple rxjs
Gb28181 protocol -- alarm
设计消息队列存储消息数据的 MySQL 表格
The annual salary of 500000 is one line, and the annual salary of 1million is another line
Colab tutorial (super detailed version) and colab pro/colab pro+ usage evaluation
[Part 7] source code analysis and application details of cyclicbarrier [key]
[recommended collection] easy to understand graphic network knowledge - Part 1
应用最广泛的动态路由协议:OSPF
随机推荐
【LeetCode】300. Longest ascending subsequence
Several Tsinghua students I know have left
Inventory of CV neural network models from 2021 to 2022
ShardingSphere-proxy-5.0.0部署之分表实现(一)
Colab tutorial (super detailed version) and colab pro/colab pro+ usage evaluation
Embedded pipeline out of the box
【LeetCode】33. Search rotation sort array
Ten key defensive points in the detailed attack and defense drill
Is there any risk in opening a securities account? How to open an account safely?
Research Report on water sports shoes industry - market status analysis and development prospect forecast
Common rendering pipeline grooming
度量学习(Metric Learning)【AMSoftmax、Arcface】
Analysis report on investment and development trend of gap base of Chinese traditional medicine 2022 ~ 2028
Matters of parent-child class construction method in inheritance
Colab tutorial (super detailed version) and colab pro/colab pro+ usage evaluation
Generate the chrysanthemum code of the applet (generate the chrysanthemum code, change the middle logo, change the image size, and add text)
项目里面的traceID的设计
80 lines of code to realize simple rxjs
InfoQ geek media's 15th anniversary solicitation | brief introduction to the four challenges of building a micro service architecture
PyTorch常用参数初始化方法:【均匀分布、正态(高斯)分布、Xavier、kaiming、正交矩阵、稀疏矩阵、常数、单位矩阵、零填充】