waiting for "ok" from GRBL

I am trying to wait for "ok" to continue sending commands to GRBL controller, using this simple function wait_for_ok() as below:

void loop()
{

readingMode = PIND & 0xF0;
switch (readingMode)
{

case B11110000:
break;

//X moves
case B01110000: // X move
if (encoderPos != oldEncPos)
{
if (oldEncPos < encoderPos)
{
Serial.println("G91 G0 X0.01 F20");
oldEncPos = encoderPos;
}
else if (oldEncPos > encoderPos)
{
Serial.println("G91 G0 X-0.01 F20");
oldEncPos = encoderPos;
}
wait_for_ok();
}
break;

case B11110001 // etc.

}
}

The code above works OK when instead of wait_for_ok()) function I use delay(5), but sometimes GRBL controller buffer is overloaded (because responses are ignored this way) so I really need to wait for "ok" acknowledgement before sending the next command.

I found the above function on the forum, but it doesn't work at all.
What am I doing wrong?

Thanks in advance for any help!

Sorry, forgot to paste the function ....

void wait_for_ok()
{
char resp[3];
while (strcmp(resp, "ok"))
{
; // waiting here for "ok" on serial
}
}

I found it here but not sure what this code really does:
https://forum.arduino.cc/index.php?topic=294155.0

    ; // waiting here for "ok" on serial

Where is the code that reads the Serial input ?

yes.. that is missing :confused:

I am not sure how to write this...

I tried to find some simple way to read "ok" from serial, but I couldn't, is there a simple recipe?

Bojan:
I tried to find some simple way to read "ok" from serial, but I couldn't, is there a simple recipe?

Reply #1 would seem to be a good place to start

You recognise a short sequence like this with a finite state machine.

I was thinking overnight about it, and I realised that detecting the single character (even CR or LF, whatever is sent by GRBL) should be enough for my purpose...

So, am I right assuming the following code should be sufficient - "while" loop should terminate the moment anything is sent from GRBL:

void wait_for_ok() {

char resp;

while (Serial.available() == 0)
{
while (Serial.available() >0)
{
resp = Serial.read();
; // waiting here for "ok" on serial
}
}

}

Will the above code clear the serial buffer for the next command?

How to clear serial buffer for the next command?

While there is serial data available read it until there is no more

Rather than just guessing what the code should look like, you should REALLY look at the documentation for how Serial.available() and Serial.read() work, and how to use them properly. Look at a few simple examples (there are THOUSANDS of them available), and learn how to use Serial properly. No part of your code is even close to correct, and you apparently have made no effort to learn the correct way to do it.

Regards,
Ray L.

@RayLivingston

You are right, my code is not OK and that is exactly the reason why I am asking for help here, in hope I will get the specific answer..

I am NOT a programmer like you guys and I do not have time to learn how to program properly - I only want to make this gadget to work somehow.
And I am sure I am not the only one...

UKHeliBob:
While there is serial data available read it until there is no more

Thank you for the reply :slight_smile:

I think I managed to do it (I edited the code a bit with another loop:

void wait_for_ok() {

char resp;

while (Serial.available() == 0)
{
;// waiting here for response on serial
}

while (Serial.available() > 0)
{
resp = Serial.read();
; // clearing buffer
}
}