Hello Guest it is July 14, 2020, 08:02:51 PM

Author Topic: Including files in G-code file.  (Read 3634 times)

0 Members and 1 Guest are viewing this topic.

Including files in G-code file.
« on: April 24, 2015, 08:38:32 PM »
Hi all. Back after a long absence...

I am using a system with Mach 3 as its controller.

I have developed a large library of support subroutines that I use extensively in new projects.

I've looked at the documentation and cannot find a way, but hope there is a method within Mach3 that allows you to include the text of a second file into your primary file when loaded.

I know I can cut and paste my support subroutines into any new project. But that would not incorporate improvements I make later.

Something like:



would load the current version of the file when executed, not a snapshot as it existed when I did the current project.


Offline BR549

  •  6,932 6,932
    • View Profile
Re: Including files in G-code file.
« Reply #1 on: April 24, 2015, 10:00:28 PM »
You would use the sub routine method as described in the manual. You can call the sub remotely with Mach3.  The sub itself has to end with a M99 line so that when called it can return the the parent file.

M98 "12345"         would call the sub "12345" from the sub directory and would run until it completed then control would return to the main program on the line after the call.

(;-) TP
Re: Including files in G-code file.
« Reply #2 on: April 25, 2015, 02:26:36 AM »
Thanks TP. I missed that.

Looking at the impact. I would need to take each routine, make it a separate file with some naming convention and then use M98 "SomeGoodName" instead of M98 P345.

Thanks again. Need to read more closely...


Offline BR549

  •  6,932 6,932
    • View Profile
Re: Including files in G-code file.
« Reply #3 on: April 25, 2015, 10:03:36 AM »
Not a problem if you need more help with SUBs  let me know.

(;-) TP
Re: Including files in G-code file.
« Reply #4 on: April 27, 2015, 03:19:23 AM »

The use of subs works, but there are some unusual artifacts that I have had happen.

1. The syntax: M98 (file.txt) is required, not M98 "file.txt"

It is very strange to suddenly have to think of () as something other than comments!

2. I have had 4 times during testing where I would make a valid change and it would not load. It would just stop loading. Sometimes, but not in all cases, it would show a subroutine in the code box instead of the main.

I could not make that file work. However, in all cases, I copied all of the code from the file and pasted it into another file with a different filename and it worked. I do not have a clue...

3. While testing, I detected a logic error in my code and stopped the program. It stopped showing a subroutine. I clicked "Edit G-Code" and Notepad opened with the main code. I made my edits, saved and exited Notepad. The file locked up and would not load. I clicked "Close G-Code" and reloaded it by clicking "Recent File" and opening it. It would not load.

After much searching I found the issue. The code had not been saved into the main programs file. It had overwritten the subroutine file that had been on the screen when I stopped execution of the program. Strange.

4. When executing with subroutines, the asynchronous behavior between executing G-Code and the display in the code window is very disconcerting. The code display may get a few subroutines ahead of what the path is showing on the display.

I've placed a couple dozen subroutines in the directory and have been using it, but it scares me that it sometimes does not run for no apparent reason.

An aside question: Mach4 includes support for Macro G-Codes 65, 66 and 67. Do they also support the subroutine directory? In other words, would "G65 (somefile.txt) A23.3 B14.1" work by calling the file and passing 23.3 and 14.1 to the subroutine as #1 and #2?

Thanks again for the help,


Offline BR549

  •  6,932 6,932
    • View Profile
Re: Including files in G-code file.
« Reply #5 on: April 27, 2015, 10:12:34 AM »
When you run the SUB M98 ()  It will transfer the SUB to the Gcode window when it runs then return back to the main when completed.

Failed to run normally means something was wrong with the sub code. Look in the status (error ) line for a message.  The most common problem is forgetting the carrage return AFTER the M99. I always end the sub code with a % to make sure.


IF you are in a routine then edit you HAVE to make sure where you are and what you are actually editing. Look at the top of the NotePad screen for the file name that is open.

I have never seen the subs get ahead of itself. It always stays in sync with teh toolpath display.

What version Mach are you running ?
What type controller ?  LPT ? other?

As to Mach4 subs you will need to read the manual.

(;-) TP

Re: Including files in G-code file.
« Reply #6 on: April 28, 2015, 12:13:50 PM »
It is working well, but the code window definitely gets confused with the combination of subroutines and variables.

Sometimes a subroutine is running and the code window shows the first several lines of the main, which is all variable definitions and value settings.

I'll send examples, if you wish...

One stupid question: How do you set the resolution of the position indicators on the Program Run page? The set up always has a zero in the 0.000X location when I am specifically setting ot to another value. Resolution is on to thousandths instead of ten thousandths.

Thanks much for you help.

85 subroutines separated and defined...


Offline BR549

  •  6,932 6,932
    • View Profile
Re: Including files in G-code file.
« Reply #7 on: April 28, 2015, 12:44:18 PM »
What you may be seeing with subs and variables is due to mach3 being a buffered system. I t may be switching back and Forth ahead of the actual running code. It cannot do it all in real time. It is the SAME if you are using the Var monitor to view variables values on the fly.

Now I will warn you , Mach3 HAS a problem with tool offsets with subs so IF you have any look out as there is no rymthe or reason  as to when it goes bonkers.

(;-) TP
Re: Including files in G-code file.
« Reply #8 on: April 28, 2015, 02:14:58 PM »
Forgot to answer your questions: Mach3 Version R3.043.066 and no controller right now. Just testing dry runs. Very simple mill uses LPT port.

I am designing a new gantry system and will be buying Mach4 Hobby to support it. (I want Macro G65, G66 and G67.)

Thanks for the heads up on Tool Offsets. Since I almost always do parametric programming, I take care of tool offsets in my code to take full control of it...

Such as:

(Definition of the Cover to be outlined)
#1 = 4 (X Dimension)
#2 = 0.8 (Y Dimension)
#3 = .1 (Corner Radius)
#4 = 0.25 (Ear Radius)
#5 = 0.2 (Ear Height)
#6 = 1.8 (Distance between Ear Centers)
#7 = 0.25 (Tool Diameter)
#113 = 15. (Milling Feedrate)
(Calculation of the parameters for subs in directory)
#140 = [#3 + #7/2] (Corner Radius + Tool Radius)
#141 = [#2 - 2 * #3] (Y - 2 times Corner Radius)
#142 = [#2 - 2 * #3] (Y - 2 times Corner Radius)
#157 = [#4 - #5]
#156 = [COS[ASIN[#157 / #4]]*#4]
#150 = [[#1 - [#6 + 2 * #156]] / 2]
#151 = [#6 - 2 * #156]
#152 = [[#1 - 2 * #156] / 2]
#153 = #150
#154 = #151
#155 = #152
M98 (O2001)
M98 (O2012)
M98 (O2003)
M98 (O2014)
M98 (O2018)
M98 (O2007)
M98 (O2016)
M98 (O2005)
M98 (O2002)
M98 (O2004)
M98 (O2008)
M98 (O2006)
M98 (O2021)
M98 (O2022)
M98 (O2023)
M98 (O2024)
M98 (O2028)
M98 (O2027)
M98 (O2026)
M98 (O2025)

#7 is the tool diameter and #140 adjust radius for tool radius. I modify the Z values for each section using a different tool instead of using tool offsets, again, to take firmer control.

Re: Including files in G-code file.
« Reply #9 on: May 01, 2015, 02:23:06 PM »

Bothering for the last time, I hope...

Two things:

1. The Position display for all axis only shows actual values to 3 decimal places, while the display has 4 places. In other words, while the screen could show 0.0000 resolution, the rightmost value is always zero, even if I intentionally set it to something else. If I click "To Go", it shows 0.0000 resolution.

How do I make the primary display show full resolution?

2. It appears Mach3 does not support "Local Variables" like other controllers I use. When I change #1 in a subroutine, it also changes the value for the calling routines.

Is there only one set of Variables #1 to #33?

Thanks for the sharing of your knowledge of the internals...