cancel
Showing results for 
Search instead for 
Did you mean: 

UART only receive one byte

MÇETİ.1
Associate III

I'm trying to receive more than 1 byte but it receive only 1 byte. Poll method or IT, it doesn't matter. Always receives 1 byte and writes it in rx_buffer[0]. When I search it I found out the reason is timeout, I increase timeout 1000 from 100ms but nothing changes. Can anyone help me please? 

Here is my code for poll

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "string.h"

/* Private variables ---------------------------------------------------------*/

UART_HandleTypeDef huart1;

UART_HandleTypeDef huart6;

DMA_HandleTypeDef hdma_usart1_rx;

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_DMA_Init(void);

static void MX_USART1_UART_Init(void);

static void MX_USART6_UART_Init(void);

USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

char rx_buffer[50];

char tx_buffer[50];

 while (1)

 {

HAL_UART_Receive(&huart1,(uint8_t*)rx_buffer,4,1000);

memcpy(tx_buffer,rx_buffer,4);

HAL_UART_Transmit(&huart6,(uint8_t*)tx_buffer,strlen(tx_buffer),1000);

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

13 REPLIES 13
MÇETİ.1
Associate III
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "string.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
 
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
 
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart6;
DMA_HandleTypeDef hdma_usart1_rx;
DMA_HandleTypeDef hdma_usart6_tx;
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART6_UART_Init(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#define rx_buff 512
#define main_buff 2048
 
uint8_t rx_buffer[rx_buff];
uint8_t main_buffer[main_buff];
 
uint16_t oldPos;
uint16_t newPos;
 
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	if (huart->Instance == USART1)
	{
		oldPos = newPos;  // Update the last position before copying new data
 
		if (oldPos+Size > main_buff)  // If the current position + new data size is greater than the main buffer
		{
			uint16_t datatocopy = main_buff-oldPos;  // find out how much space is left in the main buffer
			memcpy ((uint8_t *)main_buffer+oldPos, rx_buffer, datatocopy);  // copy data in that remaining space
 
			oldPos = 0;  // point to the start of the buffer
			memcpy ((uint8_t *)main_buffer, (uint8_t *)rx_buffer+datatocopy, (Size-datatocopy));  // copy the remaining data
			newPos = (Size-datatocopy);  // update the position
		}
 
		/* if the current position + new data size is less than the main buffer
		 * we will simply copy the data into the buffer and update the position
		 */
		else
		{
			memcpy ((uint8_t *)main_buffer+oldPos, rx_buffer, Size);
			newPos = Size+oldPos;
		}
 
 
		/* start the DMA again */
		HAL_UARTEx_ReceiveToIdle_DMA(&huart1, (uint8_t *) rx_buffer, rx_buff);
		__HAL_DMA_DISABLE_IT(&hdma_usart1_rx, DMA_IT_HT);
		HAL_UART_Transmit_DMA(&huart6,rx_buffer, sizeof(rx_buffer));
	}
}
 
 
 
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
  MX_USART6_UART_Init();
  /* USER CODE BEGIN 2 */
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, rx_buffer, rx_buff);
HAL_UART_Transmit_DMA(&huart6, rx_buffer, sizeof(rx_buffer));
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

Karl Yamashita
Lead III

From what i see, main_buffer is pointless as you are not doing anything with it.

My UART2 is your UART1 and my UART1 is your UART6 which this is working on a Nucleo-L432 board

uint8_t tx_buffer[rx_buff];
 
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	if (huart->Instance == USART2)
	{
	
		/* start the DMA again */
		memset(&tx_buffer, 0, sizeof(tx_buffer)); // clear tx buffer
		memcpy(&tx_buffer, &rx_buffer, Size); // copy only what was received
		
		HAL_UART_Transmit_DMA(&huart1, tx_buffer, Size); // use Size instead of sizeof(rx_buffer)
		
		HAL_UARTEx_ReceiveToIdle_DMA(&huart2, (uint8_t *) rx_buffer, sizeof(rx_buffer));
	}
}

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.
MÇETİ.1
Associate III

Thank you so much!!!! It works!!

You're welcome! Can you please select as best answer?

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.