/******************************************************** * processOkStreaming() * process ok message for streaming * called in RX event chain, from processGrblMessages() ********************************************************/ public void processOkStreaming() { if (isStreaming) // || isStreamingRequestStopp) { // confirm sent data if (!isStreamingPause) { streamingBuffer.LineWasReceived(); // streamingBuffer.DeleteLine(); // better don't removeAt(0)... allowStreamingEvent = true; } else { streamingStateNow = grblStreaming.pause; // update status } // send status via event to main GUI if ((streamingStateOld != streamingStateNow) || allowStreamingEvent) { if (streamingStateNow != grblStreaming.pause) { if (trgEvent) { sendStreamEvent(streamingStateNow); } trgEvent = false; } // streaming processOkStreaming streamingStateOld = streamingStateNow; //grblStatus = oldStatus; allowStreamingEvent = false; } } }
public void pauseStreaming() { if (!isStreamingPause) { isStreamingRequestPause = true; // wait until buffer is empty before switch to pause addToLog("[Pause streaming]"); addToLog("[Save Settings]"); grblStatus = grblStreaming.waitidle; getParserState = true; } else { addToLog("[Restore Settings]"); var cmds = parserState.Split(' '); foreach (var cmd in cmds) { requestSend(cmd); } // restore actual GCode settings one by one addToLog("[Start streaming - no echo]"); isStreamingPause = false; isStreamingRequestPause = false; grblStatus = grblStreaming.ok; proceedStreaming(); } }
public StreamEventArgs(int c1, float a1, float a2, grblStreaming stat) { codeLine = c1; codeFinish = a1; buffFinish = a2; status = stat; }
public StreamEventArgs(int c1, int c2, float a1, float a2, grblStreaming stat) { codeLineSent = c1; codeLineConfirmed = c2; codeFinish = a1; buffFinish = a2; status = stat; }
/*************************************************************** * streamingIDLE() * called in RX event chain, from processGrblStateChange() ***************************************************************/ private void streamingIDLE() { // in main GUI: send extra Pause-Code in MainTimer_Tick from Properties.Settings.Default.flowControlText // OnRaiseStreamEvent - case grblStreaming.pause: if (isStreamingPauseFirst && Properties.Settings.Default.flowControlEnable) delayedSend = 2; if (countPreventIdle <= 1) { waitForIdle = false; // addToLog("---------- IDLE state reached ---------"); if (logStartStop) { Logger.Trace(" grblStateChanged() ---------- IDLE state reached --------- {0}", listInfoStream()); } if ((isStreamingRequestPause || isStreamingRequestStopp) && !isStreamingPause) { isStreamingPause = true; isStreamingRequestPause = false; streamingStateNow = grblStreaming.pause; posPause = posWork; addToLog(string.Format("[Save Settings X:{0} Y:{1}]", posPause.X, posPause.Y)); if (logStartStop) { Logger.Trace("updateStreaming IDLE reached"); } gcodeVariable["MLAX"] = posMachine.X; gcodeVariable["MLAY"] = posMachine.Y; gcodeVariable["MLAZ"] = posMachine.Z; gcodeVariable["WLAX"] = posWork.X; gcodeVariable["WLAY"] = posWork.Y; gcodeVariable["WLAZ"] = posWork.Z; if (getParserState) { requestSend("$G"); } if (isStreamingRequestStopp) // 20200717 { if (logStartStop) { Logger.Trace(" grblStateChanged() - now really stop - resetStreaming"); } stopStreamingFinal(); isStreaming = false; } updateControls(); } if (streamingStateOld != streamingStateNow) { sendStreamEvent(streamingStateNow); // streaming in streamingIDLE() } streamingStateOld = streamingStateNow; if (streamingBuffer.IndexConfirmed >= streamingBuffer.Count) { streamingFinish(); } } }
private void sendStreamEvent(int lineNr, grblStreaming status) { float codeFinish = (float)gCodeLinesConfirmed * 100 / (float)gCodeLinesCount; float buffFinish = (float)(grblBufferSize - grblBufferFree) * 100 / (float)grblBufferSize; if (codeFinish > 100) { codeFinish = 100; } if (buffFinish > 100) { buffFinish = 100; } OnRaiseStreamEvent(new StreamEventArgs((int)lineNr, codeFinish, buffFinish, status)); }
private void grblStateChanged() { if ((sendLinesConfirmed == sendLinesCount) && (grblStateNow == grblState.idle)) // addToLog(">> Buffer empty\r"); { if (isStreamingRequestPause) { isStreamingPause = true; isStreamingRequestPause = false; grblStatus = grblStreaming.pause; if (getParserState) { requestSend("$G"); } } } }
/***** sendStreamEvent update main prog *****/ private void sendStreamEvent(grblStreaming status, int linePause = -1) { int lineNrSent = streamingBuffer.GetSentLineNr(); if (linePause > 0) { lineNrSent = linePause; } int lineNrConfirmed = streamingBuffer.GetConfirmedLineNr(); if (status == grblStreaming.error) { lineNrConfirmed = sendBuffer.GetConfirmedLineNr() + 1; } // progressbar.value is int int codeFinish = 0; if (streamingBuffer.Count != 0) { codeFinish = (int)Math.Ceiling((float)lineNrConfirmed * 100 / streamingBuffer.MaxLineNr) + 1; // to reach 100% } int buffFinish = 0; if (grblBufferSize != 0) { buffFinish = (int)Math.Ceiling((float)(grblBufferSize - grblBufferFree) * 100 / grblBufferSize) + 1; // to reach 100% } if (codeFinish > 100) { codeFinish = 100; } if (buffFinish > 100) { buffFinish = 100; } // Logger.Trace("OnRaiseStreamEvent {0} {1} {2} {3} {4}", lineNrSent, lineNrConfirmed, codeFinish, buffFinish, status); OnRaiseStreamEvent(new StreamEventArgs(lineNrSent, lineNrConfirmed, codeFinish, buffFinish, status)); }
/***** sendStreamEvent update main prog *****/ private void sendStreamEvent(grblStreaming status, int linePause = -1) { int lineNrSent = streamingBuffer.GetSentLineNr(); if (linePause > 0) { lineNrSent = linePause; } int lineNrConfirmed = streamingBuffer.GetConfirmedLineNr(); if (status == grblStreaming.error) { lineNrConfirmed = sendBuffer.GetConfirmedLineNr() + 1; } float codeFinish = 0; if (streamingBuffer.Count != 0) { codeFinish = (float)streamingBuffer.IndexConfirmed * 100 / (float)streamingBuffer.Count; } float buffFinish = 0; if (grblBufferSize != 0) { buffFinish = (float)(grblBufferSize - grblBufferFree) * 100 / (float)grblBufferSize; } if (codeFinish > 100) { codeFinish = 100; } if (buffFinish > 100) { buffFinish = 100; } OnRaiseStreamEvent(new StreamEventArgs(lineNrSent, lineNrConfirmed, codeFinish, buffFinish, status)); }
/***** last command was sent/received - end streaming *****/ private void streamingFinish() { countPreventEvent = 0; countPreventOutput = 0; addToLog("\r[Streaming finish]"); Logger.Info("streamingFinish ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲"); streamingStateNow = grblStreaming.finish; if (isStreamingCheck) { requestSend("$C"); isStreamingCheck = false; } OnRaiseStreamEvent(new StreamEventArgs(0, 0, 0, 0, grblStreaming.finish)); if (Properties.Settings.Default.grblPollIntervalReduce) { timerSerial.Interval = grbl.pollInterval; countMissingStatusReport = (int)(10000 / timerSerial.Interval); } sendBuffer.Clear(); streamingBuffer.Clear(); resetStreaming(); updateControls(); }
private void proceedStreaming() { while ((gCodeLinesSent < gCodeLinesCount) && (grblBufferFree >= gCodeLines[gCodeLinesSent].Length + 1)) { string line = gCodeLines[gCodeLinesSent]; int cmdMNr = gcode.getIntGCode('M', line); if ((cmdMNr == 0) && !isStreamingCheck) { isStreamingRequestPause = true; addToLog("[Pause streaming]"); addToLog("[Save Settings]"); grblStatus = grblStreaming.waitidle; getParserState = true; sendStreamEvent(gCodeLineNr[gCodeLinesSent], grblStatus); return; // abort while - don't fill up buffer } else { requestSend(line); } gCodeLinesSent++; } }
public void startStreaming(IList <string> gCodeList, bool check = false) { rtbLog.Clear(); addToLog("[Start streaming - no echo]"); isStreaming = true; isStreamingPause = false; isStreamingCheck = check; grblStatus = grblStreaming.ok; if (isStreamingCheck) { requestSend("$C"); } string[] gCode = gCodeList.ToArray <string>(); gCodeLinesSent = 0; gCodeLinesCount = 0; gCodeLinesConfirmed = 0; grblBufferFree = grblBufferSize; gCodeLines = new List <string>(); gCodeLineNr = new List <int>(); sendLinesSent = 0; sendLinesCount = 0; sendLinesConfirmed = 0; sendLines.Clear(); string tmp; for (int i = 0; i < gCode.Length; i++) { tmp = cleanUpCodeLine(gCode[i]); if ((!string.IsNullOrEmpty(tmp)) && (tmp[0] != ';')) //trim lines and remove all empty lines and comment lines { gCodeLines.Add(tmp); // add gcode line to list to send gCodeLineNr.Add(i); // add line nr gCodeLinesCount++; // Count total lines } } proceedStreaming(); }
/**************************************************************************** * startStreaming called by main-Prog * get complete GCode list and copy to streamingBuffer * initialize streaming * if startAtLine > 0 start with pause ****************************************************************************/ public void startStreaming(IList <string> gCodeList, int startAtLine, bool check = false) { grblCharacterCounting = Properties.Settings.Default.grblStreamingProtocol1 && !isMarlin; Logger.Info("Ser:{0} startStreaming at line:{1} ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼", iamSerial, startAtLine); // if (logEnable) LogPos.Info("(----- Start streaming {0} -----)", getTimeStampString()); if (grblCharacterCounting) { Logger.Info("Streaming Protocol: Character-Counting"); } else { Logger.Info("Streaming Protocol: Simple Send-Response"); } updateLogging(); grblBufferSize = grbl.RX_BUFFER_SIZE; //rx bufer size of grbl on arduino 127 grblBufferFree = grbl.RX_BUFFER_SIZE; Logger.Info("Set Buffer Free:{0} Size:{1}", grblBufferFree, grblBufferSize); if (Properties.Settings.Default.grblPollIntervalReduce) { timerSerial.Interval = grbl.pollInterval * 2; } else { timerSerial.Interval = grbl.pollInterval; } countMissingStatusReport = (int)(10000 / timerSerial.Interval); Logger.Info("Timer interval:{0} {1}", timerSerial.Interval, countMissingStatusReport); skipM30 = false; lastError = ""; countGrblError = 0; lastSentToCOM.Clear(); toolTable.init(); // fill structure rtbLog.Clear(); // check if other serial are still alive if (useSerial2) { try { if (!_serial_form2.serialPortOpen) { addToLog("[2nd serial port is not open]"); useSerial2 = false; } } catch { useSerial2 = false; } } if (useSerial3) { try { if (!_serial_form3.serialPortOpen) { addToLog("[3rd serial port is not open]"); useSerial3 = false; } } catch { useSerial3 = false; } } if (!check) { addToLog("[Start streaming - no echo]"); if (useSerial2) { addToLog("[Use serial 2]"); } if (useSerial3) { addToLog("[Use serial 3]"); } } else { addToLog("[Start code check]"); } saveLastPos(); if (replaceFeedRate) { addToLog("!!! Override Feed Rate"); } if (replaceSpindleSpeed) { addToLog("!!! Override Spindle Speed"); } isStreamingPause = false; isStreamingRequestPause = false; isStreamingCheck = check; streamingStateNow = grblStreaming.ok; lineStreamingPause = -1; // save line were M0 appeared for main GUI to show notification streamingBuffer.Clear(); // = new List<string>(); resetStreaming(); // startStreaming if (isStreamingCheck) { sendLine("$C"); // startStreaming check grblBufferSize = 100; // reduce size to avoid fake errors } countLoggerUpdate = (int)(10000 / timerSerial.Interval); timerSerial.Stop(); /***** collect subroutines, without resolving variables *****/ #region subroutines subroutines = new Dictionary <int, List <string> >(); string tmp; string[] gCode = gCodeList.ToArray <string>(); bool prg_end = false; for (int i = startAtLine; i < gCode.Length; i++) { if (gCode[i].Contains("M30")) { prg_end = true; continue; } if (!prg_end) { continue; } if (gCode[i].Contains("O")) { int cmdONr = gcode.getIntGCode('O', gCode[i]); if (cmdONr <= 0) { continue; } subroutines.Add(cmdONr, new List <string>()); Logger.Trace("Add subroutine O{0}", cmdONr); for (int k = i + 1; k < gCode.Length; k++) { if (gCode[k].IndexOf("M99") >= 0) { break; } if (gCode[k].IndexOf("M98") >= 0) { double pWord = findDouble("P", -1, gCode[k]); double lWord = findDouble("L", 1, gCode[k]); if (subroutines.ContainsKey((int)pWord)) { for (int repeat = 0; repeat < lWord; repeat++) { foreach (string subroutineLine in subroutines[(int)pWord]) // copy subroutine { subroutines[cmdONr].Add(subroutineLine); Logger.Trace(" sub in sub {0}", subroutineLine); } } } else { Logger.Error("Start stresaming Subroutine {0} not found", pWord); } } else { subroutines[cmdONr].Add(gCode[k]); Logger.Trace(" {0}", gCode[k]); } } } } /****************************************************************************/ #endregion lock (sendDataLock) { //string[] gCode = gCodeList.ToArray<string>(); //string tmp; double pWord, lWord;//, oWord; // string subline; bool tmpToolInSpindle = toolInSpindle; int cmdTNr = -1; bool foundM30 = false; for (int i = startAtLine; i < gCode.Length; i++) { tmp = cleanUpCodeLine(gCode[i]); if ((!string.IsNullOrEmpty(tmp)) && (tmp[0] != ';'))//trim lines and remove all empty lines and comment lines { int cmdMNr = gcode.getIntGCode('M', tmp); int cmdGNr = gcode.getIntGCode('G', tmp); cmdTNr = gcode.getIntGCode('T', tmp); /***** Subroutine? ********************************************************/ #region subroutine // if (tmp.IndexOf("M98") >= 0) // any subroutines? if (cmdMNr == 98) { pWord = findDouble("P", -1, tmp); lWord = findDouble("L", 1, tmp); if (subroutines.ContainsKey((int)pWord)) { for (int repeat = 0; repeat < lWord; repeat++) { foreach (string subroutineLine in subroutines[(int)pWord]) // copy subroutine { if (subroutineLine.Contains('#')) // check if variable neededs to be replaced { streamingBuffer.Add(insertVariable(subroutineLine), i); } else { streamingBuffer.Add(subroutineLine, i); // add gcode line to list to send } } } } else { Logger.Error("Subroutine {0} not found", pWord); } } #endregion /***** Subroutine ********************************************************/ else { if (grbl.unknownG.Contains(cmdGNr)) { streamingBuffer.SetSentLine("(" + tmp + " - unknown)"); // don't pass unkown GCode to GRBL because is unknown tmp = streamingBuffer.GetSentLine(); streamingBuffer.LineWasReceived(); addToLog(tmp); } if (cmdTNr >= 0) // T-word is allowed by grbl - no need to filter { setToolChangeCoordinates(cmdTNr, tmp); // update variables e.g. "gcodeVariable[TOAX]" from tool-table } if (cmdMNr == 6) // M06 is not allowed - remove { if (Properties.Settings.Default.ctrlToolChange) { insertToolChangeCode(i, ref tmpToolInSpindle); // insert external script-code and insert variables tmp = "(" + tmp + ")"; } } if (cmdMNr == 30) { if (skipM30) { tmp = "(" + tmp + ")"; } } streamingBuffer.Add(tmp, i); // add gcode line to list to send if (cmdMNr == 30) { foundM30 = true; break; } } } } if (!foundM30) { if (!skipM30) { streamingBuffer.Add("M30", gCode.Length - 1); // add end } } streamingBuffer.Add("($END)", gCode.Length - 1); // add gcode line to list to send streamingBuffer.Add("()", gCode.Length - 1); // add gcode line to list to send } // lock timerSerial.Start(); if (logEnable) { string startText = string.Format("( {0} )\r\n", getTimeStampString()); File.WriteAllText(Application.StartupPath + "\\logSendBuffer.nc", startText); // clear file File.WriteAllText(Application.StartupPath + "\\logStreamingBuffer.nc", startText); // clear file File.AppendAllLines(Application.StartupPath + "\\logStreamingBuffer.nc", streamingBuffer.Buffer); } isStreaming = true; updateControls(); if (startAtLine > 0) { pauseStreaming(); isStreamingPause = true; } else { waitForIdle = false; preProcessStreaming(); // 411 } } // startStreaming
/**************************************************************************** * startStreaming called by main-Prog * get complete GCode list and copy to streamingBuffer * initialize streaming * if startAtLine > 0 start with pause ****************************************************************************/ public void startStreaming(IList <string> gCodeList, int startAtLine, bool check = false) { grblCharacterCounting = Properties.Settings.Default.grblStreamingProtocol1 && !isMarlin; Logger.Info("Ser:{0} startStreaming at line:{1} ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼", iamSerial, startAtLine); if (grblCharacterCounting) { Logger.Info("Streaming Protocol: Character-Counting"); } else { Logger.Info("Streaming Protocol: Simple Send-Response"); } updateLogging(); grblBufferSize = grbl.RX_BUFFER_SIZE; //rx bufer size of grbl on arduino 127 grblBufferFree = grbl.RX_BUFFER_SIZE; Logger.Info("Set Buffer Free:{0} Size:{1}", grblBufferFree, grblBufferSize); if (Properties.Settings.Default.grblPollIntervalReduce) { timerSerial.Interval = grbl.pollInterval * 2; } else { timerSerial.Interval = grbl.pollInterval; } countMissingStatusReport = (int)(10000 / timerSerial.Interval); Logger.Info("Timer interval:{0} {1}", timerSerial.Interval, countMissingStatusReport); skipM30 = false; lastError = ""; countGrblError = 0; lastSentToCOM.Clear(); toolTable.init(); // fill structure rtbLog.Clear(); // check if other serial are still alive if (useSerial2) { try { if (!_serial_form2.serialPortOpen) { addToLog("[2nd serial port is not open]"); useSerial2 = false; } } catch { useSerial2 = false; } } if (useSerial3) { try { if (!_serial_form3.serialPortOpen) { addToLog("[3rd serial port is not open]"); useSerial3 = false; } } catch { useSerial3 = false; } } if (!check) { addToLog("[Start streaming - no echo]"); if (useSerial2) { addToLog("[Use serial 2]"); } if (useSerial3) { addToLog("[Use serial 3]"); } } else { addToLog("[Start code check]"); } saveLastPos(); if (replaceFeedRate) { addToLog("!!! Override Feed Rate"); } if (replaceSpindleSpeed) { addToLog("!!! Override Spindle Speed"); } isStreamingPause = false; isStreamingRequestPause = false; isStreamingCheck = check; streamingStateNow = grblStreaming.ok; lineStreamingPause = -1; // save line were M0 appeared for main GUI to show notification streamingBuffer.Clear(); // = new List<string>(); resetStreaming(); // startStreaming if (isStreamingCheck) { sendLine("$C"); // startStreaming check grblBufferSize = 100; // reduce size to avoid fake errors } countLoggerUpdate = (int)(10000 / timerSerial.Interval); timerSerial.Stop(); lock (sendDataLock) { string[] gCode = gCodeList.ToArray <string>(); string tmp; double pWord, lWord, oWord; string subline; bool tmpToolInSpindle = toolInSpindle; int cmdTNr = -1; bool foundM30 = false; for (int i = startAtLine; i < gCode.Length; i++) { tmp = cleanUpCodeLine(gCode[i]); if ((!string.IsNullOrEmpty(tmp)) && (tmp[0] != ';'))//trim lines and remove all empty lines and comment lines { int cmdMNr = gcode.getIntGCode('M', tmp); int cmdGNr = gcode.getIntGCode('G', tmp); cmdTNr = gcode.getIntGCode('T', tmp); /***** Subroutine? ********************************************************/ #region subroutine // if (tmp.IndexOf("M98") >= 0) // any subroutines? if (cmdMNr == 98) { pWord = findDouble("P", -1, tmp); lWord = findDouble("L", 1, tmp); int subStart = 0, subEnd = 0; if (pWord > 0) { oWord = -1; for (int si = i; si < gCode.Length; si++) // find subroutine { subline = gCode[si]; if (subline.IndexOf("O") >= 0) // find O-Word { oWord = findDouble("O", -1, subline); if (oWord == pWord) { subStart = si + 1; // note start of sub } } else // find end of sub { if (subStart > 0) // is match? { if (subline.IndexOf("M99") >= 0) { subEnd = si; break; } // note end of sub } } } if (subStart < subEnd) { for (int repeat = 0; repeat < lWord; repeat++) { for (int si = subStart; si < subEnd; si++) // copy subroutine { streamingBuffer.Add(gCode[si], si); } // add gcode line to list to send } } } } #endregion /***** Subroutine ********************************************************/ else { if (grbl.unknownG.Contains(cmdGNr)) { streamingBuffer.SetSentLine("(" + tmp + " - unknown)"); // don't pass unkown GCode to GRBL because is unknown tmp = streamingBuffer.GetSentLine(); streamingBuffer.LineWasReceived(); addToLog(tmp); } if (cmdTNr >= 0) // T-word is allowed by grbl - no need to filter { setToolChangeCoordinates(cmdTNr, tmp); } if (cmdMNr == 6) //M06 is not allowed - remove { if (Properties.Settings.Default.ctrlToolChange) { insertToolChangeCode(i, ref tmpToolInSpindle); tmp = "(" + tmp + ")"; } } if (cmdMNr == 30) { if (skipM30) { tmp = "(" + tmp + ")"; } } streamingBuffer.Add(tmp, i); // add gcode line to list to send if (cmdMNr == 30) { foundM30 = true; break; } } } } if (!foundM30) { if (!skipM30) { streamingBuffer.Add("M30", gCode.Length - 1); // add end } } streamingBuffer.Add("($END)", gCode.Length - 1); // add gcode line to list to send streamingBuffer.Add("()", gCode.Length - 1); // add gcode line to list to send } // lock timerSerial.Start(); isStreaming = true; updateControls(); if (startAtLine > 0) { pauseStreaming(); isStreamingPause = true; } else { waitForIdle = false; preProcessStreaming(); // 411 } } // startStreaming
/********************************************************************************** * preProcessStreaming copy line by line (requestSend(line)) to sendBuffer until * grbl-buffer (grblBufferFree) is filled, M0 or M30 or buffer-end reached. * Insert script-code on tool change. * requestSend -> processSend -> sendLine - dec. grblBufferFree * called in startStreaming, pauseStreaming (to restart) and timer **********************************************************************************/ private void preProcessStreaming() { int lengthToSend = streamingBuffer.LengthSent() + 1; if (waitForIdle || isStreamingRequestPause || (countPreventIdle > 0)) { return; } while ((streamingBuffer.IndexSent < streamingBuffer.Count) && (grblBufferFree >= lengthToSend) && !waitForIdle && (streamingStateNow != grblStreaming.pause) && externalCOMReady()) { lock (sendDataLock) { string line = streamingBuffer.GetSentLine(); if ((line == "OV") || (line == "UR")) { Logger.Error("preProcessStreaming read:{0} from streamingBuffer index:{1} count:{2}", line, streamingBuffer.IndexSent, streamingBuffer.Count); break; } streamingStateNow = grblStreaming.ok; // default status int cmdMNr = gcode.getIntGCode('M', line); if ((replaceFeedRate) && (gcode.getStringValue('F', line) != "")) { string old_value = gcode.getStringValue('F', line); replaceFeedRateCmdOld = old_value; line = line.Replace(old_value, replaceFeedRateCmd); streamingBuffer.SetSentLine(line); } if ((replaceSpindleSpeed) && (gcode.getStringValue('S', line) != "")) { string old_value = gcode.getStringValue('S', line); line = line.Replace(old_value, replaceSpindleSpeedCmd); replaceSpindleSpeedCmdOld = old_value; streamingBuffer.SetSentLine(line); } #region M0 // Program pause if ((cmdMNr == 0) && !isStreamingCheck) // M0 request pause { if (!Properties.Settings.Default.guiDisableProgramPause) { if (logStartStop || logReceive) { Logger.Trace("[Pause streaming - skip M0 - wait for IDLE] indx-sent:{0} line:{1} lineNr:{2} grblBufferFree:{3}", streamingBuffer.IndexSent, streamingBuffer.GetSentLine(), streamingBuffer.GetSentLineNr(), grblBufferFree); } addToLog("[Pause streaming - skip M0 line:" + streamingBuffer.GetSentLineNr() + "]"); lineStreamingPause = streamingBuffer.GetSentLineNr(); if (grblStateNow == grblState.idle) { requestSend("G4 P1"); // changed from 2 to 1 2021-01-26 grblStateNow = grblStateLast = grblState.unknown; countPreventInterlock = 10; } isStreamingRequestPause = true; streamingStateNow = grblStreaming.waitidle; waitForIdle = true; // block further sending waitForOk = false; getParserState = true; // ask for parser state line = "(M0)"; } } #endregion if (isMarlin) { if (updateMarlinPosition || (--insertMarlinCounter <= 0)) { //requestSend("M114", streamingBuffer.GetSentLineNr(), false); // insert getPosition commands sendBuffer.Add("M114", streamingBuffer.GetSentLineNr()); getMarlinPositionWasSent = true; streamingBuffer.LineWasSent(); streamingStateOld = streamingStateNow; lengthToSend = streamingBuffer.LengthSent() + 1; // update while-variable insertMarlinCounter = insertMarlinCounterReload; } updateMarlinPosition = false; } // requestSend(line, streamingBuffer.GetSentLineNr(), false); // fill sendBuffer, sendBuffer.Add(line, streamingBuffer.GetSentLineNr()); if (logEnable) { System.IO.File.AppendAllText(Application.StartupPath + "\\logSendBuffer.nc", line + "\r\n"); // clear file } streamingBuffer.LineWasSent(); streamingStateOld = streamingStateNow; lengthToSend = streamingBuffer.LengthSent() + 1; // update while-variable } // lock processSend(); // Logger.Trace("preProcessStreaming sent {0} lengthToSend {1} grblBufferFree {2} 3busy {3} countPreventIdle {4} line {5}", streamingBuffer.IndexSent, lengthToSend, grblBufferFree, serial3Busy, countPreventIdle, line); } // while if (streamingStateNow != grblStreaming.pause) { if (trgEvent) { sendStreamEvent(streamingStateNow); } trgEvent = false; } // streaming in preProcessStreaming }
/************************************************************************ * pauseStreaming() * Pause or restart streaming after button click ************************************************************************/ public void pauseStreaming() { updateLogging(); if (logStartStop) { Logger.Trace("Ser:{0} pauseStreaming() isStreamingPause {1} isStreamingRequestPause {2} {3}", iamSerial, isStreamingPause, isStreamingRequestPause, listInfoStream()); } // start pause if (!isStreamingPause) { isStreamingRequestPause = true; // wait until buffer is empty before switch to pause addToLog("[Pause streaming - wait for IDLE]"); addToLog("[Save Settings]"); Logger.Info("pauseStreaming RequestPause ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲"); streamingStateNow = grblStreaming.waitidle; getParserState = true; } // restart streaming else { //if ((posPause.X != posWork.X) || (posPause.Y != posWork.Y) || (posPause.Z != posWork.Z)) addToLog("++++++++++++++++++++++++++++++++++++"); timerSerial.Interval = grbl.pollInterval; if (parserStateGC.Contains("F0")) { parserStateGC = parserStateGC.Replace("F0", "F100"); // Avoid missing feed rate addToLog(string.Format("[Fix F0: {0}]", parserStateGC)); } if (!xyzPoint.AlmostEqual(posPause, posWork)) // restore position { if (logStartStop) { Logger.Trace("AlmostEqual posPause X{0:0.000} Y{1:0.000} posWork X{2:0.000} Y{3:0.000}", posPause.X, posPause.Y, posWork.X, posWork.Y); } addToLog("[Restore Position]"); requestSend(string.Format("G90 G0 X{0:0.000} Y{1:0.000}", posPause.X, posPause.Y).Replace(',', '.')); // restore last position if (logStartStop) { Logger.Trace("[Restore] X{0:0.000} Y{1:0.000} State:{2}", posPause.X, posPause.Y, parserStateGC); } requestSend("G4 P0.5"); // wait 1 second // changed from 1 to 0,5 2021-01-26 requestSend(string.Format("G0 Z{0:0.000}", posPause.Z).Replace(',', '.')); // restore last position } addToLog("[Start streaming - no echo]"); addToLog("[Restore Settings: " + parserStateGC + " ]"); Logger.Info("pauseStreaming start streaming ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼"); streamingStateNow = grblStreaming.ok; lineStreamingPause = -1; // save line were M0 appeared for main GUI to show notification string parserStateGC_wo_M = ""; if (parserStateGC.Contains("M")) { parserStateGC.Substring(parserStateGC.IndexOf("M") - 1); } if (parserStateGC.Length > 0) { if (isGrblVers0) { addToLog("[Restore Settings woM: " + parserStateGC_wo_M + " ]"); requestSend(parserStateGC_wo_M); } else { addToLog("[Restore Settings: " + parserStateGC + " ]"); requestSend(parserStateGC); } } waitForIdle = false; waitForOk = false; isStreamingPause = false; isStreamingRequestPause = false; preProcessStreaming(); // processSend(); } updateControls(); } // pauseStreaming
/********************************************************************************** * preProcessStreaming copy line by line (requestSend(line)) to sendBuffer until * grbl-buffer (grblBufferFree) is filled, M0 or M30 or buffer-end reached. * Insert script-code on tool change. * requestSend -> processSend -> sendLine - dec. grblBufferFree * called in startStreaming, pauseStreaming (to restart) and timer **********************************************************************************/ private void preProcessStreaming() { int lengthToSend = streamingBuffer.LengthSent() + 1; if (waitForIdle || isStreamingRequestPause) { return; } while ((streamingBuffer.IndexSent <= streamingBuffer.Count) && (grblBufferFree >= lengthToSend) && !waitForIdle && (streamingStateNow != grblStreaming.pause)) { string line = streamingBuffer.GetSentLine(); streamingStateNow = grblStreaming.ok; // default status int cmdMNr = gcode.getIntGCode('M', line); if ((replaceFeedRate) && (gcode.getStringValue('F', line) != "")) { string old_value = gcode.getStringValue('F', line); replaceFeedRateCmdOld = old_value; line = line.Replace(old_value, replaceFeedRateCmd); streamingBuffer.SetSentLine(line); } if ((replaceSpindleSpeed) && (gcode.getStringValue('S', line) != "")) { string old_value = gcode.getStringValue('S', line); line = line.Replace(old_value, replaceSpindleSpeedCmd); replaceSpindleSpeedCmdOld = old_value; streamingBuffer.SetSentLine(line); } #region M0 // Program pause if ((cmdMNr == 0) && !isStreamingCheck) // M0 request pause { if (!Properties.Settings.Default.guiDisableProgramPause) { if (logStartStop || logReceive) { Logger.Trace("[Pause streaming - skip M0 - wait for IDLE] indx-sent:{0} line:{1} lineNr:{2} grblBufferFree:{3}", streamingBuffer.IndexSent, streamingBuffer.GetSentLine(), streamingBuffer.GetSentLineNr(), grblBufferFree); } addToLog("[Pause streaming - skip M0 line:" + streamingBuffer.GetSentLineNr() + "]"); lineStreamingPause = streamingBuffer.GetSentLineNr(); if (grblStateNow == grblState.idle) { requestSend("G4 P2"); grblStateNow = grblStateLast = grblState.unknown; countPreventInterlock = 10; } isStreamingRequestPause = true; streamingStateNow = grblStreaming.waitidle; waitForIdle = true; // block further sending waitForOk = false; getParserState = true; // ask for parser state line = "(M0)"; } } #endregion requestSend(line, streamingBuffer.GetSentLineNr(), false); // fill sendBuffer, streamingBuffer.LineWasSent(); streamingStateOld = streamingStateNow; lengthToSend = streamingBuffer.LengthSent() + 1; // update while-variable } // while if (streamingStateNow != grblStreaming.pause) { sendStreamEvent(streamingStateNow); // streaming in preProcessStreaming } }
public void updateStreaming(string rxString) { int tmpIndex = gCodeLinesSent; if ((rxString != "ok") || (rxString != "[enabled]")) { if (rxString.IndexOf("error") >= 0) { grblStatus = grblStreaming.error; tmpIndex = gCodeLinesConfirmed; addToLog(">> " + rxString); } if (getParserState) { if (rxString.IndexOf("[G") >= 0) { parserState = rxString.Substring(1, rxString.Length - 2); parserState = parserState.Replace("M0", ""); getParserState = false; } } } if (!(isStreaming && !isStreamingPause)) { addToLog(string.Format("< {0}", rxString)); } if (sendLinesConfirmed < sendLinesCount) { grblBufferFree += (sendLines[sendLinesConfirmed].Length + 1); //update bytes supose to be free on grbl rx bufer sendLinesConfirmed++; // line processed if (sendLines.Count > 1) { sendLines.RemoveAt(0); sendLinesConfirmed--; sendLinesSent--; sendLinesCount--; } } if ((sendLinesConfirmed == sendLinesCount) && (grblStateNow == grblState.idle)) // addToLog(">> Buffer empty\r"); { if (isStreamingRequestPause) { isStreamingPause = true; isStreamingRequestPause = false; grblStatus = grblStreaming.pause; if (getParserState) { requestSend("$G"); } } } if (isStreaming) { if (!isStreamingPause) { gCodeLinesConfirmed++; //line processed } if (gCodeLinesConfirmed >= gCodeLinesCount) //Transfer finished and processed? Update status and controls { isStreaming = false; addToLog("[Streaming finish]"); grblStatus = grblStreaming.finish; if (isStreamingCheck) { requestSend("$C"); isStreamingCheck = false; } updateControls(); } else//not finished { if (!(isStreamingPause || isStreamingRequestPause)) { proceedStreaming();//If more lines on file, send it } } if (tmpIndex >= gCodeLinesCount) { tmpIndex = gCodeLinesCount - 1; } sendStreamEvent(gCodeLineNr[tmpIndex], grblStatus); } }