Adding a LF to a string of unkown length
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-02-05 10:59 AM
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
Solved! Go to Solution.
- Labels:
-
UART-USART
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-02-05 11:05 AM
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?
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-02-05 11:05 AM
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?
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-02-05 11:19 AM
Hello clive1,
yeah, this is what I was worried about. memcpy() is a great suggestion!
Thx! :)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-02-05 11:23 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-02-06 02:22 AM
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);
}
}
}