diff --git a/PrinterCommunication/PrinterConnectionAndCommunication.cs b/PrinterCommunication/PrinterConnectionAndCommunication.cs index 5c64ff3b3..e6e4f8e69 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); } } @@ -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 { @@ -1387,11 +1383,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 +1414,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication } catch (System.ArgumentOutOfRangeException) { - connectionFailureMessage = "Unsupported Baud Rate"; + connectionFailureMessage = LocalizedString.Get("Unsupported Baud Rate"); OnConnectionFailed(null); } @@ -1428,6 +1424,16 @@ 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")); + + OnConnectionFailed(null); + } } public void OnPrintFinished(EventArgs e) 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. +