Hello Guest it is January 28, 2023, 04:20:15 AM

Author Topic: Mach4 Macro - Automatically load next Gcode file  (Read 840 times)

0 Members and 1 Guest are viewing this topic.

Mach4 Macro - Automatically load next Gcode file
« on: June 11, 2020, 06:49:40 PM »
Hello Mach4 gurus,

I have a machine that runs a long list of sequential files (hundreds of files, with each file running for just a few minutes). In order to streamline the process of running, I would like to accomplish the following:

When Gcode file is finished running, automatically load the next Gcode file.     
(Note: My post-processor is able to insert the path for the next file as a comment in the current file.)

The test Gcode file is "C:\gcodetest\TEST.txt"
The first few lines look like this:

Code: [Select]
(Next File in Queue)

G0 X0.000000 Y0.000000 Z0.200000
M3 (start Spindle)
G0 X0.000000 Y0.000000 Z5.00000
G0 X1.179950 Y4.004260 Z0.200000

Just enough to demonstrate that the file loads and runs the machine, then test M100.

The M100 code looks like this:

Code: [Select]

function M100 ()

local inst = mc.mcGetInstance()

-- Get next file name
local NextFileParen = mc.mcCntlGetGcodeLine (inst, 1) --gets name of next Gcode file
local NextFile = string.gsub(NextFileParen, "[()]", "") --strips parentheses
wx.wxMessageBox("Next file path - "..NextFile) --for troubleshooting

--Close current file
mc.mcCntlCloseGCodeFile (inst)
wx.wxMessageBox("closed - "..CurrentFile) --for troubleshooting

--Load next file **NOT WORKING**
mc.mcCntlLoadGcodeFile (inst, NextFile)
wx.wxMessageBox("opened "..NextFile) --for troubleshooting


This macro runs through the end (including the last message box), but does not load the next file.

Does anyone know how I could get this macro running?
Thank you for your insight,
Re: Mach4 Macro - Automatically load next Gcode file
« Reply #1 on: June 11, 2020, 07:27:44 PM »
Also note:

I am also looking at an alternative route where I save the path to a register, and use a button to read from the register to load the file. I am able to write to and read from the register, but even loading the Gcode from the button is causing issues.

To test the most basic scenario I attempt to use the following script in a button:

Code: [Select]
local inst = mc.mcGetInstance()
mc.mcCntlLoadGcodeFile(inst, "C:\\gcodetest\\TEST2.txt")

And i receive the following: "error 5: access denied"

Either of these methods would be acceptable, does anyone happen to know a solution?
Thank you,

Offline Graham Waterworth

  • *
  •  2,535 2,535
  • Yorkshire Dales, England
    • View Profile
Re: Mach4 Macro - Automatically load next Gcode file
« Reply #2 on: June 11, 2020, 08:03:16 PM »
Why not do it in g-code with an M98 and the file name, store the programs in the subroutine folder inside mach.
Without engineers the world stops
Re: Mach4 Macro - Automatically load next Gcode file
« Reply #3 on: June 12, 2020, 12:29:35 PM »
Thanks for the suggestion!

I read up on it a bit and I like the simplicity of the approach, except that the subroutine folder isn't really a great place for this many files*, and I can't seem to sort out how to call a file in another location. It looks like someone figured out on Mach3 how to call a file with any path by placing a "-" before the path, (found here: https://www.machsupport.com/forum/index.php?topic=15528.0) but that trick doesn't seem to be working for Mach4.

If I *could* call a file from any location, i do really like the simplicity of this approach.

 *Current file organization has nested folders: Project>Panel>Piece. Each project has dozens of panels, and each panel can have a dozen pieces. Piling them all into one folder would be a step backward in terms of navigation/organization.
Re: Mach4 Macro - Automatically load next Gcode file
« Reply #4 on: June 14, 2020, 06:02:41 PM »
Solved! (at least for my use case)

I'm kinda embarrassed it took me so long to consider hidden characters, especially since I was grabbing an entire line from the Gcode to create the variable (carriage return at the very least, duh)

The hidden characters were removed by adding  %c%s to the existing string.gsub command, removing control characters and space characters.

Here's the code that is working for me:

Code: [Select]
--Function to automatically load and start next file, grabbed from comment in current file
function M100 ()

local inst = mc.mcGetInstance()
mc.mcCntlCycleStop(inst) -- stops cycle

local NextFileParen = mc.mcCntlGetGcodeLine (inst, 1)
--gets name of next Gcode file MUST BE COMMENTED INTO LINE 1 OF CODE

local NextFile = string.gsub(NextFileParen, "[()%c%s]", "")
--strips parentheses, control characters, and spaces

wx.wxSleep(1) --gives Mach a second to do its thing

mc.mcCntlLoadGcodeFile (inst, NextFile) -- loads up the file

mc.mcCntlCycleStart(inst) --automatically cycle start. comment out to allow manual start instead.


and here's what LINE 1 of the Gcode looks like that it is referencing:


Note the double backslash. Alternatively, it also works with single forward slash like such:


Hopefully this helps someone else out!