I am not actually using ad arduino but an atmel chip. but my question is c microprocessor related
I am reading an uart which returns data in the form uint8_t [100] I can print the return just fine, the return is "OK"
but when I try to use strcmp(read, "OK") I do not get the expected value, I get a mismatch ( strcmp returns 13, not 0)
uint8_t sigfox_rd_buf[100] = {0};
uint8_t sig_rd = 0;
io_sigfox->read(io_sigfox,&sig_rd, 1);
while(sig_rd != 10) // read until end char
{
sigfox_rd_buf[i] = sig_rd;
i++;
io_sigfox->read(io_sigfox,&sig_rd, 1);
}
if(strcmp((char *)sigfox_rd_buf,"OK") == 0 )
{
do_something()
}
else
{
complain();
}
and I always end up in complain for some reason.. please help!! :)
1 Answer 1
When using strcmp(), strings should be terminated with a NULL character. Which, by the way, is a zero. Your code should work as is if you replace the Line Feed character (10 or 0x0a) with a NULL (0 or 0x00). That is assuming the string you are getting back is 0x4f, 0x4b & 0x0a. Or you could play it safe and replace strcmp with strncmp. Then explicitly state the number of characters to compare:
if(strncmp((char *)sigfox_rd_buf,"OK",2) == 0 )
-
In general, yes, but we don't actually know that the data providing function will return nulls at this point, or if how much data we over or under read will leave the next run in a bad starting condition. Sigfox perhaps implies packets more than streams, but the interface being used looks at least momentarily stream-like.Chris Stratton– Chris Stratton2016年06月26日 17:07:57 +00:00Commented Jun 26, 2016 at 17:07
-
Ok, I change the wording so as to explicitly state strcmp() string arguments should be terminated by NULL.st2000– st20002016年06月26日 17:51:42 +00:00Commented Jun 26, 2016 at 17:51
13
not give it away?