2022-10-25 09:31 AM
2022-10-25 09:37 AM
Basic idea: when you ask for help with your code, show your code.
2022-10-25 12:58 PM
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.
2022-10-25 01:13 PM
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?
2022-10-25 02:41 PM
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.
2022-10-25 11:55 PM
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.
2022-10-26 01:15 AM
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.
2022-10-27 12:42 PM
Thank you for reply. After the 24ms there is no other transmission.
2022-10-27 01:01 PM
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.
2022-10-27 07:10 PM
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;
}