From 81bb4d9052c559c20592d1e7fcc14da50203c917 Mon Sep 17 00:00:00 2001 From: ellensp <530024+ellensp@users.noreply.github.com> Date: Thu, 5 Nov 2026 02:15:36 +1300 Subject: [PATCH] fix: Don't store M110 in files uploaded to SD Uploads run through startprint, which sends M110 N-1 at the start and end of a print. With M28 open the firmware writes those lines into the file, so uploaded files gained lines that weren't in the original. The start of an upload still needs the line number reset, so send it before M28 instead. Add an upload option to startprint that leaves out both, and use it from Pronterface and pronsole. --- printrun/printcore.py | 16 ++++++++++++---- printrun/pronsole.py | 3 ++- printrun/pronterface.py | 3 ++- tests/test_printcore.py | 28 ++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/printrun/printcore.py b/printrun/printcore.py index 4819738d1..3f3970ff7 100644 --- a/printrun/printcore.py +++ b/printrun/printcore.py @@ -118,6 +118,7 @@ def __init__(self, port = None, baud = None, dtr=None): self.online = False # is a print currently running, true if printing, false if paused self.printing = False + self.sd_upload = False # the current print is an upload to SD self.mainqueue = None self.priqueue = Queue(0) self.queueindex = 0 @@ -370,7 +371,7 @@ def _sender(self): def _checksum(self, command): return reduce(lambda x, y: x ^ y, map(ord, command)) - def startprint(self, gcode, startindex = 0): + def startprint(self, gcode, startindex = 0, upload = False): """Start a print. The `mainqueue` is populated and then commands are gradually sent to @@ -387,6 +388,10 @@ def startprint(self, gcode, startindex = 0): startindex : int, default: 0 The index from the `gcode` array from which the printing will be started. + upload : bool, default: False + True if the firmware is storing the lines in a file instead of + running them. No line number reset is sent, as it would be stored + too, so the caller has to send one before opening the file. Returns ------- @@ -405,8 +410,10 @@ def startprint(self, gcode, startindex = 0): if not gcode or not gcode.lines: return True - self.clear = False - self._send("M110 N-1", -1, True) + self.sd_upload = upload + if not upload: + self.clear = False + self._send("M110 N-1", -1, True) resuming = (startindex != 0) self.print_thread = threading.Thread(target = self._print, @@ -633,7 +640,8 @@ def _sendnext(self): if not self.paused: self.queueindex = 0 self.lineno = 0 - self._send("M110 N-1", -1, True) + if not self.sd_upload: + self._send("M110 N-1", -1, True) def _send(self, command, lineno = 0, calcchecksum = False): # Only add checksums if over serial (tcp does the flow control itself) diff --git a/printrun/pronsole.py b/printrun/pronsole.py index c954e4d3c..d59241b15 100644 --- a/printrun/pronsole.py +++ b/printrun/pronsole.py @@ -1027,9 +1027,10 @@ def do_upload(self, l): self._do_load(filename) self.log(_("Uploading as %s") % targetname) self.log(_("Uploading %s") % self.filename) + self.p.send_now("M110 N-1") self.p.send_now("M28 " + targetname) self.log(_("Press Ctrl-C to interrupt upload.")) - self.p.startprint(self.fgcode) + self.p.startprint(self.fgcode, upload = True) try: sys.stdout.write(_("Progress: ") + "00.0%") sys.stdout.flush() diff --git a/printrun/pronterface.py b/printrun/pronterface.py index 8c7bd09f6..347b0be64 100644 --- a/printrun/pronterface.py +++ b/printrun/pronterface.py @@ -1464,6 +1464,7 @@ def upload(self, event): dlg = wx.TextEntryDialog(self, _("Enter a target filename in 8.3 format:"), _("Pick SD filename"), dosify(self.filename)) if dlg.ShowModal() == wx.ID_OK: self.p.send_now("M21") + self.p.send_now("M110 N-1") self.p.send_now("M28 " + str(dlg.GetValue())) self.recvlisteners.append(self.uploadtrigger) dlg.Destroy() @@ -1471,7 +1472,7 @@ def upload(self, event): def uploadtrigger(self, l): if "Writing to file" in l: self.uploading = True - self.p.startprint(self.fgcode) + self.p.startprint(self.fgcode, upload = True) self.p.callback.end = self.endupload self.recvlisteners.remove(self.uploadtrigger) elif "open failed, File" in l: diff --git a/tests/test_printcore.py b/tests/test_printcore.py index 3b764de93..6605142ab 100644 --- a/tests/test_printcore.py +++ b/tests/test_printcore.py @@ -392,6 +392,34 @@ def setUp(self): self.start_cb = mock_callback(self, self.core, "startcb") self.end_cb = mock_callback(self, self.core, "endcb") + def run_to_end(self, **kwargs): + """Run the whole print and return the bytes written during it""" + self.mocked_serial.return_value.write.reset_mock() + self.core.startprint(self.print_code, **kwargs) + limit = time.monotonic() + 5 + while self.core.printing and time.monotonic() < limit: + time.sleep(0.01) + self.assertFalse(self.core.printing) + return b"".join(c.args[0] for c + in self.mocked_serial.return_value.write.call_args_list) + + def test_print_resets_line_number(self): + """Test a print resets the line number at its start and end""" + written = self.run_to_end() + self.assertEqual(written.count(b"M110 N-1"), 2) + self.assertTrue(written.startswith(checksum_command("M110 N-1", -1))) + + def test_upload_sends_only_its_lines(self): + """Test an upload doesn't add lines to the file being written""" + written = self.run_to_end(upload=True) + self.assertNotIn(b"M110", written) + self.assertEqual(written, b"".join(self.parsed_print_code)) + + def test_print_after_upload_resets_line_number(self): + """Test a normal print still resets the line number after an upload""" + self.run_to_end(upload=True) + self.assertEqual(self.run_to_end().count(b"M110 N-1"), 2) + def check_unfinished_print(self): """Check the first command was sent but the last wasn't""" first_call = mock.call(self.parsed_print_code[0])