当前位置:网站首页>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
边栏推荐
- Analysis report on the "fourteenth five year plan" and the latest development trend of China's medical information industry from 2022 to 2028
- Research and Analysis on the development of China's Melamine Industry from 2022 to 2028 and market prospect forecast report
- InfoQ 极客传媒 15 周年庆征文|简述构建微服务架构的四大挑战
- 应用最广泛的动态路由协议:OSPF
- [Part VI] source code analysis and application details of countdownlatch [key]
- 認識的幾比特清華同學都離職了……
- 基于51单片机的酒精检测仪
- Analysis report on production and marketing demand and investment forecast of China's Melamine Industry from 2022 to 2028
- Ten key defensive points in the detailed attack and defense drill
- Insight into China's smart medical industry in 2022
猜你喜欢
MySQL case when then function use
深度学习-神经网络:卷积的实现方法【直接法(精度没损失)、GEMM(矩阵乘法,精度没损失)、FFT(傅里叶变换,精度有损失)、Winograd(精度有损失)】
基于51单片机的酒精检测仪
ASP. Net core Middleware
度量学习(Metric Learning)【AMSoftmax、Arcface】
C language: how to give an alias to a global variable?
The annual salary of 500000 is one line, and the annual salary of 1million is another line
2022 heavyweight: growth law - skillfully use digital marketing to break through enterprise difficulties
The most widely used dynamic routing protocol: OSPF
Leetcode 890 finding and replacing patterns [map] the leetcode path of heroding
随机推荐
Inventory of CV neural network models from 2021 to 2022
Photoshop:PS如何实现放大图片不模糊
Wechat applet withdrawal function
China barcode decoder market trend report, technical innovation and market forecast
Alcohol detector based on 51 single chip microcomputer
MySQL case when then function use
深度学习-神经网络:卷积的实现方法【直接法(精度没损失)、GEMM(矩阵乘法,精度没损失)、FFT(傅里叶变换,精度有损失)、Winograd(精度有损失)】
[890. find and replace mode]
csredis-in-asp. Net core theory practice - use examples
反走样/抗锯齿技术
China's elastic belt market trend report, technical dynamic innovation and market forecast
Sword finger offer series - 47 Maximum value of gifts
China Aquatic Fitness equipment market trend report, technical innovation and market forecast
PHP删除二维数组中相同项的数据
Embedded pipeline out of the box
Zhengzhou University of light industry -- development and sharing of harmonyos pet health system
【LeetCode】5. 最长回文子串
China's new generation information technology industry "14th five year plan" special planning and innovation strategic direction report 2022 ~ 2028
Matters of parent-child class construction method in inheritance
Mysql case when then函数使用