Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grbl checksum #865

Open
wants to merge 3 commits into
base: Devt
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Grbl_Esp32/src/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ enum class Error : uint8_t {
GcodeG43DynamicAxisError = 37,
GcodeMaxValueExceeded = 38,
PParamMaxExceeded = 39,
GcodeChecksumFailed = 40,
FsFailedMount = 60, // SD Failed to mount
FsFailedRead = 61, // SD Failed to read file
FsFailedOpenDir = 62, // SD card failed to open directory
Expand Down
1 change: 1 addition & 0 deletions Grbl_Esp32/src/Exec.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ enum class ExecAlarm : uint8_t {
HomingFailPulloff = 8,
HomingFailApproach = 9,
SpindleControl = 10,
ChecksumFailed = 11,
};

extern std::map<ExecAlarm, const char*> AlarmNames;
46 changes: 42 additions & 4 deletions Grbl_Esp32/src/GCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,47 @@ void gc_sync_position() {

// Edit GCode line in-place, removing whitespace and comments and
// converting to uppercase
void collapseGCode(char* line) {
Error collapseGCode(char* line) {
// parenPtr, if non-NULL, is the address of the character after (
char* parenPtr = NULL;

// outPtr is the address where newly-processed characters will be placed.
// outPtr is alway less than or equal to inPtr.
char* outPtr = line;
char c;
for (char* inPtr = line; (c = *inPtr) != '\0'; inPtr++) {

// Skip insignificant newlines at the start
char* inPtr = line;
for (; *inPtr != '\0' && (*inPtr == '\n' || *inPtr == '\r'); ++inPtr) {}

int checksum = 0;
for (; (c = *inPtr) != '\0'; inPtr++) {
if (c == '*') {
if ((checksum & 0xFF00) == 0) {
*outPtr = '\0'; // Terminate gcode statement; the parser doesn't understand '*'.

auto checksumFound = atoi(inPtr + 1);

if ((checksum & 0xFF) != (checksumFound & 0xFF)) {
grbl_msg_sendf(
CLIENT_ALL, MsgLevel::Debug, "Checksum mismatched. Expected %d, but checksum is %d", checksumFound, checksum);
sys.state = State::Alarm; // Ensure alarm state is active.
report_alarm_message(ExecAlarm::ChecksumFailed);
return Error::GcodeChecksumFailed;
} else {
return Error::Ok;
}
}
}

if ((checksum & 0xFF00) == 0) {
checksum = checksum ^ uint8_t(c);
}

if (isspace(c)) {
continue;
}

switch (c) {
case ')':
if (parenPtr) {
Expand All @@ -82,14 +112,16 @@ void collapseGCode(char* line) {
case '(':
// Start the comment at the character after (
parenPtr = inPtr + 1;
checksum |= 0x200;
break;
case ';':
// NOTE: ';' comment to EOL is a LinuxCNC definition. Not NIST.
#ifdef REPORT_SEMICOLON_COMMENTS
report_gcode_comment(inPtr + 1);
#endif
*outPtr = '\0';
return;
checksum |= 0x200;
return Error::Ok;
case '%':
// TODO: Install '%' feature
// Program start-end percent sign NOT SUPPORTED.
Expand All @@ -105,6 +137,7 @@ void collapseGCode(char* line) {
if (!parenPtr) {
*outPtr++ = toupper(c); // make upper case
}
break;
}
}
// On loop exit, *inPtr is '\0'
Expand All @@ -113,6 +146,8 @@ void collapseGCode(char* line) {
report_gcode_comment(parenPtr);
}
*outPtr = '\0';

return Error::Ok;
}

// Executes one line of NUL-terminated G-Code.
Expand All @@ -123,7 +158,10 @@ void collapseGCode(char* line) {
// coordinates, respectively.
Error gc_execute_line(char* line, uint8_t client) {
// Step 0 - remove whitespace and comments and convert to upper case
collapseGCode(line);
auto error = collapseGCode(line);
if (error != Error::Ok) {
return error;
}
#ifdef REPORT_ECHO_LINE_RECEIVED
report_echo_line_received(line, client);
#endif
Expand Down
Loading