Esempio n. 1
0
        protected virtual void OnError(GrblErrorEventArgs e)
        {
            EventHandler handler = ErrorEvent;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Process grbl response data
        /// </summary>
        private void ProcessData()
        {
            //Console.WriteLine("ProcessData ...");
            int currentStart = 0;

            //            DisplayQueue("B:",receivedData);
            //Convert the receivedData into an array for easy processing
            //exp1: 123456\n789
            byte[] bytes = receivedData.ToArray();

            int count = 0;

            //Loop through each byte inside the array
            for (int i = 0; i < bytes.Length; i++)
            {
                //Get a line break indicator
                if (bytes[i] == '\n')
                {
                    //if the beginning of the line is a line break,
                    //It is left over from previous line, ignore it and continue to the next
                    if (i == 0)
                    {
                        count++;
                        continue;
                    }

                    count += i - currentStart + 1;

                    //Convertthe byte array into a string
                    string response = System.Text.Encoding.UTF8.GetString(bytes, currentStart, i - currentStart);

                    currentStart = i + 1;

                    //Trime the response
                    string orig = response;
                    response = response.Trim();

                    respRouter.Route(response);

                    //If there is content inside the response string
                    if (response.Length > 0)
                    {
                        //Create a grbl update argument with response string
                        respArgs.Response = response;


                        //If received a ok,
                        if (response.Trim().ToUpper().Equals("OK"))
                        {
                            ExecuteQueue();
                            //If the file is running,
                            if (IsGCodeLoaded)
                            {
                                //Execute CommandOK to recalculate buffer
                                gcodeFile.CommandOK();
                                //Send next line
                                SendNextLineNew();
                            }
                            else
                            {
                                CommandComplted = true;
                            }
                        }
                        else if (response.StartsWith("$"))
                        {
                            ProcessParameter(response);
                            continue;
                        }
                        else if (response.ToUpper().StartsWith("GRBL"))
                        {
                            CMC_QUERYPARAMS();
                        }
                        else if (response.ToUpper().StartsWith("ERROR"))
                        {
                            string[]           errFlds = response.Split(':');
                            GrblErrorEventArgs errArg  = new GrblErrorEventArgs();
                            errArg.Code = errFlds[1];
                            errArg.Desc = GetErrorDesc(errFlds[1]);
                            errArg.Line = lastLineSent;
                            try
                            {
                                ErrorEvent(this, errArg);
                            }
                            catch
                            {
                            }
                        }
                        else if (response.ToUpper().StartsWith("ALARM"))
                        {
                            string[] errFlds = response.Split(':');
                            OnError(new GrblErrorEventArgs()
                            {
                                Code = errFlds[1], Desc = GetAlarmDesc(errFlds[1]), Line = lastLineSent
                            });
                        }

                        if (respArgs.State == MachineState.RUN ||
                            respArgs.State == MachineState.JOG)
                        {
                            //SetQueryTimerInterval(100);
                            QUERY_INTERVAL = 100;
                        }
                        else
                        {
                            //SetQueryTimerInterval(1000);
                            QUERY_INTERVAL = 100;
                        }

                        if (AnythingChanged(respArgs))
                        {
                            //                            logger.Info(">>>Changled<<<");
                            OnResponseReceived(respArgs);
                        }
                    }
                }
            }

            for (int j = 0; j < count; j++)
            {
                byte db = receivedData.Dequeue();
            }
        }