Skip to content

Commit

Permalink
basic checksum working
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanix committed Aug 23, 2012
1 parent e5d34a4 commit c7b9b57
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 27 deletions.
67 changes: 48 additions & 19 deletions gcode.c
Expand Up @@ -50,6 +50,7 @@

#define BUFFER_LINE_SIZE 80
char rx_line[BUFFER_LINE_SIZE];
char *rx_line_cursor;

#define FAIL(status) gc.status_code = status;

Expand Down Expand Up @@ -97,7 +98,7 @@ void gcode_init() {


void gcode_process_line() {
char chr = '\0';
uint8_t chr = '\0';
int numChars = 0;
uint8_t iscomment = false;
int status_code = STATUS_OK;
Expand Down Expand Up @@ -134,9 +135,9 @@ void gcode_process_line() {
iscomment = true;
} else if (chr >= 'a' && chr <= 'z') {
// upcase any lower case chars
rx_line[numChars++] = chr-'a'+'A';
rx_line[numChars++] = (char)chr-'a'+'A';
} else {
rx_line[numChars++] = chr;
rx_line[numChars++] = (char)chr;
}
}
}
Expand All @@ -160,28 +161,56 @@ void gcode_process_line() {
printString("B"); // Stop: Rx Buffer Overflow
} else if (status_code == STATUS_LINE_BUFFER_OVERFLOW) {
printString("I"); // Stop: Line Buffer Overflow
} else if (status_code == STATUS_TRANSMISSION_ERROR) {
printString("T"); // Stop: Serial Transmission Error
} else {
printString("O"); // Stop: Other error
printInteger(status_code);
}
} else if (rx_line[0] != '?') {
// process the next line of G-code
} else {
printString(rx_line); // DEBUG
status_code = gcode_execute_line(rx_line);
line_processed = true;
// report parse errors
if (status_code == STATUS_OK) {
// pass
} else if (status_code == STATUS_BAD_NUMBER_FORMAT) {
printString("N"); // Warning: Bad number format
} else if (status_code == STATUS_EXPECTED_COMMAND_LETTER) {
printString("E"); // Warning: Expected command letter
} else if (status_code == STATUS_UNSUPPORTED_STATEMENT) {
printString("U"); // Warning: Unsupported statement
if (rx_line[0] == '*' || rx_line[0] == '^') {
rx_line_cursor = rx_line+2; // set line offset
uint8_t rx_checksum = (uint8_t)rx_line[1];
char *itr = rx_line_cursor;
uint16_t checksum = 0;
while (*itr) { // all chars without 0-termination
checksum += (uint8_t)*itr++;
if (checksum >= 128) {
checksum -= 128;
}
}
checksum = (checksum >> 1) + 128; // /2, +128
printString("(");
printInteger(rx_checksum);
printString(",");
printInteger(checksum);
printString(")");
if (checksum != rx_checksum) {
stepper_request_stop(STATUS_TRANSMISSION_ERROR);
}
} else {
printString("W"); // Warning: Other error
printInteger(status_code);
}
rx_line_cursor = rx_line;
}

if (rx_line_cursor[0] != '?') {
// process the next line of G-code
status_code = gcode_execute_line(rx_line_cursor);
line_processed = true;
// report parse errors
if (status_code == STATUS_OK) {
// pass
} else if (status_code == STATUS_BAD_NUMBER_FORMAT) {
printString("N"); // Warning: Bad number format
} else if (status_code == STATUS_EXPECTED_COMMAND_LETTER) {
printString("E"); // Warning: Expected command letter
} else if (status_code == STATUS_UNSUPPORTED_STATEMENT) {
printString("U"); // Warning: Unsupported statement
} else {
printString("W"); // Warning: Other error
printInteger(status_code);
}
}
}

#ifndef DEBUG_IGNORE_SENSORS
Expand Down
17 changes: 9 additions & 8 deletions gcode.h
Expand Up @@ -28,14 +28,15 @@
#define STATUS_OK 0
#define STATUS_RX_BUFFER_OVERFLOW 1
#define STATUS_LINE_BUFFER_OVERFLOW 2
#define STATUS_BAD_NUMBER_FORMAT 3
#define STATUS_EXPECTED_COMMAND_LETTER 4
#define STATUS_UNSUPPORTED_STATEMENT 5
#define STATUS_SERIAL_STOP_REQUEST 6
#define STATUS_LIMIT_HIT 7
#define STATUS_POWER_OFF 8
// #define STATUS_DOOR_OPEN 9
// #define STATUS_CHILLER_OFF 10
#define STATUS_TRANSMISSION_ERROR 3
#define STATUS_BAD_NUMBER_FORMAT 4
#define STATUS_EXPECTED_COMMAND_LETTER 5
#define STATUS_UNSUPPORTED_STATEMENT 6
#define STATUS_SERIAL_STOP_REQUEST 7
#define STATUS_LIMIT_HIT 8
#define STATUS_POWER_OFF 9
// #define STATUS_DOOR_OPEN 10
// #define STATUS_CHILLER_OFF 11


// Initialize the parser
Expand Down

0 comments on commit c7b9b57

Please sign in to comment.