Example #1
0
 public void SetError(int error)
 {
     GrblError = error;
     Message   = error == 0 ? "" : GrblErrors.GetMessage(error.ToString());
 }
Example #2
0
 public void SetGrblError(int error)
 {
     GrblError = error;
     Message   = error == 0 ? string.Empty : GrblErrors.GetMessage(error.ToString());
 }
Example #3
0
        public void DataReceived(string data)
        {
            if (data.Length == 0)
            {
                return;
            }

            if (SuspendProcessing)
            {
                OnResponseReceived?.Invoke(data);
                return;
            }

            if (ResponseLogVerbose || !(data.First() == '<' || data.First() == '$' || data.First() == 'o' || (data.First() == '[' && DataIsEnumeration(data))) || data.StartsWith("error"))
            {
                if (!(data.First() == '<' && ResponseLogFilterRT))
                {
                    if (data.StartsWith("error:"))
                    {
                        var msg = GrblErrors.GetMessage(data.Substring(6));
                        ResponseLog.Add(data + (msg == data ? "" : " - " + msg));
                    }
                    else if (!ResponseLogFilterOk || data != "ok")
                    {
                        ResponseLog.Add(data);
                    }

                    if (ResponseLog.Count > 200)
                    {
                        ResponseLog.RemoveAt(0);
                    }
                }
            }

            if (data.First() == '<')
            {
                ParseStatus(data);

                OnRealtimeStatusProcessed?.Invoke(data);
            }
            else if (data.StartsWith("ALARM"))
            {
                string[] alarm = data.Split(':');

                SetGRBLState("Alarm", alarm.Length == 2 ? int.Parse(alarm[1]) : -1, false);
            }
            else if (data.StartsWith("["))
            {
                switch (data.Substring(1, data.IndexOf(':') - 1))
                {
                case "PRB":
                    ParseProbeStatus(data);
                    break;

                case "GC":
                    ParseGCStatus(data);
                    break;

                case "TLR":
                    TloReference = dbl.Parse(data.Substring(5).TrimEnd(']'));
                    break;

                case "TLO":
                    // Workaround for legacy grbl, it reports only one offset...
                    ToolOffset.SuspendNotifications = true;
                    ToolOffset.Z = double.NaN;
                    ToolOffset.SuspendNotifications = false;
                    // End workaround

                    ToolOffset.Parse(data.Substring(5).TrimEnd(']'));

                    // Workaround for legacy grbl, copy X offset to Z (there is no info available for which axis...)
                    if (double.IsNaN(ToolOffset.Z))
                    {
                        ToolOffset.Z = ToolOffset.X;
                        ToolOffset.X = ToolOffset.Y = 0d;
                        OnPropertyChanged(nameof(IsToolOffsetActive));
                    }

                    GrblWorkParameters.ToolLengtOffset.Z = ToolOffset.Z;
                    // End workaround
                    break;

                case "HOME":
                    ParseHomedStatus(data);
                    break;

                case "MSG":
                    data = data.Substring(5).Trim().TrimEnd(']');
                    if (data == "'$H'|'$X' to unlock")
                    {
                        Message = GrblInfo.IsGrblHAL ? "<Unlock> to continue" : "<Home> or <Unlock> to continue";
                    }
                    else if (GrblState.State == GrblStates.Alarm && data != "Caution: Unlocked")
                    {
                        switch (GrblState.Substate)
                        {
                        case 10:
                            _message = "clear then <Reset> then <Unlock> to continue";
                            break;

                        case 11:
                            _message = "<Home> to continue";
                            break;

                        default:
                            _message = "<Reset> then <Unlock> to continue";
                            break;
                        }
                        Message = (data == "Reset to continue" ? string.Empty : data + ", ") + _message;
                    }
                    else
                    {
                        Message = data;
                    }
                    if (data == "Pgm End")
                    {
                        ProgramEnd = true;
                    }
                    break;
                }
            }

            else if (data.StartsWith("Grbl"))
            {
                if (Poller != null)
                {
                    Poller.SetState(0);
                }
                _grblState.State = GrblStates.Unknown;
                var msg = Message;
                GrblReset = true;
                OnGrblReset?.Invoke(data);
                Message = msg;
                _reset  = false;
            }
            else if (_grblState.State != GrblStates.Jog)
            {
                if (data == "ok")
                {
                    OnCommandResponseReceived?.Invoke(data);
                }
                else
                {
                    if (data.StartsWith("error:"))
                    {
                        try
                        {
                            SetGrblError(int.Parse(data.Substring(6)));
                        }
                        catch
                        {
                        }
                        OnCommandResponseReceived?.Invoke(data);
                    }
                    else if (!data.StartsWith("?"))
                    {
                        //                 Message = data; //??
                    }
                }
            }
            OnResponseReceived?.Invoke(data);
        }