1. About
  2. Features
  3. Explore

I am wondering if this piece of G-code is valid:

G0 (Some comment (Its G0 command)) Y10 Z-5 

I have tested this on my Chinese CNC machine and it strips out the comment and works flawlessly.

The machine processes this as G0Y10Z-5 which seems like the correct approach to me.

I have however never seen such comment in real CNC practise. It would be nice if anyone is able to test it out on their CNC/3D Printer.

Many G-code simulators on the internet fail to process such a line in their parser so it makes me confused. I haven't found anything about it on RepRap Wiki or even Google.

1 Answer 1

This is not universally valid G-code, and how it is handled depends on the implementation. You can use this style of comment on some machines, but not all.

The way parsing used to be implemented in Marlin (a very common 3D printer firmware), it would work fine unless the comment string included a X, Y, Z, E or F character. The parser simply looks for the first occurrence of X/Y/Z/E/F and then tries to parse the bit of text appearing after that character into a number. If the string cannot be parsed as a number, it defaults to 0 instead. For example,

 G0 (Some comment containing the character Y) Y10 Z-5 

would be interpreted as G0 Y0 Z-5 and not as G0 Y10 Z-5, because ") " (the string appearing after the first occurrence of "Y") does not parse to any valid number. Your example happens to work fine because the comment string doesn't contain any special characters.

Marlin does support end-of-line comments, which should start with a semicolon and continue until the end of the line.

This is how it used to work in older Marlin versions. Newer Marlin versions have a more advanced parser, but it still would not play well with these parentheses-style comments. It is best to avoid them, as compatibility is not guaranteed.