Skip to main content
cjaya.1
Associate III
October 25, 2022
Question

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?

  • October 25, 2022
  • 16 replies
  • 3809 views

..

This topic has been closed for replies.

16 replies

gbm
Super User
October 25, 2022

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
cjaya.1
cjaya.1Author
Associate III
October 25, 2022
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.

Karl Yamashita
Principal
October 28, 2022

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

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
Bob S
Super User
October 25, 2022

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
Super User
October 25, 2022

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
cjaya.1Author
Associate III
October 26, 2022

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
Super User
October 26, 2022

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
cjaya.1Author
Associate III
October 27, 2022

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

cjaya.1
cjaya.1Author
Associate III
October 27, 2022

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.

AScha.3
Super User
October 28, 2022

try:

char buffer[30];

.... &

 HAL_UART_Transmit(UART_HandleType ....) // Send an amount of data in blocking mode.

If you feel a post has answered your question, please click on " Best Answer ".
cjaya.1
cjaya.1Author
Associate III
November 2, 2022

thank you for the replies. I even initialized the char buffer[30] = {0} and then use strncat(). Also used HAL_UART_Transmit() but it just one character not transmitting. It transmitting most of it just misses one character from the string. Its transmitting "setfrequency 10" instead of "100". Dont know why?. Any suggestions?

Pavel A.
November 2, 2022

> transmitting most of it just misses one character from the string.

Do you use UART (TTL) to USB adapter?