From 53ded760e9a683851491dcd651422bb1a53b3115 Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Wed, 25 Mar 2015 18:01:57 -0700 Subject: [PATCH] Put a try catch around the printer on idle call Refactor / renaming --- ActionBar/PrintActionRow.cs | 7 ++- CustomWidgets/ExportPrintItemWindow.cs | 6 +- MatterControlApplication.cs | 12 +++- PartPreviewWindow/ViewGcodeBasic.cs | 2 +- PartPreviewWindow/ViewGcodeWidget.cs | 4 +- PrinterCommunication/Io/PrinterIoGCodeFile.cs | 26 ++++---- .../PrinterConnectionAndCommunication.cs | 60 +++++++++---------- PrinterControls/PrintLevelingPlane.cs | 2 +- 8 files changed, 66 insertions(+), 53 deletions(-) diff --git a/ActionBar/PrintActionRow.cs b/ActionBar/PrintActionRow.cs index cfca8b321..5970ac977 100644 --- a/ActionBar/PrintActionRow.cs +++ b/ActionBar/PrintActionRow.cs @@ -244,8 +244,11 @@ namespace MatterHackers.MatterControl.ActionBar void onConfirmCancelPrint(bool messageBoxResponse) { if (messageBoxResponse) - { - CancelPrinting(); + { + UiThread.RunOnIdle((state) => + { + CancelPrinting(); + }); } } diff --git a/CustomWidgets/ExportPrintItemWindow.cs b/CustomWidgets/ExportPrintItemWindow.cs index 015d727ad..a5aa7dee2 100644 --- a/CustomWidgets/ExportPrintItemWindow.cs +++ b/CustomWidgets/ExportPrintItemWindow.cs @@ -289,9 +289,9 @@ namespace MatterHackers.MatterControl PrintLevelingData levelingData = PrintLevelingData.GetForPrinter(ActivePrinterProfile.Instance.ActivePrinter); if (levelingData != null) { - for (int i = 0; i < unleveledGCode.Count; i++) + for (int lineIndex = 0; lineIndex < unleveledGCode.LineCount; lineIndex++) { - PrinterMachineInstruction instruction = unleveledGCode.Instruction(i); + PrinterMachineInstruction instruction = unleveledGCode.Instruction(lineIndex); List linesToWrite = null; switch (levelingData.levelingSystem) @@ -312,7 +312,7 @@ namespace MatterHackers.MatterControl foreach(string line in linesToWrite) { PrinterMachineInstruction newInstruction = new PrinterMachineInstruction(line); - unleveledGCode.Insert(++i, newInstruction); + unleveledGCode.Insert(++lineIndex, newInstruction); } } } diff --git a/MatterControlApplication.cs b/MatterControlApplication.cs index a86d62c91..84c14696b 100644 --- a/MatterControlApplication.cs +++ b/MatterControlApplication.cs @@ -346,7 +346,17 @@ namespace MatterHackers.MatterControl void CheckOnPrinter(object state) { - PrinterConnectionAndCommunication.Instance.OnIdle(); + try + { + PrinterConnectionAndCommunication.Instance.OnIdle(); + } + catch(Exception e) + { + Debug.Print(e.Message); +#if DEBUG + throw e; +#endif + } UiThread.RunOnIdle(CheckOnPrinter); } diff --git a/PartPreviewWindow/ViewGcodeBasic.cs b/PartPreviewWindow/ViewGcodeBasic.cs index 865b0301e..bc477940f 100644 --- a/PartPreviewWindow/ViewGcodeBasic.cs +++ b/PartPreviewWindow/ViewGcodeBasic.cs @@ -875,7 +875,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow if (gcodeViewWidget != null && gcodeViewWidget.LoadedGCode != null - && gcodeViewWidget.LoadedGCode.Count > 0) + && gcodeViewWidget.LoadedGCode.LineCount > 0) { CreateOptionsContent(); buttonRightPanel.Visible = true; diff --git a/PartPreviewWindow/ViewGcodeWidget.cs b/PartPreviewWindow/ViewGcodeWidget.cs index cbdcad6ea..58c002299 100644 --- a/PartPreviewWindow/ViewGcodeWidget.cs +++ b/PartPreviewWindow/ViewGcodeWidget.cs @@ -244,13 +244,13 @@ namespace MatterHackers.MatterControl.PartPreviewWindow void SetInitalLayer() { activeLayerIndex = 0; - if (loadedGCode.Count > 0) + if (loadedGCode.LineCount > 0) { int firstExtrusionIndex = 0; Vector3 lastPosition = loadedGCode.Instruction(0).Position; double ePosition = loadedGCode.Instruction(0).EPosition; // let's find the first layer that has extrusion if possible and go to that - for (int i = 1; i < loadedGCode.Count; i++) + for (int i = 1; i < loadedGCode.LineCount; i++) { PrinterMachineInstruction currentInstruction = loadedGCode.Instruction(i); if (currentInstruction.EPosition > ePosition && lastPosition != currentInstruction.Position) diff --git a/PrinterCommunication/Io/PrinterIoGCodeFile.cs b/PrinterCommunication/Io/PrinterIoGCodeFile.cs index 4d54b84b7..2fc398a68 100644 --- a/PrinterCommunication/Io/PrinterIoGCodeFile.cs +++ b/PrinterCommunication/Io/PrinterIoGCodeFile.cs @@ -38,7 +38,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io public class PrinterIoGCodeFile : PrinterIoBase { GCodeFile loadedGCode; - int printerCommandQueueIndex; + int printerCommandQueueLineIndex; public PrinterIoGCodeFile(GCodeFile loadedGCode) { this.loadedGCode = loadedGCode; @@ -48,7 +48,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io { get { - if (loadedGCode.Count > 0) + if (loadedGCode.LineCount > 0) { return loadedGCode.Instruction(0).secondsToEndFromHere; } @@ -62,9 +62,9 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io { get { - int currentIndex = printerCommandQueueIndex - backupAmount; + int currentIndex = printerCommandQueueLineIndex - backupAmount; if (currentIndex >= 0 - && currentIndex < loadedGCode.Count) + && currentIndex < loadedGCode.LineCount) { for (int zIndex = 0; zIndex < loadedGCode.NumChangesInZ; zIndex++) { @@ -101,19 +101,19 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io { get { - int currentIndex = printerCommandQueueIndex - backupAmount; - if (currentIndex >= 0 - && currentIndex < loadedGCode.Count) + int currentLineIndex = printerCommandQueueLineIndex - backupAmount; + if (currentLineIndex >= 0 + && currentLineIndex < loadedGCode.LineCount) { int currentLayer = CurrentlyPrintingLayer; int startIndex = loadedGCode.GetInstructionIndexAtLayer(currentLayer); - int endIndex = loadedGCode.Count - 1; + int endIndex = loadedGCode.LineCount - 1; if (currentLayer < loadedGCode.NumChangesInZ - 2) { endIndex = loadedGCode.GetInstructionIndexAtLayer(currentLayer + 1) - 1; } - int deltaFromStart = Math.Max(0, currentIndex - startIndex); + int deltaFromStart = Math.Max(0, currentLineIndex - startIndex); return deltaFromStart / (double)(endIndex - startIndex); } @@ -127,11 +127,11 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io { if (NumberOfInstruction > 0) { - if (printerCommandQueueIndex >= 0 - && printerCommandQueueIndex < loadedGCode.Count - && loadedGCode.Instruction(printerCommandQueueIndex).secondsToEndFromHere != 0) + if (printerCommandQueueLineIndex >= 0 + && printerCommandQueueLineIndex < loadedGCode.LineCount + && loadedGCode.Instruction(printerCommandQueueLineIndex).secondsToEndFromHere != 0) { - return loadedGCode.Instruction(printerCommandQueueIndex).secondsToEndFromHere; + return loadedGCode.Instruction(printerCommandQueueLineIndex).secondsToEndFromHere; } } diff --git a/PrinterCommunication/PrinterConnectionAndCommunication.cs b/PrinterCommunication/PrinterConnectionAndCommunication.cs index 90a59eb78..a79af023b 100644 --- a/PrinterCommunication/PrinterConnectionAndCommunication.cs +++ b/PrinterCommunication/PrinterConnectionAndCommunication.cs @@ -357,7 +357,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication private PrintItemWrapper activePrintItem; int lastRemainingSecondsReported = 0; - int printerCommandQueueIndex = -1; + int printerCommandQueueLineIndex = -1; Vector3 currentDestination; double currentFeedRate; @@ -702,7 +702,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication { get { - return loadedGCode.Count; + return loadedGCode.LineCount; } } @@ -710,7 +710,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication { get { - if (loadedGCode.Count > 0) + if (loadedGCode.LineCount > 0) { if (FeedRateRatio != 0) { @@ -729,7 +729,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication { get { - int instructionIndex = printerCommandQueueIndex - backupAmount; + int instructionIndex = printerCommandQueueLineIndex - backupAmount; return loadedGCode.GetLayerIndex(instructionIndex); } } @@ -754,7 +754,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication { get { - int instructionIndex = printerCommandQueueIndex - backupAmount; + int instructionIndex = printerCommandQueueLineIndex - backupAmount; return loadedGCode.Ratio0to1IntoContainedLayer(instructionIndex); } } @@ -766,13 +766,13 @@ namespace MatterHackers.MatterControl.PrinterCommunication { if (NumberOfLinesInCurrentPrint > 0) { - if (printerCommandQueueIndex >= 0 - && printerCommandQueueIndex < loadedGCode.Count - && loadedGCode.Instruction(printerCommandQueueIndex).secondsToEndFromHere != 0) + if (printerCommandQueueLineIndex >= 0 + && printerCommandQueueLineIndex < loadedGCode.LineCount + && loadedGCode.Instruction(printerCommandQueueLineIndex).secondsToEndFromHere != 0) { if (FeedRateRatio != 0) { - lastRemainingSecondsReported = (int)(loadedGCode.Instruction(printerCommandQueueIndex).secondsToEndFromHere / FeedRateRatio); + lastRemainingSecondsReported = (int)(loadedGCode.Instruction(printerCommandQueueLineIndex).secondsToEndFromHere / FeedRateRatio); } } @@ -817,7 +817,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication else if (NumberOfLinesInCurrentPrint > 0 && loadedGCode != null) { - return loadedGCode.PercentComplete(printerCommandQueueIndex); + return loadedGCode.PercentComplete(printerCommandQueueLineIndex); } else { @@ -855,7 +855,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication } if (CommunicationState == CommunicationStates.PrintingFromSd - && (!timeWaitingForSdProgress.IsRunning || timeWaitingForSdProgress.Elapsed.TotalSeconds > 60)) + && (!timeWaitingForSdProgress.IsRunning || timeWaitingForSdProgress.Elapsed.TotalSeconds > 10)) { timeWaitingForSdProgress.Restart(); SendLineToPrinterNow("M27"); // : Report SD print status @@ -1889,12 +1889,12 @@ namespace MatterHackers.MatterControl.PrinterCommunication if (PrinterIsPrinting && CommunicationState != CommunicationStates.PrintingFromSd) { // insert the command into the printing queue at the head - if (printerCommandQueueIndex >= 0 - && printerCommandQueueIndex < loadedGCode.Count - 1) + if (printerCommandQueueLineIndex >= 0 + && printerCommandQueueLineIndex < loadedGCode.LineCount - 1) { - if (!loadedGCode.Instruction(printerCommandQueueIndex + 1).Line.Contains(lineToWrite)) + if (!loadedGCode.Instruction(printerCommandQueueLineIndex + 1).Line.Contains(lineToWrite)) { - loadedGCode.Insert(printerCommandQueueIndex + 1, new PrinterMachineInstruction(lineToWrite, loadedGCode.Instruction(printerCommandQueueIndex))); + loadedGCode.Insert(printerCommandQueueLineIndex + 1, new PrinterMachineInstruction(lineToWrite, loadedGCode.Instruction(printerCommandQueueLineIndex))); } } } @@ -2135,7 +2135,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication double MaxTimeToMoveForSentInstructions() { double maxTime = 0; - for(int i = Math.Max(0, printerCommandQueueIndex - backupAmount); i < printerCommandQueueIndex; i++) + for(int i = Math.Max(0, printerCommandQueueLineIndex - backupAmount); i < printerCommandQueueLineIndex; i++) { maxTime = Math.Max(maxTime, loadedGCode.Instruction(i).secondsThisLine); } @@ -2154,10 +2154,10 @@ namespace MatterHackers.MatterControl.PrinterCommunication using (TimedLock.Lock(this, "WriteNextLineFromGCodeFile1")) { // we are still sending commands - if (printerCommandQueueIndex > 0 && printerCommandQueueIndex < loadedGCode.Count - 1) + if (printerCommandQueueLineIndex > 0 && printerCommandQueueLineIndex < loadedGCode.LineCount - 1) { // the last instruction was a move - PrinterMachineInstruction lastInstruction = loadedGCode.Instruction(printerCommandQueueIndex - 1); + PrinterMachineInstruction lastInstruction = loadedGCode.Instruction(printerCommandQueueLineIndex - 1); double epectedSecondsToWait = Math.Max(5, MaxTimeToMoveForSentInstructions()); bool wasMoveAndNoOK = (lastInstruction.Line.Contains("G0 ") || lastInstruction.Line.Contains("G1 ")) && timeHaveBeenWaitingForOK.Elapsed.TotalSeconds > epectedSecondsToWait; @@ -2190,7 +2190,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication bool pauseRequested = false; using (TimedLock.Lock(this, "WriteNextLineFromGCodeFile2")) { - if (printerCommandQueueIndex < loadedGCode.Count) + if (printerCommandQueueLineIndex < loadedGCode.LineCount) { if (firstLineToResendIndex < allCheckSumLinesSent.Count) { @@ -2205,7 +2205,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication return; } - string lineToWrite = loadedGCode.Instruction(printerCommandQueueIndex).Line; + string lineToWrite = loadedGCode.Instruction(printerCommandQueueLineIndex).Line; string[] splitOnSemicolon = lineToWrite.Split(';'); string trimedLine = splitOnSemicolon[0].Trim().ToUpper(); @@ -2222,7 +2222,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication } else if (lineToWrite == "M226" || lineToWrite == "@pause") { - RequestPause(printerCommandQueueIndex + 1); + RequestPause(printerCommandQueueLineIndex + 1); } else { @@ -2231,7 +2231,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication firstLineToResendIndex++; } - printerCommandQueueIndex++; + printerCommandQueueLineIndex++; } } else if (printWasCanceled) @@ -2244,7 +2244,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication } else { - if (printerCommandQueueIndex == loadedGCode.Count) + if (printerCommandQueueLineIndex == loadedGCode.LineCount) { CommunicationState = CommunicationStates.FinishedPrint; @@ -2271,7 +2271,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication { if (injectionStartIndex == 0) { - injectionStartIndex = printerCommandQueueIndex; + injectionStartIndex = printerCommandQueueLineIndex; } if (PrinterIsPrinting) @@ -2323,7 +2323,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication } } - private int InjectGCode(string codeToInject, int indexToStartInjection) + private int InjectGCode(string codeToInject, int lineIndexToStartInjection) { codeToInject = GCodeProcessing.ReplaceMacroValues(codeToInject); @@ -2339,9 +2339,9 @@ namespace MatterHackers.MatterControl.PrinterCommunication { trimedLine = ReplacePrinterMacros(trimedLine); - if (loadedGCode.Count > indexToStartInjection) + if (loadedGCode.LineCount > lineIndexToStartInjection) { - loadedGCode.Insert(indexToStartInjection, new PrinterMachineInstruction(trimedLine, loadedGCode.Instruction(indexToStartInjection))); + loadedGCode.Insert(lineIndexToStartInjection, new PrinterMachineInstruction(trimedLine, loadedGCode.Instruction(lineIndexToStartInjection))); } else { @@ -2351,7 +2351,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication } } - return indexToStartInjection + linesAdded; + return lineIndexToStartInjection + linesAdded; } private string ReplacePrinterMacros(string trimedLine) @@ -2403,7 +2403,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication void ClearQueuedGCode() { loadedGCode.Clear(); - printerCommandQueueIndex = 0; + printerCommandQueueLineIndex = 0; lastRemainingSecondsReported = 0; allCheckSumLinesSent.Clear(); @@ -2439,7 +2439,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication if (cancelGCode.Trim() != "") { // add any gcode we want to print while canceling - InjectGCode(cancelGCode, printerCommandQueueIndex); + InjectGCode(cancelGCode, printerCommandQueueLineIndex); } // let the process know we canceled not ended normaly. printWasCanceled = true; diff --git a/PrinterControls/PrintLevelingPlane.cs b/PrinterControls/PrintLevelingPlane.cs index ec40d0f36..e2ba9034f 100644 --- a/PrinterControls/PrintLevelingPlane.cs +++ b/PrinterControls/PrintLevelingPlane.cs @@ -124,7 +124,7 @@ namespace MatterHackers.MatterControl public void ApplyLeveling(GCodeFile unleveledGCode) { - for(int i=0; i