Use a regular expression to parse GCodes

This should parse parentheses correctly and handle (valid) gcodes such as
G1X2Y3, while before we were assuming spaces between tokens (which is wrong as
per the G-Code spec). This should fix req #333 in a clean and systematic way.
This is also a component for #343 (Marlin M114 response is in the form ok C:
X11Y22Z33E44, which we can parse with the same expression).
master
Guillaume Seguin 2013-05-18 21:16:09 +02:00
parent 06e3a02d4f
commit 6826dad61d
1 changed files with 2 additions and 1 deletions

View File

@ -20,6 +20,7 @@ import math
import datetime
gcode_parsed_args = ["x", "y", "e", "f", "z", "p", "i", "j"]
gcode_exp = re.compile("\([^\(\)]*\)|[/\*].*\n|\n|[a-z][-+]?[0-9]*\.?[0-9]*")
class Line(object):
@ -46,7 +47,7 @@ class Line(object):
self.raw = l.lower()
if ";" in self.raw:
self.raw = self.raw.split(";")[0].rstrip()
self.split_raw = self.raw.split(" ")
self.split_raw = gcode_exp.findall(self.raw)
self.command = self.split_raw[0].upper() if not self.split_raw[0].startswith("n") else self.split_raw[1]
self.is_move = self.command in ["G0", "G1"]