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

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

 Insert picture description here

原网站

版权声明
本文为[weixin_ sixty-five million four hundred and eighty-nine thousan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202281118525364.html