Guide · Programming
Fanuc Macro B Advanced: System Variables, Safe Macros and Real Shop Examples
The next step after the basics: reading positions and offsets, writing safe macros that save and restore modal state, user alarms, counters and data logging.
On this page
If you can write a WHILE loop and call a macro with G65, you know the language. This guide is about using it like the builders of probing and tool changer software do. Every example can be run in the Macro B simulator.
1. Check every argument
A macro must not guess. Vacant arguments are #0, which is not the same as zero:
IF [#18 EQ #0] THEN #3000 = 1 (R RADIUS MISSING)
IF [#18 LE 0] THEN #3000 = 2 (R MUST BE POSITIVE)
IF [#11 EQ #0] THEN #11 = 4 (H: DEFAULT 4 HOLES)#3000 stops the machine with an alarm 3000 + n and your text. The operator sees exactly what is wrong.
2. Leave the machine as you found it
A macro that changes G90/G91, the feed or the plane must restore them. The modal state is in #4001 onwards:
#30 = #4003 (G90 OR G91)
#31 = #4001 (G00/G01/G02/G03)
#32 = #4109 (F)
G91 G01 Z-5. F100.
…
G#30 G#31 F#32 (RESTORE)3. Read where the machine is
#5001–#5003 hold the end point of the last block in work coordinates, #5021–#5023 the machine position, #5061–#5063 the position where a G31 probe move stopped. The difference between machine and work position is the active offset, which a macro can use to check whether the right work offset is active before cutting.
4. Change offsets safely
Probing macros write work offsets (#5221 = G54 X …) or wear offsets (#2001 + H). Write only after checking that the probe really touched and that the result is within a sensible range:
G31 Z-30. F150.
#100 = #5063
IF [#100 LT -29.9] THEN #3000 = 10 (NO TOUCH)
IF [ABS[#100 - #1] GT 0.5] THEN #3000 = 11 (DEVIATION TOO LARGE)
#5223 = #5223 + #100 - #15. Count and log
Permanent variables #500–#999 keep their values after power off: tool life counters, batch counters, the last measured value. With POPEN, DPRNT and PCLOS a macro can send measured values to a PC for a simple quality log, without extra software.
6. Make your own cycles
G66 calls a macro after every move until G67, which turns a macro into your own canned cycle. Some controls also allow a custom G-code number that calls a macro (parameters 6050 onwards on Fanuc), so G100 can start your own cycle; this is set by parameters and needs care.
Test before the machine
Run the macro in the simulator, read the variable table block by block and try missing or wrong arguments on purpose. On the machine, run it the first time in single block with the feed override low. The pro codes list all system variables mentioned here.
More guides
- High-Speed Machining: Look-Ahead, Tolerance and Why Your Machine Never Reaches the Programmed Feed
- Trochoidal Milling Explained: Engagement Angle, Chip Thinning and When It Pays
- The Machinist of 2030: Skills That Will Matter in CNC and Additive Manufacturing
- Machining Metal 3D Printed Parts: Datums, Supports and Stock Allowance
- AI in CNC Programming: What Works Today and What to Watch Out For
- MTConnect vs OPC UA: Connecting CNC Machines for Industry 4.0