RS274 is the numerical control language from which printer G-code descends. Its last standard version, EIA RS-274-D, dates from 1979, and makers’ dialects differ from one another. RS274/NGC is a fuller definition drawn up for the Next Generation Controller programme; the United States National Institute of Standards and Technology (NIST) published a report that describes the language its interpreter accepts. This page follows that report. It is written for milling machines, so printer firmware keeps only part of it; see line syntax for what printers do. The individual codes and canned cycles are on RS274/NGC G and M codes.
Files and programs
- A program is lines of text, also called blocks. There is no start marker, and a program may span several files.
- A file may be framed by percent lines: the first non-blank line holds only
%, and a second such line closes the useful content. The frame is needed when there is noM2orM30. An opening%without a closing one is an error; text after the closing one is ignored. M2andM30end the program. Later lines are not run.
%
(square, 10 mm)
G21 G90
G0 X0 Y0 Z5
G1 Z-0.5 F100
G1 X10 F300
G1 Y10
G1 X0
G1 Y0
G0 Z5
M2
%
Format of a line
A line holds, in order:
- an optional block delete character,
/; - an optional line number;
- any number of words, parameter settings and comments;
- the line end (carriage return, line feed or both).
- A line may be at most 256 characters long (the limit of the report’s version).
- Anything not expressly allowed is an error.
- Spaces and tabs may appear anywhere and mean nothing outside comments:
g0x +0. 12 34y 7is the same asg0 x+0.1234 y7. - Blank lines are ignored. Letters outside comments are not case sensitive.
- When the operator’s block delete switch is on, lines starting with
/are skipped; when it is off they run. - A line number is
Nand an unsigned integer from 0 to 99999 of at most five digits. Numbers may repeat, skip or run out of order.
Words
A word is a letter other than N followed by a real value: a number, a
parameter value, an expression or a unary operation.
| Letter | Meaning |
|---|---|
A, B, C | Rotary axes |
D | Tool radius compensation number |
F | Feed rate |
G | General function |
H | Tool length offset index |
I, J, K | X, Y and Z offsets for arcs and for the G87 cycle |
L | Repeat count of canned cycles; key used with G10 |
M | Miscellaneous function |
N | Line number (not counted as a word) |
P | Dwell time in canned cycles and with G4; key used with G10 |
Q | Feed increment of the G83 cycle |
R | Arc radius; canned cycle plane |
S | Spindle speed |
T | Tool selection |
X, Y, Z | Linear axes |
Numbers
- An optional sign, digits, optionally a decimal point and more digits, with at least one digit somewhere. Leading and trailing zeros change nothing.
- A number without a point is an integer, with one a decimal. Any length that fits the line is allowed; about seventeen significant figures are kept.
- Where a whole number is required (parameter and slot numbers, M codes, ten times a G code), a value within 0.0001 of one is accepted.
Repeats and order
- Any number of
Gwords may share a line, but not two of one modal group. - Up to four
Mwords, again not two of one group. - Every other letter may appear once.
- Setting one parameter twice keeps the last setting. Of several comments only the last is used, though all are checked.
- The order of words on a line does not matter. Words, settings and comments may be mixed freely, as long as repeated settings and comments keep their order.
Parameters
The machine keeps 5400 numbered values that should survive power off.
#followed by a value from 1 to 5399 reads a parameter.#binds first:#1+2is parameter 1 plus 2, while#[1+2]is parameter 3.##2reads the parameter whose number is in parameter 2.#3=15sets a parameter. All settings on a line take effect after all reads on it, so after#3=15, the line#3=6 G1 X#3moves to X 15 and leaves 6 in parameter 3.
| Parameters | Use |
|---|---|
| 5161–5166 | G28 home position, X Y Z A B C |
| 5181–5186 | G30 home position |
| 5211–5216 | G92 axis offsets |
| 5220 | Number (1–9) of the coordinate system selected at start; default 1 |
| 5221–5226, 5241–5246, 5261–5266 and so on to 5381–5386 | Offsets of coordinate systems 1 to 9, twenty apart |
All these default to 0 except 5220.
Parameter file
The interpreter of the report keeps parameters in a text file: any header lines, exactly one empty line (no spaces), then one line per parameter giving its number and value, and optionally a comment that is not read. Numbers must ascend. The parameters in the table above are required, except those of unused rotary axes; others from 1 to 5400 may be added and are written back. The old file is kept as a backup.
Expressions and operations
An expression is enclosed in [ and ] and may nest. All
expressions on a line are worked out when it is read, before anything runs. Binary
operations exist only inside expressions; within a rank they run left to right.
| Rank | Binary operations |
|---|---|
| 1 (first) | ** power |
| 2 | *, /, MOD |
| 3 | +, -, OR, XOR, AND |
Zero is false and any other value true; logic and MOD apply to any real
numbers. Unary operations take an expression: ABS, ACOS,
ASIN, COS, EXP, FIX (round
down), FUP (round up), LN, ROUND,
SIN, SQRT, TAN. ATAN takes two,
written ATAN[2]/[1+3]. Angles are in degrees. FIX[-2.8] is
−3 and FUP[-2.8] is −2.
Modes and modal groups
A modal command stays in force until another of its group replaces it; a non-modal one acts on its own line only. Once a motion code is in force, a later line with only axis words repeats that motion.
| Group | G codes | Subject |
|---|---|---|
| 1 | G0 G1 G2 G3 G38.2 G80 to G89 | Motion |
| 2 | G17 G18 G19 | Plane |
| 3 | G90 G91 | Distance mode |
| 5 | G93 G94 | Feed rate mode |
| 6 | G20 G21 | Units |
| 7 | G40 G41 G42 | Cutter radius compensation |
| 8 | G43 G49 | Tool length offset |
| 10 | G98 G99 | Canned cycle return level |
| 12 | G54 to G59, G59.1 G59.2 G59.3 | Coordinate system |
| 13 | G61 G61.1 G64 | Path control |
| 0 | G4 G10 G28 G30 G53 G92 G92.1 G92.2 G92.3 | Non-modal |
| Group | M codes | Subject |
|---|---|---|
| 4 | M0 M1 M2 M30 M60 | Stopping |
| 6 | M6 | Tool change |
| 7 | M3 M4 M5 | Spindle |
| 8 | M7 M8 M9 | Coolant (M7 and M8 may both be on) |
| 9 | M48 M49 | Override switches |
The codes of group 0 that use axis words are G10, G28,
G30 and G92. Writing one of them and a motion code with
axis words on one line is an error. If the motion code is only in force from an
earlier line, it is suspended for the line with the group 0 code.
Coordinate systems
There is one absolute machine system and nine program systems, selected by
G54 to G59.3; the absolute system cannot be selected.
G10 L2 Pn sets the offsets of program system n.
G92 and G92.3 lay a further offset over all nine, removed
by G92.1 or G92.2. G53 makes a single
G0 or G1 move in machine coordinates.
Order of execution on a line
- Comment, including a message
- Feed rate mode (
G93,G94) - Feed rate (
F) - Spindle speed (
S) - Tool selection (
T) - Tool change (
M6) - Spindle (
M3,M4,M5) - Coolant (
M7,M8,M9) - Overrides (
M48,M49) - Dwell (
G4) - Plane (
G17–G19) - Units (
G20,G21) - Cutter radius compensation (
G40–G42) - Tool length offset (
G43,G49) - Coordinate system (
G54–G59.3) - Path control (
G61,G61.1,G64) - Distance mode (
G90,G91) - Return level (
G98,G99) - Home (
G28,G30), coordinate data (G10) or axis offsets (G92–G92.3; the report’s table prints this range as ending inG94) - Motion (
G0–G3,G80–G89), as changed byG53 - Stop (
M0,M1,M2,M30,M60)
Grammar of a line
Appendix E of the report defines which sequences of characters form a line. It is
condensed here: [ ] means zero or one, { } zero or more,
and | a choice; quoted text is literal. Letters and operation names
may be in either case, and outside comments spaces and tabs do not count.
line = ["/"] [line_number] {segment} newline
line_number = "n" digit [digit] [digit] [digit] [digit]
segment = word | comment | parameter_setting
word = letter real_value
letter = a b c d f g h i j k l m p q r s t x y z (one of)
real_value = real_number | expression | parameter_value | unary
real_number = ["+" | "-"] ( digit {digit} ["."] {digit}
| "." digit {digit} )
expression = "[" real_value {binary_operation real_value} "]"
binary_operation = "**"
| "/" | "mod" | "*"
| "and" | "xor" | "-" | "or" | "+"
unary = operation expression
| "atan" expression "/" expression
operation = "abs" | "acos" | "asin" | "cos" | "exp" | "fix"
| "fup" | "ln" | "round" | "sin" | "sqrt" | "tan"
parameter_value = "#" real_value
parameter_setting = "#" real_value "=" real_value
comment = message | "(" {comment_character} ")"
message = "(" {space} "m" {space} "s" {space} "g" {space} ","
{comment_character} ")"
A comment character is any printable character, space or tab other than a
parenthesis, so comments do not nest. Text that fits both forms of comment is a
message. A parameter index must come to an integer from 1 to 5399. The letters
E, O, U, V and W
start no word, and N only the line number. The grammar does not
express the rules about which words may appear together, so a line can fit it and
still be rejected.
Sources
- The NIST RS274NGC Interpreter – Version 3 – Kramer, Proctor and Messina, NISTIR 6556, 17 August 2000, mainly sections 1.2, 2.2, 3.1 to 3.4 and 3.8 and appendix E
ISO 6983-1, the international standard for numerical control program format, is sold by ISO and was not used for this page.
Comments and messages
), must close on the same line and cannot be nested.MSG,(in any case, spaces allowed), the rest is a message to show the operator:(MSG, change tool).