I thought that I would give an update of where I have got to in all of this so that it may be of use to others.
Having got to the point of being able to load and run G-Code programs using a screen button, I spent some time during the last couple of weeks looking at what approach works best when several buttons are to be used. What I am trying to do involves 8 or so buttons each running different pieces of code and I originally thought that having these as discrete programs, loaded each time they were needed, was the best way to go. However the main problem is that loading the individual program each time is really tedious. The other problem is that if parameter (#) values are used in the G code, they are not carried through from one operation to the next
The approach I have found works best is to have everything in one (fairly long) program with the code required for each button as a subroutine. One can then use a # parameter to call up the subroutine required. The essential parts of the program then looks like this:
M98 P#41 (CALLS THE SUBROUTINE NUMBER THAT #41 IS SET TO)
M2
%
O1 (SUBROUTINE 1)
LINES OF CODE
M99 (SUBROUTINE RETURN)
%
O2 (SUBROUTINE 2)
LINES OF CODE
M99 (SUBROUTINE RETURN)
And so on. The subroutines can themselves call other subroutines.
The deficiency in this simple arrangement is that When the file is loaded one would really like Mach3 to run through the whole program and carry out all of its checks. If the structure is as above, there is really no "path" all the way through for this to occur. This can be overcome by using a starting subroutine, that is called at the loading
stage, which is itself used to call all of the other subroutines in turn, e.g:
M98 P#41 (CALLS THE SUBROUTINE NUMBER THAT #41 IS SET TO)
M2
%
O1 (SUBROUTINE 1 - TO ALLOW WHOLE PROGRAM TO BE CHECKED OUT)
M98 P2 Q1
M98 P3 Q1
...and so on for all of the other subroutines
M99 (SUBROUTINE RETURN)
%
O2 (SUBROUTINE 2 - PROGRAM FOR FIRST BUTTON)
LINES OF CODE
M99 (SUBROUTINE RETURN)
%
O3 (SUBROUTINE 3 - PROGRAM FOR SECOND BUTTON)
LINES OF CODE
M99 (SUBROUTINE RETURN)
An M2 also appears to be needed after the last subroutine return.
One can use a button to load the file using button code along the following lines
SetVar(41,1) 'Set #41 to call subroutine 1'
Rem other # values may need to be set to avoid issues like dividing by zero
LoadFile("C:\Mach3\User GCodes\G-Code File.txt")
Sleep(3000) 'Allow 3 sec for file to load and be checked through'
Rem the time has to be adjusted according to the program length
The button script to run the operation in subroutine 2 is then along the lines:
SetVar(41,2)
Rem set any other # parameters as necessary
RunFile()
One can use "If" statements in the button script to check that various conditions are met before the piece of code is run.
What I have described is mainly what has to be done to make the approach work but a few further points need to be made:
1.The above approach with a two line main program to call the subroutine required gives some strange problems that I don't really understand. What works better is to have a similarly short main program that calls a subroutine that in turn calls the other subroutines, i.e.
M98 P10
M2
%
O10
M98 P#41 (CALLS THE SUBROUTINE NUMBER THAT #41 HAS BEEN SET TO)
M99
%
O1 (SUBROUTINE 1)
% etc.
2. It is OK to use user defined macros in the program to set UserLEDs, DROs etc.
3. When carrying out an operation using the button script after RunFile() has been called it sometimes appears necessary to have a short pause (e.g. Sleep(200))
otherwise the operation is missed.
I hope all of this will be of help to someone.
J