private void MachineConnection_LineReceived(string line) { if (line.StartsWith("<")) { Match statusMatch = StatusEx.Match(line); if (!statusMatch.Success) { Console.WriteLine("Received Bad Status: '{0}'", line); return; } Group status = statusMatch.Groups["State"]; if (status.Success) { Status = status.Value; } Group mx = statusMatch.Groups["MX"], my = statusMatch.Groups["MY"], mz = statusMatch.Groups["MZ"]; if (mx.Success) { MachinePosition = new Vector3(double.Parse(mx.Value, GCodeCommand.NumberFormat), double.Parse(my.Value, GCodeCommand.NumberFormat), double.Parse(mz.Value, GCodeCommand.NumberFormat)); } Group wx = statusMatch.Groups["WX"], wy = statusMatch.Groups["WY"], wz = statusMatch.Groups["WZ"]; if (wx.Success) { WorkPosition = new Vector3(double.Parse(wx.Value, GCodeCommand.NumberFormat), double.Parse(wy.Value, GCodeCommand.NumberFormat), double.Parse(wz.Value, GCodeCommand.NumberFormat)); } StatusUpdate(); } else if (line.StartsWith("ok")) { if (ActiveCommands.Count > 0) { CharBufferCount -= ActiveCommands.Dequeue().Length; } else { Console.WriteLine("Received ok without active command"); } } else if (line.StartsWith("[PRB:")) { Match probeMatch = ProbeEx.Match(line); Group mx = probeMatch.Groups["MX"]; if (!probeMatch.Success || !mx.Success) { Console.WriteLine("Received Bad Probe: '{0}'", line); return; } double height = double.Parse(mx.Value, GCodeCommand.NumberFormat); height += WorkPosition.Z - MachinePosition.Z; if (Probes.Count > 0) { Probes.Dequeue().SetResult(height); } } else if (line.StartsWith("error")) { string command; if (ActiveCommands.Count > 0) { command = ActiveCommands.Dequeue(); CharBufferCount -= command.Length; } else { command = "No active command"; } HandleError(line, command); } else if (line.StartsWith("[")) //feedback message { } else if (line.StartsWith("ALARM")) { } else if (line.StartsWith("Grbl")) { Probes.Clear(); ActiveCommands.Clear(); CharBufferCount = 0; RequestStatus(); } }
private static bool HandleReceivedLine(string line) { if (line.StartsWith("ok")) { if (ActiveCommands.Count > 0) { CurrentGRBLBuffer -= ActiveCommands.Dequeue().Length + 1; } else { Console.Error.WriteLine("Received ok without active command"); } return(true); } else if (line.StartsWith("error")) { string activeCommand; if (ActiveCommands.Count > 0) { activeCommand = ActiveCommands.Dequeue(); CurrentGRBLBuffer -= activeCommand.Length + 1; } else { Console.Error.WriteLine("Received ok without active command"); activeCommand = "no active command"; } if (line.StartsWith(Properties.Resources.errorInvalidGCode)) { int errorNo; if (int.TryParse(line.Substring(Properties.Resources.errorInvalidGCode.Length), out errorNo)) { if (Util.GrblErrorProvider.Errors.ContainsKey(errorNo)) { App.Message($"'{activeCommand}':\n'{line}'\n{Util.GrblErrorProvider.Errors[errorNo]}"); goto skipMessage; } else { Console.Error.WriteLine($"Unknown Error ID '{line}'\ntime to update error definitions?"); } } else { Console.Error.WriteLine($"Couldn't parse Error ID in '{line}'"); } } App.Message($"'{activeCommand}':\n'{line}'"); skipMessage: return(true); } else if (line.StartsWith("Alarm")) { App.Message(line); return(true); } return(false); }