cancel
Showing results for 
Search instead for 
Did you mean: 

I concanate two strings using strcat. Then I transmit that strings through uart2 but when I check in the putty terminal the string is showing half of it. It is not showing full string on the putty terminal. Any idea about this?

cjaya.1
Associate II
 
16 REPLIES 16
gbm
Lead III

Basic idea: when you ask for help with your code, show your code.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
char *buffer[20];
 
for(char *ptr = strtok((char*)rx, "  "); ptr; ptr = strtok(NULL, "  "))
  {
  if(strncmp((char*)ptr, "send", 4) == 0)
    continue;
 
 strcat(buffer, ptr);
 strcat(buffer, "  ");
 }
strcat(buffer, "\n");
 
HAL_UART_Transmit_IT(&huart2, (uint8_t*)buffer, strlen(buffer));
HAL_Delay(24);
 
 

so the rx buffer hold a string like this "send setfrequency 100\n" . I am seperating it out to hold the buffer with "setfrequency 100\n" and then transmit it. but it just show "setfr" on the putty terminal. It does not show the full string.

Bob S
Principal

Never use strcat() (or strcpy() or sprintf()). They allow buffer overflows. Use strncat() (and strncpy() and snprintf()). Your code as above won't overflow buffer[], but some change in the code later by you or someone else MIGHT.

Second - the code you show doesn't initialize buffer[] to anything. If buffer[] is a global variable, then it will be initialized to all zeros when the program starts. But the SECOND time through the code you've shown will append to the existing buffer[] contents and overflow the buffer. If buffer[] is declared local to your function then its contents are UNKNOWN, whatever was on the stack at that location when your function was called.

Third - did you verify with a debugger that when you get to the HAL_UART_Transmit_IT() call, buffer[] contains the full string that you expect, with no NULL characters in the middle?

gbm
Lead III

The first thing to do is to get rid of all the warning messages produced by the compiler, and it seems to me you've got a lot of them with this code. buffer[] is a vector of pointers to char. In your code it's used as a vector of chars. It's not initialized, so if it's declared inside of function, it contains some random thrash. Catenating a string with an unknown buffer content doesn't seem reasonable.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
cjaya.1
Associate II

Thank you for the replies. Also, I wanted to pointed out when I debug I get the full string display on the putty terminal. It is bit strange.

gbm
Lead III

Not strange at all. You didn't show in the code fragment above what you are doing after 24 ms passed from transmission start, so if baud rate is low then maybe you overwrite the buffer with some other stuff while the transfer is still ongoing.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
cjaya.1
Associate II

Thank you for reply. After the 24ms there is no other transmission.

cjaya.1
Associate II

so dont know what is happening. When I debugging it, I checked the string to see if there is NULL character in the middle. I cant see a NULL character in the middle.

Now that I re-read you issue the buffer length is ok.

You pasted code but it doesn't show how it's done within a function so the only thing i can think of is that your buffer array is not global? If it's within a function you either have to make it static or move it outside of the function. Also I don't think you wanted to add spaces after the '\n' character so I've updated the code a little. I ran it on a dev board and it prints what you are looking for

char buffer[20] = {0}; // global
 
void SomeFunction(void)
{
	char str[] = "send setfrequency 100\n";
 
	SendString(str);
}
 
int SendString(char * rx)
{
	char *ptr;
 
	for (ptr = strtok((char*) rx, " "); ptr; ptr = strtok(NULL, " "))
	{
		if ((strncmp(ptr, "send", 4) == 0)) {
			continue;
		}
		strcat(buffer, ptr);
		if(strstr(ptr, "\n") == 0)
		{
			strcat(buffer, " ");
		}
	}
 
	HAL_UART_Transmit_IT(&huart2, (uint8_t*)buffer, strlen(buffer));
 
	return 0;
}

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.