Here is the part of my code which is handling the communication with GRBL. Waits for ok to send new line and stops when reports on error.
if (grbl_reply.indexOf("error:") >= 0) {
while (Serial1.available()) {
grbl_error += Serial1.read();
}
Serial.println(grbl_error);
grblReportError();
return -1;
}
}
}
grblReportError();
return -1;
}