cancel
Showing results for 
Search instead for 
Did you mean: 

How to "return" HAL_UART_Receive buffer?

Tvan.7
Associate

I'm using the HAL_UART_Receive function for receiving strings and I need to check if the receiving strings matches with the strings that I want to receive.

First I want to create the char for the string in heap:

char *response = (char *)malloc(RN2483_MAX_BUFF);

Then i'm calling the function:

RN2483_response(&huart1, (uint8_t *)response);

This function reads from RX buffer until he reads '\n' character:

// Reads from the RX buffer into response until '\n' 
static int RN2483_response(UART_HandleTypeDef *huart, uint8_t *response)
{
	int i;
	huart->pRxBuffPtr = 0; // delete the last value
 
	// while receiving transfer is not complete
	while((*huart->pRxBuffPtr) != '\n')
	{
		HAL_UART_Receive(&huart1, (uint8_t *)response, RN2483_MAX_BUFF, 1);
		huart->pRxBuffPtr++;
		i++;
	}
 
	if (i <= 0)
		return RN2483_ERR_PANIC;
	else
		return i;
}

 How can I get the receiving string in de response character array?

EDIT:

Looks like it's almost working.. The string is just a bit off in the response array:

I have"\nk\r"in response while I receive "ok\r\n"

3 REPLIES 3
S.Ma
Principal

First, avoid using malloc for embedded, just assign at first a static array

The incoming message length is unknown, so the HAL function may not be ideal, if need to use it, receive byte by byte and look for your \n character.

For USART, LL (low layer) maybe more suitable for this mode of console/printf operation.

  1. huart->pRxBuffPtr = 0; // delete the last value
  2.  
  3. // while receiving transfer is not complete
  4. while((*huart->pRxBuffPtr) != '\n')

Surely accessing a NULL pointer is not the intended goal?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Are you talking about line 1 or 4?