From 71195961701578d91bb348d71f5adcaa028ae9e9 Mon Sep 17 00:00:00 2001
From: Mark Scammacca <github@cnrdesigns.com>
Date: Mon, 1 Jun 2020 22:19:10 -0500
Subject: [PATCH] G-code line number for each serial port (for TFTs) (#18165)

---
 Marlin/src/gcode/host/M110.cpp |  5 ++++-
 Marlin/src/gcode/queue.cpp     | 17 +++++++++--------
 Marlin/src/gcode/queue.h       | 13 +++++++++++--
 3 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/Marlin/src/gcode/host/M110.cpp b/Marlin/src/gcode/host/M110.cpp
index f7b6dfd4f4..126063116a 100644
--- a/Marlin/src/gcode/host/M110.cpp
+++ b/Marlin/src/gcode/host/M110.cpp
@@ -27,5 +27,8 @@
  * M110: Set Current Line Number
  */
 void GcodeSuite::M110() {
-  if (parser.seenval('N')) queue.last_N = parser.value_long();
+
+  if (parser.seenval('N'))
+    queue.last_N[queue.command_port()] = parser.value_long();
+
 }
diff --git a/Marlin/src/gcode/queue.cpp b/Marlin/src/gcode/queue.cpp
index b98ef10992..a4a4993556 100644
--- a/Marlin/src/gcode/queue.cpp
+++ b/Marlin/src/gcode/queue.cpp
@@ -52,7 +52,7 @@ GCodeQueue queue;
  * sending commands to Marlin, and lines will be checked for sequentiality.
  * M110 N<int> sets the current line number.
  */
-long gcode_N, GCodeQueue::last_N;
+long GCodeQueue::last_N[NUM_SERIAL];
 
 /**
  * GCode Command Queue
@@ -277,7 +277,7 @@ void GCodeQueue::enqueue_now_P(PGM_P const pgcode) {
  */
 void GCodeQueue::ok_to_send() {
   #if NUM_SERIAL > 1
-    const int16_t pn = port[index_r];
+    const int16_t pn = command_port();
     if (pn < 0) return;
     PORT_REDIRECT(pn);                    // Reply to the serial port that sent the command
   #endif
@@ -302,14 +302,15 @@ void GCodeQueue::ok_to_send() {
  * indicate that a command needs to be re-sent.
  */
 void GCodeQueue::flush_and_request_resend() {
+  const int16_t pn = command_port();
   #if NUM_SERIAL > 1
-    const int16_t pn = port[index_r];
     if (pn < 0) return;
     PORT_REDIRECT(pn);                    // Reply to the serial port that sent the command
   #endif
   SERIAL_FLUSH();
   SERIAL_ECHOPGM(STR_RESEND);
-  SERIAL_ECHOLN(last_N + 1);
+  
+  SERIAL_ECHOLN(last_N[pn] + 1);  
   ok_to_send();
 }
 
@@ -336,7 +337,7 @@ void GCodeQueue::gcode_line_error(PGM_P const err, const int8_t pn) {
   PORT_REDIRECT(pn);                      // Reply to the serial port that sent the command
   SERIAL_ERROR_START();
   serialprintPGM(err);
-  SERIAL_ECHOLN(last_N);
+  SERIAL_ECHOLN(last_N[pn]);
   while (read_serial(pn) != -1);          // Clear out the RX buffer
   flush_and_request_resend();
   serial_count[pn] = 0;
@@ -475,9 +476,9 @@ void GCodeQueue::get_serial_commands() {
             if (n2pos) npos = n2pos;
           }
 
-          gcode_N = strtol(npos + 1, nullptr, 10);
+          const long gcode_N = strtol(npos + 1, nullptr, 10);
 
-          if (gcode_N != last_N + 1 && !M110)
+          if (gcode_N != last_N[i] + 1 && !M110)
             return gcode_line_error(PSTR(STR_ERR_LINE_NO), i);
 
           char *apos = strrchr(command, '*');
@@ -490,7 +491,7 @@ void GCodeQueue::get_serial_commands() {
           else
             return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), i);
 
-          last_N = gcode_N;
+          last_N[i] = gcode_N;
         }
         #if ENABLED(SDSUPPORT)
           // Pronterface "M29" and "M29 " has no line number
diff --git a/Marlin/src/gcode/queue.h b/Marlin/src/gcode/queue.h
index 053d143e76..6c14d7d3db 100644
--- a/Marlin/src/gcode/queue.h
+++ b/Marlin/src/gcode/queue.h
@@ -35,7 +35,8 @@ public:
    * commands to Marlin, and lines will be checked for sequentiality.
    * M110 N<int> sets the current line number.
    */
-  static long last_N;
+
+  static long last_N[NUM_SERIAL];
 
   /**
    * GCode Command Queue
@@ -51,13 +52,21 @@ public:
 
   static char command_buffer[BUFSIZE][MAX_CMD_SIZE];
 
-  /*
+  /**
    * The port that the command was received on
    */
   #if NUM_SERIAL > 1
     static int16_t port[BUFSIZE];
   #endif
 
+  static int16_t command_port() {
+    return (0
+      #if NUM_SERIAL > 1
+        + port[index_r]
+      #endif
+    );
+  }
+
   GCodeQueue();
 
   /**
-- 
GitLab