Skip to main content
Benjamin Brammer
Senior II
February 5, 2020
Solved

Adding a LF to a string of unkown length

  • February 5, 2020
  • 2 replies
  • 893 views

Hello Everybody,

I am wondering if this is good code when using an embedded system or if I should avoid adding a LF to an unkown string like I do it here, since this could overwerite other data in the following address:

void USART_transmit_message(volatile uint8_t *message)
{
	if(!strchr(message, '\n')) strcat(message,"\n");
 
	strncpy(USARTBufferTX, message, sizeof(USARTBufferTX));
 
	if(huart1.gState == HAL_UART_STATE_READY)
	{
		if(HAL_UART_Transmit_DMA(&huart1, &USARTBufferTX, strlen(message))!= HAL_OK)
		{
			Error_Handler();
		}
	}
 
}

best regards

Benjamin

This topic has been closed for replies.
Best answer by Tesla DeLorean

Makes some rather dangerous assumptions about where the string is located, and how long the memory buffer really is

Why not determine the length ONCE

memcpy() that into the USARTBufferTX if it fits, and add the LF there?

2 replies

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
February 5, 2020

Makes some rather dangerous assumptions about where the string is located, and how long the memory buffer really is

Why not determine the length ONCE

memcpy() that into the USARTBufferTX if it fits, and add the LF there?

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Benjamin Brammer
Senior II
February 5, 2020

Hello clive1,

yeah, this is what I was worried about. memcpy() is a great suggestion!

Thx! :)

S.Ma
Principal
February 5, 2020

Try to rewrite your function to be lean:

  • Test if the string doesn't include '/n' and as you sweep the string, find out the string length
  • Use DMA to directly send the string out (without the null terminated string character)
  • If '/n' is needed, send this char manually after DMA is completed. (avoid using DMA here for a single byte transfer)

Using global variable referenced directly in a function is very good for learning maintenance cost and bug hunting down the road...

Moving the data from one memory place to another then another is a good way to justify MIPS and power

Benjamin Brammer
Senior II
February 6, 2020

why should I not send short strings or even one character over UART with DMA?

When I receive characters over UART, I always receive one and test if it is LF or CR. With this method I can easily process strings of an unkwon size:

void HAL_UART_RxCpltCallback (UART_HandleTypeDef * husart)
{
	if(husart->Instance == USART1)
	{
		if(tempRxIndex < USART_BUFFER_SIZE)
		{
			if(USARTTempRX[0] == '\n' || USARTTempRX[0] == '\r')
			{
				if(tempRxIndex !=0)		// decode received message
				{
 
					USART_transmit_message(USARTBufferRX);		//loopback for testing
					tempRxIndex = 0;
					memset(USARTBufferRX, 0, USART_BUFFER_SIZE);
				}
				else
				{
					tempRxIndex = 0;
					memset(USARTBufferRX, 0, USART_BUFFER_SIZE);
					return;
				}
			}
			else
			{
				USARTBufferRX[tempRxIndex] = USARTTempRX[0];
				tempRxIndex++;
			}
 
		}
		else if(tempRxIndex >= USART_BUFFER_SIZE)		// buffer overflow
		{
			tempRxIndex = 0;
			memset(USARTBufferRX, 0, USART_BUFFER_SIZE);
		}
	}
 
}