Reading and Structuring a CNC Program
A CNC program is not a random list of numbers. This page teaches the anatomy: program number, blocks, word addresses, sequence numbers, and the standard program skeleton every program follows.
Concept
A CNC program consists of blocks (lines), each containing words (letter + number). A word like X25.5 means "the X coordinate is 25.5." A block may contain multiple words: G01 X25.5 Y10. F200. means linear motion to (25.5, 10) at 200 mm/min.
The program number (O1001) identifies the program on the controller. Sequence numbers (N10, N20) label blocks for reference but do not affect execution. Comments in parentheses explain what the code does for human readers.
Why It Matters
One line is a combination of states and actions. G01 stays active until another motion code replaces it. If you forget that G01 is modal and write X100 without F, the feed from earlier still applies. Reading a program means tracking state as you go, not just reading each line in isolation.
A block does not execute "left to right" the way a script does. The controller evaluates all words in a block together: G01 sets the motion mode, X/Y set the target, F sets the feed. Avoid writing conflicting G-codes from the same modal group on one line; the result depends on the controller and can be rejected or ambiguous. Understanding how words group and override is what separates reading code from guessing at it. When in doubt, write one modal group per block and keep the program explicit rather than relying on implicit behavior.
The program number after O identifies this program on the controller and is used when selecting it from memory. On FANUC and Haas, O followed by four or more digits is the program number. The percent signs at top and bottom mark the start and end of the program file; they are optional on some controllers but good practice. Comments in parentheses are ignored by the machine and exist only for humans reading the code. A well-commented program explains the operation, tool, and setup at key points without repeating what the code already says.
How It Works
Anatomy of a Block
N10 G01 X100.0 Y50.0 F300.0
- N10 — sequence number (label only; does not affect motion)
- G01 — linear interpolation (motion mode, modal)
- X100.0 — X coordinate target
- Y50.0 — Y coordinate target
- F300.0 — feed rate (modal)
Standard Program Skeleton
- Program start — % and O number
- Startup block — G21 G17 G90 G40 G49 G80 (known state)
- Tool change and spindle — T01 M06, S2000 M03
- Positioning and machining — G00 approach, G01 cuts, cycles
- Retract and shutdown — G00 Z50, M09, M05
- Program end — M30, %
Complete Example Program
%
O1001 (DEMO PROGRAM)
G21 G17 G90 G94 G40 G49 G80
G54
T01 M06
S2000 M03
G00 G43 H01 Z50. M08
G00 X0 Y0
G00 Z2.
G01 Z-2. F100.
G01 X50. F300.
G00 Z50. M09
M05
M30
%
Read it top to bottom: startup, tool, spindle, approach, cut, retract, shutdown. Every CNC program follows this skeleton.
Example: Line-by-Line Trace
Entry conditions: Haas mill, tool changed, G54 and H01 offsets already set, Z50 and all XY paths verified clear of fixtures. We trace what the machine does at each line, tracking modal state:
| Line | What happens | State after |
|---|---|---|
| G21 G17 G90 G94... | Known state: metric, XY plane, absolute, feed/min, comp off | G21/G17/G90/G94/G40/G49/G80 active |
| G54 | Select work offset 54 | G54 active |
| T01 M06 | ATC picks tool 1 | Tool 1 in spindle |
| S2000 M03 | Spindle on CW at 2000 RPM | Spindle running CW |
| G00 G43 H01 Z50. | Rapid to Z50 with tool length offset H01 | Z50 reached, H01 active, M08 coolant on |
| G00 X0 Y0 | Rapid XY to (0,0) at Z50 | X0 Y0 Z50 |
| G00 Z2. | Rapid Z to 2 mm above surface | X0 Y0 Z2 |
| G01 Z-2. F100. | Feed down to Z-2 at 100 mm/min (G94) | G01 active, F100 modal |
| G01 X50. F300. | Feed to X50 at 300 mm/min | F300 modal, at X50 Y0 Z-2 |
| G00 Z50. M09 | Rapid up to Z50, coolant off | G00 active, Z50, M09 |
| M05 | Spindle stop | Spindle off |
| M30 | End program, rewind | Program stops |
Common Mistakes
- Treating N numbers as coordinates — N10 is a label, not a move distance.
- Confusing S and F — S is spindle RPM; F is feed rate. They are independent.
- Using M30 as a move command — M30 ends the program and rewinds; it does not move axes.
- Forgetting the startup block — leftover modal state from a previous program causes unexpected motion.
Practice
1. Distance 30 mm at F300. Cutting time?
Show answer
30/300 = 0.1 minutes = 6 seconds.
2. What does T03 M06 do?
Show answer
Select tool 3 and execute the automatic tool change on a mill. On a lathe, T0303 indexes turret to station 3 and loads offset 3.
3. Why is the startup block important?
Show answer
It establishes known machine state: metric units, XY plane, absolute mode, compensation off, cycles off. Without it, leftover state from the previous program can cause crashes.
Sources
- Haas G00 Rapid Positioning — supports rapid motion behavior.
- Haas G01 Linear Interpolation — supports feed motion and F command rules.
- Program examples and speeds/feeds are classroom teaching assumptions; verify against tooling catalogs and machine documentation.