how to convert received uint8_t array to string in stm32 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-11-11 3:46 AM
I am receiving data from UART in uint8_t array.
I want to convert array to string to work on it:
if (Received Data=="Reset") { resetFunc(); }
if (Received Data.substring(0,8)=="ABCDASD")
{
Do something
}
if (Received Data.substring(4,8)=="ABC1234")
{
int Number=1234; // convert substring to int
char Data ="ABC"; // convert substring to char
}
- Labels:
-
UART-USART
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-11-11 5:06 AM
You cannot compare strings in C.
You can cast (uint8_t*) to (char*) ans use strcmp thereafter.
hth
KnarfB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-11-11 5:25 AM
if (strcmp((const char *) Received Data, "Reset") == 0) {
resetFunc();
}
if (strncmp((const char *) Received Data, "ABCDASD", 7) == 0) {
Do something
}
if (strncmp((const char *) &Received Data[4], "ABC1234", 4) == 0) {
...
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-11-11 6:27 AM
This is more a C language issue, and not STM32 specific
You can construct/re-construct a string a character at a time.
You might need to synchronize and discard characters if you receive from a serial port.
To use string functions you need to insure the data is NUL terminated.
Review C libraries and documentation. Review materials on parsing input.
For numeric conversion there are functions like atoi() and atod(), also sscanf()
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
‎2021-11-11 12:15 PM
Thanks for all.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-11-11 12:15 PM
It worked
Very thanks
