Strchr causes arduino to crash

Here is my script

unsigned long startedWaiting = millis();   
char buffer[90];
int size = ultrasonic.readBytesUntil('\n', buffer, 80);

while (strchr(buffer, 'IdleSec') == NULL && millis() - startedWaiting <= 10000) {
  trow_away=(ultrasonic.read());
  size = ultrasonic.readBytesUntil('\n', buffer, 80);
  if (sleepT==1) { ultrasonic.write(">PwrIdleCfg:1,1\r\n"); }
  else if (sleepT==2) { ultrasonic.write(">PwrIdleCfg:1,2\r\n"); }
    delay(300);
  }

Im communicating with the serial device which is periodically going to sleep and cant receive commands while sleeping. So to change sleep time i wrote a function using strchrthat checks when sleep time was changed and to keep sending the change command if it has not yet happened. This function works but from time to time it crash my arduino that then restarts.
Any idea why ?

Oops

while (strchr(buffer, 'IdleSec') == NULL && millis() - startedWaiting <= 10000)

doesn't strchr() require a string as a parameter ?

"IdleSec"

if i change 'IdleSec' to "IdleSec" i get compile error

invalid conversion from 'const char*' to 'int' [-fpermissive]

Nope
The second argument to strchr is an int

The clue is that 'IdleSec' isn't a character.

Im not sure i fully understand what you mean.
So i can with strchr i can only search for one character not string right ?

How would i search for the whole "IdleSec" string ?

Could i do
strchr(buffer, 'S') == NULL && strchr(buffer, 'I') == NULL....

or there is something easyer ?

Here are two options, there are many more:

Do you know if the .readBytesUntil() funtions adds a zero-terminator when it returned with a timout ? No ? Then why do you use that function :stuck_out_tongue_closed_eyes:

I've never used readBytesUntil (and I'm away from my computer) - does it terminate the buffer?

Edit: Back at my computer now. No, it doesn't. See reply #3

Let’s read the manual…
strstr() might be a better fit, but ‘xxxx’ won’t cut the mustard.

There’s a difference between a character, and a character array.

If you’re looking for an exact match, rather than a contained ‘string’, you could read the reference for strcmp()

Im not searching for exact match but rather if string contains a string like if there is "IdleSec" in "somethin something Idlesec 1 something".

Im looking into strstr()... What do you mean by 'xxx' wont cut the mustard ?

Because 'xxx' is a multi-character constant, the same size as an int, and not a string.

It’s a learning curve…
We’re not trying to be unhelpful, but a fork and a knife are both cutlery, yet do quite different things.

Sometimes you need to understand what you’re looking for - before pitching a solution.

The idea of ‘contains’ a string is quite different to ‘is the same as’…
read about both strchr, strcmp, and strstr… the function references explain the differences.

HINT:
‘x’ is a character constant
“xxx” is a string literal (constant). Typically stored in an array.

You are of course correct. I should have read the post more carefully... I was seeing strchr, and reading strstr

Thank you all for your explanations! I think i manage to solve it with strstr().
I believe there were some memory issues as well, that should also be fixed with last update. Fingers crossed.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.