From b4e830fd49caa4f6505d57d7a70604f0535410a8 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Fri, 1 Dec 2023 22:04:17 +0530
Subject: [PATCH] serial_server: handle the case where read returns a 0 (eof)

---
 rigctl.c      | 16 ++++++++++------
 rigctl_menu.c |  2 +-
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/rigctl.c b/rigctl.c
index cd46a51..3203d81 100644
--- a/rigctl.c
+++ b/rigctl.c
@@ -4370,15 +4370,14 @@ static gpointer serial_server(gpointer data) {
                 command_index++;
                 if (cmd_input[i] == ';') {
                     command[command_index] = '\0';
-                    if (rigctl_debug)
+                    if (rigctl_debug) {
                         g_print("RIGCTL: command=%s\n", command);
+		    }
                     COMMAND *info = g_new(COMMAND, 1);
                     info->client = client;
                     info->command = command;
                     g_mutex_lock(&mutex_busy->m);
-		    if (client->fd != -1) {
-			g_idle_add(parse_cmd, info);
-		    }
+		    g_idle_add(parse_cmd, info);
                     g_mutex_unlock(&mutex_busy->m);
 
                     command = g_new(char, MAXDATASIZE);
@@ -4386,9 +4385,14 @@ static gpointer serial_server(gpointer data) {
                 }
             }
         } else if (numbytes < 0) {
+	    perror("serial_server");
             break;
-        }
-        // usleep(100L);
+        } else if (numbytes == 0) {
+	    // don't continue in the loop, the other side has closed
+	    // the connection, so no point trying to read from it.
+	    perror("serial_server: connection closed by the client");
+	    break;
+	}
     }
     close(client->fd);
     cat_control--;
diff --git a/rigctl_menu.c b/rigctl_menu.c
index 2ee0750..d036fb4 100644
--- a/rigctl_menu.c
+++ b/rigctl_menu.c
@@ -36,7 +36,7 @@ gboolean  serial_enable = TRUE; // edit (enable serial at startup)
 char ser_port[64]="/dev/ttyACM0";
 int serial_baud_rate = B9600;
 int serial_parity = 0; // 0=none, 1=even, 2=odd
-gboolean rigctl_debug=FALSE;
+gboolean rigctl_debug = TRUE;
 
 static GtkWidget *parent_window=NULL;
 static GtkWidget *dialog=NULL;
-- 
2.45.2