/// <summary> /// fill current gcode line structure /// </summary> private void parseGCodeToken(char cmd, double value, ref modalGroup modalState) { // Logger.Trace("parseGCodeToken {0} {1}",cmd, value); switch (Char.ToUpper(cmd)) { case 'X': x = value; break; case 'Y': y = value; break; case 'Z': z = value; break; case 'A': a = value; break; case 'B': b = value; break; case 'C': c = value; break; case 'U': u = value; break; case 'V': v = value; break; case 'W': w = value; break; case 'I': i = value; break; case 'J': j = value; break; case 'N': nNumber = (int)value; break; case 'F': modalState.feedRate = feedRate = (int)value; break; case 'S': modalState.spindleSpeed = spindleSpeed = (int)value; break; case 'G': if (value <= 3) // Motion Mode 0-3 c { modalState.motionMode = motionMode = (byte)value; if (value >= 2) { modalState.containsG2G3 = true; } } else { otherCode += "G" + ((int)value).ToString() + " "; } if (value == 10) { isSetCoordinateSystem = true; isNoMove = true; } else if ((value == 20) || (value == 21)) // Units Mode { modalState.unitsMode = (byte)value; isNoMove = true; } else if ((value >= 43) && (value < 53)) { isNoMove = true; } else if (value == 53) // move in machine coord. { ismachineCoordG53 = true; } else if ((value >= 54) && (value <= 59)) // Coordinate System Select { modalState.coordinateSystem = (byte)value; isNoMove = true; } else if (value == 90) // Distance Mode { modalState.distanceMode = (byte)value; modalState.isdistanceModeG90 = true; } else if (value == 91) { modalState.distanceMode = (byte)value; modalState.isdistanceModeG90 = false; modalState.containsG91 = true; } else if ((value == 93) || (value == 94)) // Feed Rate Mode { modalState.feedRateMode = (byte)value; } break; case 'M': if ((value < 3) || (value == 30)) // Program Mode 0, 1 ,2 ,30 { modalState.programMode = (byte)value; } else if (value >= 3 && value <= 5) // Spindle State { modalState.spindleState = spindleState = (byte)value; } else if (value >= 7 && value <= 9) // Coolant State { modalState.coolantState = coolantState = (byte)value; } modalState.mWord = (byte)value; if ((value < 3) || (value > 9)) { otherCode += "M" + ((int)value).ToString() + " "; } break; case 'T': modalState.tool = (byte)value; otherCode += "T" + ((int)value).ToString() + " "; break; case 'P': modalState.pWord = (int)value; otherCode += "P" + value.ToString() + " "; break; case 'O': modalState.oWord = (int)value; break; case 'L': modalState.lWord = (int)value; break; } isdistanceModeG90 = modalState.isdistanceModeG90; }
/// <summary> /// fill current gcode line structure /// </summary> private void parseGCodeToken(char cmd, double value, ref modalGroup modalState) { switch (Char.ToUpper(cmd)) { case 'X': x = value; break; case 'Y': y = value; break; case 'Z': z = value; break; case 'A': a = value; break; case 'B': b = value; break; case 'C': c = value; break; case 'U': u = value; break; case 'V': v = value; break; case 'W': w = value; break; case 'I': i = value; break; case 'J': j = value; break; case 'F': modalState.feedRate = (int)value; break; case 'S': modalState.spindleSpeed = (int)value; break; case 'G': if (value <= 3) // Motion Mode { modalState.motionMode = (byte)value; if (value >= 2) { modalState.containsG2G3 = true; } } if (value == 53) // move in machine coord. { ismachineCoordG53 = true; } if ((value >= 54) && (value <= 59)) // Coordinate System Select { modalState.coordinateSystem = (byte)value; } if (value == 90) // Distance Mode { modalState.distanceMode = (byte)value; modalState.isdistanceModeG90 = true; } if (value == 91) { modalState.distanceMode = (byte)value; modalState.isdistanceModeG90 = false; modalState.containsG91 = true; } if ((value == 93) || (value == 94)) // Feed Rate Mode { modalState.feedRateMode = (byte)value; } if ((value == 20) || (value == 21)) // Units Mode { modalState.unitsMode = (byte)value; } break; case 'M': if ((value <= 3) || (value == 30)) // Program Mode { modalState.programMode = (byte)value; } if (value >= 3 && value <= 5) // Spindle State { modalState.spindleState = (byte)value; } if (value >= 7 && value <= 9) // Coolant State { modalState.coolantState = (byte)value; } modalState.mWord = (byte)value; break; case 'T': modalState.tool = (byte)value; break; case 'P': modalState.pWord = (int)value; break; case 'O': modalState.oWord = (int)value; break; case 'L': modalState.lWord = (int)value; break; } motionMode = modalState.motionMode; spindleState = modalState.spindleState; coolantState = modalState.coolantState; spindleSpeed = modalState.spindleSpeed; feedRate = modalState.feedRate; isdistanceModeG90 = modalState.isdistanceModeG90; }
/// <summary> /// parse gcode line /// </summary> public void parseLine(int lineNr, string line, ref modalGroup modalState) { presetParsing(lineNr, line); char cmd = '\0'; string num = ""; bool comment = false; double value = 0; line = line.ToUpper().Trim(); // 2020-07-26 isSetCoordinateSystem = false; isNoMove = false; #region parse if ((!(line.StartsWith("$") || line.StartsWith("("))) && (line.Length > 1))//do not parse grbl comments { try { foreach (char c in line) { if (c == ';') // comment? { break; } if (c == '(') // comment starts { comment = true; } if (!comment) { if (Char.IsLetter(c)) // if char is letter { if (cmd != '\0') // and command is set { if (double.TryParse(num, System.Globalization.NumberStyles.Float, System.Globalization.NumberFormatInfo.InvariantInfo, out value)) { parseGCodeToken(cmd, value, ref modalState); } } cmd = c; // char is a command num = ""; } else if (Char.IsNumber(c) || c == '.' || c == '-') // char is not letter but number { num += c; } } if (c == ')') // comment ends { comment = false; } } if (cmd != '\0') // finally after for-each process final command and number { //Logger.Trace("parseLine {0} {1}",cmd, num); if (double.TryParse(num, System.Globalization.NumberStyles.Float, System.Globalization.NumberFormatInfo.InvariantInfo, out value)) { parseGCodeToken(cmd, value, ref modalState); } } } catch (Exception er) { Logger.Error(er, "parseLine"); } } #endregion if (isSetCoordinateSystem) { resetCoordinates(); } }
/// <summary> /// parse gcode line /// </summary> public void parseLine(int lineNr, string line, ref modalGroup modalState) { presetParsing(lineNr, line); char cmd = '\0'; string num = ""; bool comment = false; double value = 0; line = line.ToUpper().Trim(); if (!(line.StartsWith("$") || line.StartsWith("("))) //do not parse grbl comments { try { foreach (char c in line) { if (c == ';') // comment? { break; } if (c == '(') // comment starts { comment = true; } if (!comment) { if (Char.IsLetter(c)) // if char is letter { if (cmd != '\0') // and command is set { value = 0; if (num.Length > 0) // try to parse previous command and number { try { value = double.Parse(num, System.Globalization.NumberFormatInfo.InvariantInfo); } catch { } } try { parseGCodeToken(cmd, value, ref modalState); } catch { } } cmd = c; // char is a command num = ""; } else if (Char.IsNumber(c) || c == '.' || c == '-') // char is not letter but number { num += c; } } if (c == ')') // comment ends { comment = false; } } if (cmd != '\0') // finally after for-each process final command and number { try { parseGCodeToken(cmd, double.Parse(num, System.Globalization.NumberFormatInfo.InvariantInfo), ref modalState); } catch { } } } catch { } } }