From b472366da739102e72b042ffb631d5c70acee41b Mon Sep 17 00:00:00 2001 From: larsbrubaker Date: Tue, 4 Feb 2014 10:51:06 -0800 Subject: [PATCH] The printer was having errors with resend interacting with code to try and correct transmission errors of "ok". Took out the code for attempted transmission correction. Put in the ability to force immediate writes so that we can be sure to turn off the heaters and motors when exiting the program. Fixed a bug in the injection of m105 during printing so that it does not go in more than once if present. --- PrinterCommunication.cs | 47 ++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/PrinterCommunication.cs b/PrinterCommunication.cs index a4bc792d4..f23325f90 100644 --- a/PrinterCommunication.cs +++ b/PrinterCommunication.cs @@ -139,6 +139,8 @@ namespace MatterHackers.MatterControl public enum CommunicationStates { Disconnected, AttemptingToConnect, FailedToConnect, Connected, PreparingToPrint, Printing, Paused, FinishedPrint, Disconnecting, ConnectionLost }; CommunicationStates communicationState = CommunicationStates.Disconnected; + bool ForceImmediateWrites = false; + public CommunicationStates CommunicationState { get @@ -1494,17 +1496,27 @@ namespace MatterHackers.MatterControl { // Get the temperature during print. if (printerCommandQueueIndex > 0 - && printerCommandQueueIndex < loadedGCode.GCodeCommandQueue.Count - && loadedGCode.GCodeCommandQueue[printerCommandQueueIndex].Line != lineToWrite) + && printerCommandQueueIndex < loadedGCode.GCodeCommandQueue.Count -1) { - loadedGCode.GCodeCommandQueue.Insert(printerCommandQueueIndex + 1, new PrinterMachineInstruction(lineToWrite, loadedGCode.GCodeCommandQueue[printerCommandQueueIndex])); + if (loadedGCode.GCodeCommandQueue[printerCommandQueueIndex+1].Line != lineToWrite) + { + loadedGCode.GCodeCommandQueue.Insert(printerCommandQueueIndex + 1, new PrinterMachineInstruction(lineToWrite, loadedGCode.GCodeCommandQueue[printerCommandQueueIndex])); + } } } else { - if (LinesToWriteQueue.Count == 0 || LinesToWriteQueue[LinesToWriteQueue.Count - 1] != lineToWrite) + // sometimes we need to send code without buffering (like when we are closing the program). + if (ForceImmediateWrites) { - LinesToWriteQueue.Add(lineToWrite); + WriteToPrinter(lineToWrite + "\r\n", lineToWrite); + } + else + { + if (LinesToWriteQueue.Count == 0 || LinesToWriteQueue[LinesToWriteQueue.Count - 1] != lineToWrite) + { + LinesToWriteQueue.Add(lineToWrite); + } } } } @@ -1574,10 +1586,16 @@ namespace MatterHackers.MatterControl { if (PrinterIsConnected) { + // Make sure we send this without waiting for the printer to respond. We want to try and turn off the heaters. + // It may be possible in the future to make this go into the printer queue for assured sending but it means + // the program has to be smart about closing an able to wait until the printer has agreed that it shut off + // the motors and heaters (a good idea ane something for the future). + ForceImmediateWrites = true; ReleaseMotors(); TargetExtruderTemperature = 0; TargetBedTemperature = 0; FanSpeed = 0; + ForceImmediateWrites = false; CommunicationState = CommunicationStates.Disconnecting; if (readFromPrinterThread != null) @@ -1608,10 +1626,13 @@ namespace MatterHackers.MatterControl timeSinceLastWrite.Restart(); if (PrinterIsConnected) { - bool forceResendInCaseOfOKError = false; + //bool forceResendInCaseOfOKError = false; // wait until the printer responds from the last command with an ok OR we waited to long - while (PrinterIsPrinting && timeHaveBeenWaitingForOK.IsRunning && !forceResendInCaseOfOKError) + while (PrinterIsPrinting && timeHaveBeenWaitingForOK.IsRunning)// && !forceResendInCaseOfOKError) { +#if false + // this is a bunch of code to try and make sure the printer does not stop on transmission errors. + // It is not working and is currently disabled. It would be great to debug this and get it working. Lars. // It has been more than 5 seconds since the printer responded anything // and it was not ok, and it's been more than 10 second since we sent the command if (timeSinceLastReadAnything.Elapsed.Seconds > 5 && timeSinceLastWrite.Elapsed.Seconds > 10) @@ -1621,10 +1642,9 @@ namespace MatterHackers.MatterControl { // the last instruction was a move PrinterMachineInstruction lastInstruction = loadedGCode.GCodeCommandQueue[printerCommandQueueIndex - 1]; - bool isMove = lastInstruction.Line.StartsWith("G1") || lastInstruction.Line.StartsWith("G0"); - if (isMove && firstLineToResendIndex == allCheckSumLinesSent.Count) + if (firstLineToResendIndex == allCheckSumLinesSent.Count) { - // Basically we got some response but it did not contain on OK. + // Basically we got some response but it did not contain an OK. // The theory is that we may have recieved a transmission error (like 'OP' rather than 'OK') // and in that event we don't want the print to just stop and wait forever. forceResendInCaseOfOKError = true; @@ -1633,6 +1653,13 @@ namespace MatterHackers.MatterControl } } + bool printerWantsResend = firstLineToResendIndex < allCheckSumLinesSent.Count; + if (printerWantsResend) + { + forceResendInCaseOfOKError = true; + } +#endif + // we are waiting for ok so wait some time System.Threading.Thread.Sleep(1); }