2

I have an input which comes over UART.

uint8_t uartRX_data[UART_RX_BUF_SIZE]="";

I need to pass this data to a function. And, in this function I want to compare it with predefined strings like:

char RESP_OK[]                  = "OK";
char RESP_ERROR[]               = "ERROR";
char RESP_FAIL[]                = "FAIL";

What is the easiest way to do that?

EDIT: My problem is only about the data comparison and data passing to a function.

Caglayan DOKME
  • 948
  • 8
  • 21
  • 2
    Is the data in `uartRX_data` terminated with a NUL (`'\0'`) character or does a separate variable tell the number of valid bytes in the buffer? If the buffer is not terminated with a NUL (`'\0'`) character, does it have space to add one? Please [edit] your question to add information. – Bodo May 03 '19 at 10:02
  • 'uartRX_data' is not the original data of buffer. I'm parsing the original data under an event handler to create 'uartRX_data' . The data is not terminated with a NUL. It only includes characters. – Caglayan DOKME May 03 '19 at 10:05
  • 1
    So a non separated array of chars? Will this array include additional substrings than the 3 you've listed above? – dtell May 03 '19 at 10:08
  • 2
    Please [edit] your question to add this information. How do you know how many characters are valid in `uartRX_data`? Is there a variable that contains the number of characters? Does the buffer have space to add a NUL? – Bodo May 03 '19 at 10:09
  • I understood your question. You can assume that the array is terminated with a NUL character. It's my fault, sorry. – Caglayan DOKME May 03 '19 at 10:15
  • 1
    Then just `strcmp((const char *)uartRX_data, RESP_OK)` and so on should work. – simon May 03 '19 at 10:17
  • @simon It worked. Thanks. – Caglayan DOKME May 03 '19 at 10:26

2 Answers2

4

As long as the string in uartRX_data is NULL terminated you should be able to use strcmp like so:

if (strcmp((const char *)uartRX_data, RESP_OK) == 0)
{
  // handle OK
}
else if (strcmp((const char *)uartRX_data, RESP_ERROR) == 0)
{
  // handle ERROR
}
else if (strcmp((const char *)uartRX_data, RESP_FAIL) == 0)
{
  // handle FAIL
}
else
{
  // handle unknown response
}
simon
  • 2,042
  • 2
  • 20
  • 31
1

Most conversions between the types char, signed char, unsigned char, int8_t and uint8_t can be regarded as safe. These are the character types and come with various special exceptions that make them more "rugged" than other types.

Specifically, the character types:

  • Cannot be misaligned.
  • Cannot contain padding bits or trap representations.
  • Have exceptions from the effective type/strict aliasing rules.
  • Are always of size 1 byte.

Meaning you can do all manner of wild conversions between different character types. With a few exceptions:

  • When going from unsigned to signed types the data may not necessarily fit and you'll overwrite the sign bit. This will potentially cause bugs.
  • It is dangerous to "cast away" const or volatile qualifiers, if present.

Therefore it is safe to convert from uint8_t* to char* and de-reference the data as another type, (char*)uartRX_data. Especially if you know that the uint8_t array contains valid 7 bit characters only with MSB never set, and with a null termination in the end of the array.

Lundin
  • 195,001
  • 40
  • 254
  • 396