From c1a66d192edcd969273027294a08897270ec47e2 Mon Sep 17 00:00:00 2001 From: Edward Wright Date: Tue, 4 Jun 2019 21:42:43 -0400 Subject: [PATCH] ensure serial port is always closed before exiting --- SimplySerial/SimplySerial.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/SimplySerial/SimplySerial.cs b/SimplySerial/SimplySerial.cs index edad3b2..0b79f03 100644 --- a/SimplySerial/SimplySerial.cs +++ b/SimplySerial/SimplySerial.cs @@ -47,7 +47,7 @@ namespace SimplySerial } catch (Exception e) { - ExitProgram((e.GetType() + " occurred while attempting to open the serial port."), exitCode: -1); + ExitProgram((e.GetType() + " occurred while attempting to open the serial port " + port + "."), exitCode: -1); } // set up keyboard input for relay to serial port @@ -118,10 +118,6 @@ namespace SimplySerial } } - // the serial port should be closed by now, but we'll make sure it is anyway - if (serialPort.IsOpen) - serialPort.Close(); - // the program should have ended gracefully before now - there is no good reason for us to be here! ExitProgram("\nSession terminated unexpectedly.", exitCode: -1); } @@ -244,6 +240,11 @@ namespace SimplySerial // get a list of all available com ports string[] availablePorts = SerialPort.GetPortNames(); + foreach (string potentialPort in availablePorts) + { + Console.WriteLine("PORT: {0}", potentialPort); + } + // if no port was specified, default to the first/only available com port, exit with error if no ports are available if (port == String.Empty) { @@ -303,6 +304,10 @@ namespace SimplySerial /// Exits without displaying a message or asking for a key press when set to 'true' static void ExitProgram(string message="", int exitCode=0, bool silent=false) { + // the serial port should be closed before exiting + if (serialPort.IsOpen) + serialPort.Close(); + if (!silent) Output("\n" + message);