From 4fbfcd1666a9b198d5017e08e09192f124c95e65 Mon Sep 17 00:00:00 2001 From: jlewin Date: Sun, 19 Oct 2014 10:13:23 -0700 Subject: [PATCH 1/4] Report connection failure reason - Fixes #80972004 --- PrinterCommunication/PrinterConnectionAndCommunication.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/PrinterCommunication/PrinterConnectionAndCommunication.cs b/PrinterCommunication/PrinterConnectionAndCommunication.cs index 5c64ff3b3..17d16c6c8 100644 --- a/PrinterCommunication/PrinterConnectionAndCommunication.cs +++ b/PrinterCommunication/PrinterConnectionAndCommunication.cs @@ -1428,6 +1428,11 @@ namespace MatterHackers.MatterControl.PrinterCommunication } } } + else + { + connectionFailureMessage = (serialPortIsAlreadyOpen ? "Port already in use" : "Port not found"); + OnConnectionFailed(null); + } } public void OnPrintFinished(EventArgs e) From b7e25cc57441f7c4bac87f689d7a560456bb7080 Mon Sep 17 00:00:00 2001 From: jlewin Date: Sun, 19 Oct 2014 10:26:09 -0700 Subject: [PATCH 2/4] Localize connection related error messages --- .../PrinterConnectionAndCommunication.cs | 15 +++++++++------ StaticData/Translations/Master.txt | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/PrinterCommunication/PrinterConnectionAndCommunication.cs b/PrinterCommunication/PrinterConnectionAndCommunication.cs index 17d16c6c8..4996d5a06 100644 --- a/PrinterCommunication/PrinterConnectionAndCommunication.cs +++ b/PrinterCommunication/PrinterConnectionAndCommunication.cs @@ -1203,7 +1203,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication { connectThread.Join(JoinThreadTimeoutMs); //Halt connection thread Disable(); - connectionFailureMessage = "Cancelled"; + connectionFailureMessage = LocalizedString.Get("Cancelled"); OnConnectionFailed(null); return false; } @@ -1296,7 +1296,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication else { Debug.WriteLine("Connection failed: {0}".FormatWith(this.ActivePrinter.ComPort)); - connectionFailureMessage = "Unavailable"; + connectionFailureMessage = LocalizedString.Get("Unavailable"); OnConnectionFailed(null); } } @@ -1387,11 +1387,11 @@ namespace MatterHackers.MatterControl.PrinterCommunication void AttemptToConnect(string serialPortName, int baudRate) { - connectionFailureMessage = "Unknown Reason"; + connectionFailureMessage = LocalizedString.Get("Unknown Reason"); if (PrinterIsConnected) { - throw new Exception("You can only connect when not currently connected."); + throw new Exception(LocalizedString.Get("You can only connect when not currently connected.")); } CommunicationState = CommunicationStates.AttemptingToConnect; @@ -1418,7 +1418,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication } catch (System.ArgumentOutOfRangeException) { - connectionFailureMessage = "Unsupported Baud Rate"; + connectionFailureMessage = LocalizedString.Get("Unsupported Baud Rate"); OnConnectionFailed(null); } @@ -1430,7 +1430,10 @@ namespace MatterHackers.MatterControl.PrinterCommunication } else { - connectionFailureMessage = (serialPortIsAlreadyOpen ? "Port already in use" : "Port not found"); + connectionFailureMessage = (serialPortIsAlreadyOpen ? + LocalizedString.Get("Port already in use") : + LocalizedString.Get("Port not found")); + OnConnectionFailed(null); } } diff --git a/StaticData/Translations/Master.txt b/StaticData/Translations/Master.txt index e346dcbe9..aae9a3fb3 100644 --- a/StaticData/Translations/Master.txt +++ b/StaticData/Translations/Master.txt @@ -2762,3 +2762,21 @@ Translated:Material 4 English:Material 5 Translated:Material 5 +English:Unknown Reason +Translated:Unknown Reason + +English:Port already in use +Translated:Port already in use + +English:Unsupported Baud Rate +Translated:Unsupported Baud Rate + +English:Port not found +Translated:Port not found + +English:Cancelled +Translated:Cancelled + +English:You can only connect when not currently connected. +Translated:You can only connect when not currently connected. + From 0690ce4bfdaf5dba6b91a7ca63a60fbeb500231d Mon Sep 17 00:00:00 2001 From: jlewin Date: Sun, 19 Oct 2014 10:34:09 -0700 Subject: [PATCH 3/4] Ensure close is called in all scenarios and switch to using{} for Dispose --- .../PrinterConnectionAndCommunication.cs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/PrinterCommunication/PrinterConnectionAndCommunication.cs b/PrinterCommunication/PrinterConnectionAndCommunication.cs index 4996d5a06..4130f67c6 100644 --- a/PrinterCommunication/PrinterConnectionAndCommunication.cs +++ b/PrinterCommunication/PrinterConnectionAndCommunication.cs @@ -1354,15 +1354,11 @@ namespace MatterHackers.MatterControl.PrinterCommunication const int GENERIC_WRITE = 0x40000000; //Borrowed from Microsoft's Serial Port Open Method :) - SafeFileHandle hFile = CreateFile(@"\\.\" + portName, GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, 3, dwFlagsAndAttributes, IntPtr.Zero); - if (hFile.IsInvalid) + using (SafeFileHandle hFile = CreateFile(@"\\.\" + portName, GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, 3, dwFlagsAndAttributes, IntPtr.Zero)) { - return true; + hFile.Close(); + return hFile.IsInvalid; } - - hFile.Close(); - - return false; } else { From ea5466ba7855a23306b28adcdedaa3a5d31fb944 Mon Sep 17 00:00:00 2001 From: jlewin Date: Tue, 21 Oct 2014 14:56:19 -0700 Subject: [PATCH 4/4] Improve comments --- PrinterCommunication/PrinterConnectionAndCommunication.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/PrinterCommunication/PrinterConnectionAndCommunication.cs b/PrinterCommunication/PrinterConnectionAndCommunication.cs index 4130f67c6..e6e4f8e69 100644 --- a/PrinterCommunication/PrinterConnectionAndCommunication.cs +++ b/PrinterCommunication/PrinterConnectionAndCommunication.cs @@ -1426,6 +1426,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication } else { + // If the serial port isn't avaiable (i.e. the specified port name wasn't found in GetPortNames()) or the serial + // port is already opened in another instance or process, then report the connection problem back to the user connectionFailureMessage = (serialPortIsAlreadyOpen ? LocalizedString.Get("Port already in use") : LocalizedString.Get("Port not found"));