当前位置:网站首页>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

边栏推荐
- 反走样/抗锯齿技术
- The most widely used dynamic routing protocol: OSPF
- Hostvars in ansible
- 管线中的坐标变换
- ShardingSphere-proxy-5.0.0部署之分表实现(一)
- 【LeetCode】102. Sequence traversal of binary tree
- Shardingsphere-proxy-5.0.0 deployment table implementation (I)
- Common rendering pipeline grooming
- [recommended collection] easy to understand graphic network knowledge - Part 1
- Go时间格式化 赋值
猜你喜欢

Design a MySQL table for message queue to store message data

Summary of MySQL foundation view

反走样/抗锯齿技术

JVM foundation - > three ⾊ mark

Shardingsphere-proxy-5.0.0 deployment table implementation (I)

ASP. Net core Middleware

Hostvars in ansible
![[Part 8] semaphore source code analysis and application details [key points]](/img/e2/05c08435d60564aaa1172d2d574675.jpg)
[Part 8] semaphore source code analysis and application details [key points]

Photoshop:ps how to enlarge a picture without blurring

应用最广泛的动态路由协议:OSPF
随机推荐
【LeetCode】102. Sequence traversal of binary tree
数字藏品的发展趋势!
Theory + practice will help you master the dynamic programming method
LeetCode 146. LRU cache
Report on the "fourteenth five year plan" and strategic strategy recommendations for China's intellectual property protection industry 2022 ~ 2028
Common rendering pipeline grooming
JVM foundation - > three ⾊ mark
80 lines of code to realize simple rxjs
PHP删除二维数组中相同项的数据
InfoQ geek media's 15th anniversary solicitation | brief introduction to the four challenges of building a micro service architecture
[890. find and replace mode]
Is it safe to open an account in flush? How to open an account online to buy stocks
证券开户有风险吗?怎么开户安全呢?
China's alternative sports equipment market trend report, technology dynamic innovation and market forecast
DETR(Detection with Transformers) 学习笔记
Shardingsphere-proxy-5.0.0 deployment table implementation (I)
Summary of MySQL foundation view
ShardingSphere-proxy-5.0.0部署之分表实现(一)
Global and Chinese Melamine Industry Development Research and prospect trend report 2022-2028
LeetCode —— 26. Remove duplicates from an ordered array