diff --git a/Marlin/src/gcode/queue.cpp b/Marlin/src/gcode/queue.cpp index 12c3703630..c3c155e4c3 100644 --- a/Marlin/src/gcode/queue.cpp +++ b/Marlin/src/gcode/queue.cpp @@ -526,42 +526,38 @@ void GCodeQueue::get_serial_commands() { while (length < BUFSIZE && !card_eof) { const int16_t n = card.get(); card_eof = card.eof(); + if (n < 0) { SERIAL_ERROR_MSG(MSG_SD_ERR_READ); continue; } const char sd_char = (char)n; - if (card_eof || n < 0 || sd_char == '\n' || sd_char == '\r') { + if (sd_char == '\n' || sd_char == '\r' || card_eof) { + + // Reset stream state, terminate the buffer, and commit a non-empty command + if (!process_line_done(sd_input_state, command_buffer[index_w], sd_count)) { + _commit_command(false); // The file was not terminated with a newline + #if ENABLED(POWER_LOSS_RECOVERY) + recovery.cmd_sdpos = card.getIndex(); // Prime for the next _commit_command + #endif + } + if (card_eof) { - card.printingHasFinished(); + card.fileHasFinished(); // Handle end of file reached - if (IS_SD_PRINTING()) - sd_count = 0; // If a sub-file was printing, continue from call point - else { - SERIAL_ECHOLNPGM(MSG_FILE_PRINTED); + if (!IS_SD_PRINTING()) { // Was it the main job file? + SERIAL_ECHOLNPGM(MSG_FILE_PRINTED); // Tell the host the file is printed. #if ENABLED(PRINTER_EVENT_LEDS) - printerEventLEDs.onPrintCompleted(); + printerEventLEDs.onPrintCompleted(); // Change LED color for Print Completed #if HAS_RESUME_CONTINUE - enqueue_now_P(PSTR("M0 S" + enqueue_now_P(PSTR("M0 S" // Display "Click to Continue..." #if HAS_LCD_MENU - "1800" + "1800" // ...for 30 minutes with LCD #else - "60" + "60" // ...for 1 minute with no LCD #endif )); #endif #endif } } - else if (n < 0) - SERIAL_ERROR_MSG(MSG_SD_ERR_READ); - - // Terminate the buffer, reset the input state, continue for empty line - if (process_line_done(sd_input_state, command_buffer[index_w], sd_count)) - continue; - - _commit_command(false); - - #if ENABLED(POWER_LOSS_RECOVERY) - recovery.cmd_sdpos = card.getIndex(); // Prime for the next _commit_command - #endif } else process_stream_char(sd_char, sd_input_state, command_buffer[index_w], sd_count); diff --git a/Marlin/src/sd/cardreader.cpp b/Marlin/src/sd/cardreader.cpp index 0b3a91f40e..3cc888fd96 100644 --- a/Marlin/src/sd/cardreader.cpp +++ b/Marlin/src/sd/cardreader.cpp @@ -1063,7 +1063,7 @@ uint16_t CardReader::get_num_Files() { // // Return from procedure or close out the Print Job // -void CardReader::printingHasFinished() { +void CardReader::fileHasFinished() { planner.synchronize(); file.close(); if (file_subcall_ctr > 0) { // Resume calling file after closing procedure diff --git a/Marlin/src/sd/cardreader.h b/Marlin/src/sd/cardreader.h index 54d541c38a..8e09f44479 100644 --- a/Marlin/src/sd/cardreader.h +++ b/Marlin/src/sd/cardreader.h @@ -106,7 +106,7 @@ public: // Print job static void openAndPrintFile(const char *name); // (working directory) - static void printingHasFinished(); + static void fileHasFinished(); static void getAbsFilename(char *dst); static void startFileprint(); static void printFilename();