cancel
Showing results for 
Search instead for 
Did you mean: 

Adding a LF to a string of unkown length

Benjamin Brammer
Senior II

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

1 ACCEPTED SOLUTION

Accepted Solutions

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 Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

4 REPLIES 4

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 Venmo
Up vote any posts that you find helpful, it shows what's working..

Hello clive1,

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

Thx! :)

S.Ma
Principal

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

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);
		}
	}
 
}