Cura plugin to restart print on USB comm error

Hi folks,

There’s a really, really simple way to implement “disaster recovery” if we log the last line of gcode sent to a printer over USB, dupe the gcode associated with the print and strip any code above the ‘last line sent’… it might not be perfect, but it would probably work 99% of the
time. Even if it doesn’t make it into the main build, I’d like to add this functionality to a fork of Cura. How would I go about doing this?

I snaked through the Github and eventually found where the serialized gcode commands are being sent.

It seems like we could, at the start of the print, serialize/write all of the gcode to a file. While iterating through

        try:
            command = (cmd + "\n").encode()
            self._serial.write(b"\n")
            self._serial.write(command)
        except serial.SerialTimeoutException:
            Logger.log("w","Serial timeout while writing to serial port, trying again.")
            try:
                time.sleep(0.5)
                self._serial.write((cmd + "\n").encode())
            except Exception as e:
                Logger.log("e","Unexpected error while writing serial port %s " % e)
                self._setErrorState("Unexpected error while writing serial port %s " % e)
                self.close()
        except Exception as e:
            Logger.log("e","Unexpected error while writing serial port %s" % e)
            self._setErrorState("Unexpected error while writing serial port %s " % e)
            self.close()

I propose we add a “finally” to cache the last sent command… either just in a variable or written to a file.

        finally:
            self._last_command = command



How would I add a button to Cura that, assuming a print is in a paused/canceled/non-printing state, would read from this last command, manipulate the cached gcode file and remove everything up to and including this _last_command, then send the modified gcode to the printer?

Hi, maintainer of Cura LulzBot Edition here. I like the idea behind this and want to hear back if you get this working. Hope to give you a few pointers in the right direction.

So, we host our code on our own instance of Phabricator located here (code.alephobjects.com) and have a few repositories mirrored onto github. After cloning the repository onto your computer, follow the readme to setup Cura to be able to run directly from the source code https://code.alephobjects.com/diffusion/CURA/browse/devel/README.md. Feel free to ask questions here if you run into any problems doing this part.

After you have Cura setup, you should be able to just run the dev-cura file to test. As far as the actual software modifications you would need to make start looking at printWindow.py (https://code.alephobjects.com/diffusion/CURA/browse/devel/Cura/gui/printWindow.py) this file contains most of the logic and user interface for managing a USB print.