Exemple #1
0
/****************************************************************************
*  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
Exemple #2
0
/****************************************************************************
*  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