Skip to main content
Tvan.7
Associate
January 13, 2019
Question

How to "return" HAL_UART_Receive buffer?

  • January 13, 2019
  • 3 replies
  • 1282 views

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"

This topic has been closed for replies.

3 replies

S.Ma
Principal
January 13, 2019

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.

Tesla DeLorean
Guru
January 13, 2019
  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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
Tvan.7
Tvan.7Author
Associate
January 13, 2019

Are you talking about line 1 or 4?