Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: alanbredbury on August 09, 2022, 12:38:37 PM

Title: lua in Mach 4
Post by: alanbredbury on August 09, 2022, 12:38:37 PM

really simple question, I think.
I am building a m6 in Lua in Mach4
I have a 4 tool changer it has air cylinders that need to be activated, I have them on output 2 - 5
I want to build a variable name to activate the correct cylinder for the tool change
tool 1 is output 2
Line one does not work, it assigns the variable Tsig1 the text value
line two does work it returns a number

I am putting the text that is shown in parenthesis in a variable but it is text
how do I tell the program that I want it to see the text as a output variable thingy?

local Tsig1 = ("mc.OSIG_OUTPUT2")
local Tsig2 = mc.OSIG_OUTPUT3

So really x="mc.OSIG_OUTPUT2"
Local Tsig1 = x
or I tried Tsig1 = (x) and every other kind of bracket or parenthesis
I also tried string.format
and every other thing I could find on the web
No dice
Thanks
Title: Re: lua in Mach 4
Post by: Cbyrdtopper on August 09, 2022, 12:45:26 PM
Here is how you turn on and turn off outputs in Mach4 Lua.

local TSig1 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5) --This get the handle for the output.
mc.mcSignalSetState(TSig1, 1) -- This turns on Output 5
mc.mcSignalSetState(TSig1, 0) -- This turns off Output 5

Once you make the TSig1, Tsig2, etc... variable you can call the mc.mcSignalSetState (Variable Name, 0 or 1) anywhere in your m6 without having to declare the variable again.
Title: Re: lua in Mach 4
Post by: alanbredbury on August 09, 2022, 04:23:56 PM
Well I am far past that, thanks anyway. What I an trying to do is build the mc.OSIG_OUTPUT2 through 5 based on the tool number. In other words I have successfully built a variable that holds the text string but when I out the variable in place of mc.Osig_output# it reads it as text and will not return the handle but throws an error because ots text not the machine function or whatever. There has to be a way to turn text into a usable variable. Thanks in most languages its some function, special character or brackets. For instance you combine text with .. in this language.
Title: Re: lua in Mach 4
Post by: Cbyrdtopper on August 09, 2022, 04:48:29 PM
I'm confused on what you are trying to do.
You want to turn on a specific output based on what tool it is on?

If that is the case, just put an if statement.
--Example
function m200()
local inst = mc.mcGetInstance()
local selectedTool = mc.mcToolGetSelected(inst)
local TSig1 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2)
local TSig2 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3)
local TSig3 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4)
local TSig4 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
   
if selectedTool == 1 then
     mc.mcSignalSetState(TSig1, 1)
    mc.mcSignalSetState(TSig2, 0)
    mc.mcSignalSetState(TSig3, 0)
    mc.mcSignalSetState(TSig4, 0)
elseif selectedTool == 2 then
     mc.mcSignalSetState(TSig1, 0)
    mc.mcSignalSetState(TSig2, 1)
    mc.mcSignalSetState(TSig3, 0)
    mc.mcSignalSetState(TSig4, 0)
elseif selectedTool == 3 then
     mc.mcSignalSetState(TSig1, 0)
    mc.mcSignalSetState(TSig2, 0)
    mc.mcSignalSetState(TSig3, 1)
    mc.mcSignalSetState(TSig4, 0)    
elseif selectedTool == 4 then
     mc.mcSignalSetState(TSig1, 0)
    mc.mcSignalSetState(TSig2, 0)
    mc.mcSignalSetState(TSig3, 1)
    mc.mcSignalSetState(TSig4, 0)
else
    mc.mcCntlSetLastError(inst, "OUT OF RANGE")
end--Selected Tools

end--m200

if (mc.mcInEditor() == 1) then
   m200()
end
Title: Re: lua in Mach 4
Post by: alanbredbury on August 09, 2022, 06:37:41 PM
In short i want to build a text variable that contains mc.OSIG_OUTPUT# as text and use that value in the statement seems that would be the most efficient. Im fustrated that i dont seem to be able to do long command strings as i also have SQL background. Can I pass that function the tool number  and prefered status and get a return to raise or lower the tool change station as needed? I am accustomed to visual basic and before that foxpro it's pretty different both many years ago. I mean yes I can do it all with if statements but it seems like it's going to get pretty verbose. I will play with what you provided and see what I can do with it as a function. With the old languages I could just build a text string and use it as a variable and to make a call to an address. I saw some stuff about a load command, that didn't seem to work.
Title: Re: lua in Mach 4
Post by: Cbyrdtopper on August 09, 2022, 06:42:18 PM
I have no idea. I have long since forgotten VB script. LUA is so much better.  Function based. I like it quite a lot.
There are some pretty smart people on here though, they may chime in. 
Move made some very very powerful tool change routines with LUA
Title: Re: lua in Mach 4
Post by: joeaverage on August 09, 2022, 07:29:48 PM
Hi,
Quote
In short i want to build a text variable that contains mc.OSIG_OUTPUT# as text and use that value in the statement seems that would be the most efficient

That does not work in Lua. In order to address and either set or read a variable you must first get its 'handle', which in effect a number corresponding to the memory location of the
variable.

For example:

local VariableHandle=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT5)
mc.mcSignalSetState(VariableHandle,1)

So the only way you can address a signal is by using its handle. That's the way Lua is and always has been.

Unlike languages that you may be used to which have a variable table and using it you can address any variable you require and remains fixed for the duration of the program.
In Lua variables come and go and are located in different addresses as the program progresses, in fact many of them are in a stack which will be garbage collected once the variable
goes out of scope.

Lua is not really a computer language, but rather a scripting language, and is meant as an extension to the main program. In the case of Mach the main program is all C/C++, and
Lua is the scripting language laid on top of it for our use.

So when a Lua function is encountered  the PC sets up a stack and all variables (local) are put (called 'pushing') onto the stack while the function runs, so the memory location of a variable is changing
as new stack entities are 'pushed' onto the stack or 'popped' off it, ergo the repeated and common use of a 'handle' rather than a textual variable name.

You may find the PDF I wrote about using the SignalScript and covers some of these ideas useful:

https://www.machsupport.com/forum/index.php?topic=40051.msg267764#msg267764 (https://www.machsupport.com/forum/index.php?topic=40051.msg267764#msg267764)

Craig
Title: Re: lua in Mach 4
Post by: joeaverage on August 09, 2022, 09:54:40 PM
Hi,
when I'm coding Lua, this link is permanently open in my browser:

https://www.lua.org/manual/5.3/ (https://www.lua.org/manual/5.3/)

It tells you about the basic syntax, control structures, scope etc. My guess is you'll use the Standard Libraries quite a bit too.

The next thing you want open and you will consult dozens of times per hour is Mach4CoreAPI.chm, its the API Help file, found in C:/Mach4Hobby/Docs.

It helps to know about Machs structure and organisation but if you are reasonably familiar with the API and can refer to the Lua Reference Manual,
the link above, then you'll be able to handle 90% of what comes up.

Craig
Title: Re: lua in Mach 4
Post by: alanbredbury on August 10, 2022, 09:10:40 PM
Thanks for all the input, I had to do other stuff today and probably won't get back to it till Friday. I am sure I will succeed with all the advise here my code just may not be as elegant as I would like. I don't expect to do much more coding after this, I just want to run the machine so it's kinda a one shot deal I think. Thank you again for all the help.
Title: Re: lua in Mach 4
Post by: alanbredbury on August 11, 2022, 11:54:50 AM
Ok I got back at it today, here is my code that works to change from tool one to tool two.
I keep getting stuck as to how to use a tool change output variable that I can't build in a text string.
sure I can put a bunch of complex if statements for every time I use Tsig# but it will be so bulky
Tsig# needs to change based on

local SelectedTool = 2 ---mc.mcToolGetSelected(inst)
local CurrentTool = 1 --- mc.mcToolGetCurrent(inst)

I hard coded the 1 and 2 for debugging as you can see the tool changer is unfortunately more complex than would be optimal.
any help would be appreciated, thanks

local Tsig1 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2)
local Tsig2 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3)
local Tsig3 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4)
local Tsig4 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
   
----mc.mcSignalSetState(Tsig1out, 1)

------ Move to current tool change position ------
local GCode = ""
GCode = GCode .. "G00 G90 G53 Z0.0\n"
GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", XPos1 + 1.3, YPos1)
GCode = GCode .. string.format("G00 G90 G53 Z%.4f\n", ZPos1 + 1.0)
GCode = GCode .. string.format("G01 G90 G53 Z%.4f F15.0\n", ZPos1)
mc.mcCntlGcodeExecuteWait(inst, GCode)
---Raise tool change holder #1
mc.mcSignalSetState(Tsig1, 1)
---Move x axis to place too in change holder
GCode = ""
GCode = GCode .. string.format("G00 G90 G53 X%.4f\n", XPos1)
mc.mcCntlGcodeExecuteWait(inst, GCode)
------ Release drawbar ------
local DrawBarOut = mc.OSIG_OUTPUT7
local hsig = mc.mcSignalGetHandle(inst, DrawBarOut)
mc.mcSignalSetState(hsig, 1)
---drop tool change holder #1
mc.mcSignalSetState(Tsig1, 0)
------ Move to next tool change position ------
GCode = ""
GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", XPos2, YPos2)
mc.mcCntlGcodeExecuteWait(inst, GCode)
----Raise tool change holder #2
mc.mcSignalSetState(Tsig2, 1)
--- give it a bit of time
wx.wxSleep(1)
------ Clamp drawbar ------
mc.mcSignalSetState(hsig, 0)
---- move x axis to slide tool out of holder #2
GCode = ""
GCode = GCode .. string.format("G00 G90 G53 X%.4f\n", XPos1 + 1.3)
mc.mcCntlGcodeExecuteWait(inst, GCode)
----- lower tool change holder #2
mc.mcSignalSetState(Tsig2, 0)
------ Move Z to home position ------
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z0.0\n")
------ Reset state ------
Title: Re: lua in Mach 4
Post by: Cbyrdtopper on August 11, 2022, 02:01:47 PM
Just a matter of opinion, but I personally don't make a "GCode" string variable, I just have lines of mc.mcCntlGcodeExecuteWait with the code inside of it.  You are doing just that in some cases.  Just keep it going. 
Also, I don't think it is necessary to have %.4f every time you call a position.  Also, I'm assuming you are declaring your XPos1, YPos1 etc as local variables at the top of your macro?
Just declare each position as a variable and call it later in your mc.mcCntlGcodeExecuteWait. 

I'm also attaching the m6 I've been working on for my current mill in progress.

Here is how I would do what you've done.

local inst = mc.mcGetInstance()
local selectedTool = mc.mcToolGetSelected(inst)
local TSig1 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2)
local TSig2 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3)
local TSig3 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4)
local TSig4 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
local DrawBarOut = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7)
local ZPos = 1.2345
local XPos1 = 1.2345
local YPos1 = 1.2345
local XPos2 = 2.3456
local YPos2 = 2.3456

--Tool 1 Position
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z0.0")
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 X" .. (XPos1 + 1.3) .. "Y" .. YPos1)
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z" .. (ZPos1 + 1.0))
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z" .. Zpos1)
--Raise tool change holder #1
mc.mcSignalSetState(TSig1, 1)
--Move x axis to place too in change holder
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 X" .. XPos1)
--Release the drawbar
mc.mcSignalSetState(DrawBarOut, 1)
--drop tool change holder #1
mc.mcSignalSetState(TSig1, 0)

--Tool 2 Position
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z0.0")
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 X" .. (XPos2 + 1.3) "Y" .. YPos2)
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z" .. (ZPos2 + 1.0))
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z" .. ZPos)

--Raise tool change holder #1
mc.mcSignalSetState(TSig2, 1)
--Release the drawbar
mc.mcSignalSetState(DrawBarOut, 0)
wx.wxMilliSleep(1000)--1 Second
--Move X Axis to grab tool.
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 X" .. (XPos2 + 1.3))

--Drop tool change holder #2
mc.mcSignalSetState(TSig2, 0)

--Move to Z Clearance
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z0.0")
Title: Re: lua in Mach 4
Post by: alanbredbury on August 11, 2022, 02:23:51 PM
well that's nice and all but what about when the current tool is 2 and it calls for tool 4. I know what tool it has and wants but I need to move the correct change station. I need to get the number from the tool into the tool change number. in prior languages I have used I could just make a text string Tsig# whatever I wanted and use it as the variable apparently there is no way to use a text value as a variable in this language.
Thanks
Title: Re: lua in Mach 4
Post by: alanbredbury on August 11, 2022, 03:45:07 PM
Got it, still needs a bit of cleanup, just had to get the way a function works in this thing
thanks

function TchgStation(SelectedTool, CurrentTool)

   local Tsig1 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2)
   local Tsig2 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3)
   local Tsig3 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4)
   local Tsig4 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
   if (SelectedTool == 1) then
      TsigSout = Tsig1
   elseif (SelectedTool == 2) then
      TsigSout = Tsig2
   elseif (SelectedTool == 3) then
      TsigSout = Tsig3
   elseif (SelectedTool == 4) then
      TsigSout = Tsig4
   end
   if (CurrentTool == 1) then
      TsigCout = Tsig1
   elseif (CurrentTool == 2) then
      TsigCout = Tsig2
   elseif (CurrentTool == 3) then
      TsigCout = Tsig3
   elseif (CurrentTool == 4) then
      TsigCout = Tsig4
   end   

   return TsigSout, TsigCout;
end
   
TchgStation(SelectedTool, CurrentTool)

------ Move to current tool change position ------
local GCode = ""
GCode = GCode .. "G00 G90 G53 Z0.0\n"
GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", XPos1 + 1.3, YPos1)
GCode = GCode .. string.format("G00 G90 G53 Z%.4f\n", ZPos1 + 1.0)
GCode = GCode .. string.format("G01 G90 G53 Z%.4f F15.0\n", ZPos1)
mc.mcCntlGcodeExecuteWait(inst, GCode)
---Raise tool change holder #1
mc.mcSignalSetState(TsigCout, 1)
---Move x axis to place too in change holder
GCode = ""
GCode = GCode .. string.format("G00 G90 G53 X%.4f\n", XPos1)
mc.mcCntlGcodeExecuteWait(inst, GCode)
------ Release drawbar ------
local DrawBarOut = mc.OSIG_OUTPUT7
local hsig = mc.mcSignalGetHandle(inst, DrawBarOut)
mc.mcSignalSetState(hsig, 1)
---drop tool change holder #1
mc.mcSignalSetState(TsigCout, 0)
------ Move to next tool change position ------
GCode = ""
GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", XPos2, YPos2)
mc.mcCntlGcodeExecuteWait(inst, GCode)
----Raise tool change holder #2
mc.mcSignalSetState(TsigSout, 1)
--- give it a bit of time
wx.wxSleep(1)
------ Clamp drawbar ------
mc.mcSignalSetState(hsig, 0)
---- move x axis to slide tool out of holder #2
GCode = ""
GCode = GCode .. string.format("G00 G90 G53 X%.4f\n", XPos1 + 1.3)
mc.mcCntlGcodeExecuteWait(inst, GCode)
----- lower tool change holder #2
mc.mcSignalSetState(TsigSout, 0)
------ Move Z to home position ------
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z0.0\n")
Title: Re: lua in Mach 4
Post by: SwiftyJ on August 12, 2022, 05:03:01 AM
You're not really gaining anything by using a function there as you are only calling it once.

If you want to tidy things up you can replace that function with all the if statements with the following:

Code: [Select]
local inst = mc.mcGetInstance()
local SelectedTool = mc.mcToolGetSelected(inst)
local CurrentTool = mc.mcToolGetCurrent(inst)

local SigTable = {[1] = mc.OSIG_OUTPUT2, [2] = mc.OSIG_OUTPUT3, [3] = mc.OSIG_OUTPUT4, [4] = mc.OSIG_OUTPUT5}

local TsigSout = mc.mcSignalGetHandle(inst, SigTable[SelectedTool])
local TsigCout = mc.mcSignalGetHandle(inst, SigTable[CurrentTool])

All this does is look up the signal in the table based on the selected tool or current tool
Title: Re: lua in Mach 4
Post by: alanbredbury on August 12, 2022, 10:26:33 AM
That is so far from any of my prior programming experience that it is nearly incomprehensible. I do see what it is doing though I think. Thanks I will try to incorporate it into the code.
Here is another question. the code I have does one really irritating thing if I hit the stop button it drops the tool and raises and drops the tool change station. I assume its completing the code after the stop is pressed. I am willing to bet someone here knows how to fix that. It also changes the tool number even though it never happened kinda. Thoughts? everyone here has been a great help.
Title: Re: lua in Mach 4
Post by: SwiftyJ on August 13, 2022, 04:58:56 AM
To stop it continuing if you press stop you need to check the return code of mc.mcCntlGcodeExecuteWait each time you call it. If you look up the mc.CntlGCodeExecuteWait in the Mach4API.chm file in the docs folder you will see it returns four possible errors one of those being "no error". So you can do something like the following to exit the M6 macro if something happens during execution. The CntlMacroStop call forces the control into idle

Code: [Select]
local rc
rc = mc.mcCntlGcodeExecuteWait(inst, "some gcode string")
if (rc ~= mc.MERROR_NOERROR) then
     mc.mcCntlMacroStop(inst, 6, "Error moving to tool change position")  --The 6 can be replaced with any number, it is just an error number you want to assign. Also custom message can be used
     return --exit the m6 macro
end

Ideally you should check the return codes of when you turn on outputs as well in the same way. Otherwise the activation of an output may fail, such as the draw bar opening, but the machine will continue to move to the next tool position.
Title: Re: lua in Mach 4
Post by: alanbredbury on August 18, 2022, 02:45:14 PM
got back to this, the code that you provided below just dumps me out of the macro when I paste it in to my M6 macro. Please keep in mind I do not want to become an expert lua programmer I just want a single usable piece of code to run my tool changer. first question do I need to put this code in my program every time I call a gcode string and every time I change an output? this seems really bulky and inefficient. I tried putting the  mc.mcSignalSetState(hsig, 1) in this function where it says "some gcode string" yup that just dumps me out of the program and un-clamps the drawbar. I am clearly not getting this language. thoughts? examples? Thanks

local rc
rc = mc.mcCntlGcodeExecuteWait(inst, "some gcode string")
if (rc ~= mc.MERROR_NOERROR) then
     mc.mcCntlMacroStop(inst, 6, "Error moving to tool change position")  --The 6 can be replaced with any number, it is just an error number you want to assign. Also custom message can be used
     return --exit the m6 macro
end
Title: Re: lua in Mach 4
Post by: alanbredbury on August 18, 2022, 03:11:55 PM
Did a bit more searching and I think these guys are going to do what I need once I get the usage through my thick skull. Me smart like tractor.

These next two functions are very helpful inside lua macros (scripts) since they will stop the machine if an error condition occurs (provided you check for and handle that error condition). Either of these functions will stop the execution of the GCode after the macro ends, but will not halt the execution of the macro itself. You will need to put "return" statements in after the calling of these functions, in order to halt the macro itself.
mcCntlMacroAlarm(...)
number: rc = mc.mcCntlMacroAlarm(number: inst, number: error_number, string: error_message)

mcCntlMacroStop(...)
    number: rc = mc.mcCntlMacroStop(number: inst, number: error_number, string: error_message)
Title: Re: lua in Mach 4
Post by: joeaverage on August 18, 2022, 04:55:51 PM
Hi,
every API is a function, and every function has at least one result, the return code, and maybe other results of the function.

There is a list of return codes here:

https://www.machsupport.com/forum/index.php?topic=40051.0 (https://www.machsupport.com/forum/index.php?topic=40051.0)

The software test you are applying asks 'Is there an error in the function', if so stop the macro otherwise continue. That is a pretty crude test, there are many reasons that
a function can fail, one of the more common is:

-18     MERROR_NOT_NOW 

This means that for whatever reason Mach cannot execute the macro, and that in turn usually results because the screenscript is not executing at that particular moment. If you were to wait,
maybe a few milliseconds and try again then the function would proceed.

There are a few circumstances this error is common. They are characterised by control of the machine as it passes from the main Gcode interpreter to the screenscript,
and an m6 is a prime example. So in this circumstance it may be necessary to use another software test, and even devise a strategy for repeating the API call if it fails.
You may be concerned that ' this seems really bulky and inefficient.', and this further testing makes it worse....but you potentially can save the macro from faulting.

A great deal of effort and expertise in software development is less about 'normal and correct' operation but what happens in a fault condition? Having software to trap an error and ideally
correct it automatically, is the holy grail. You'd certainly hope nuclear launch facilities have robust error trapping routines for example, LOL.

Quote
Please keep in mind I do not want to become an expert lua programmer I just want a single usable piece of code to run my tool changer.

You may not want to become an expert and I imagine you'll get your wish, but you will have to be proficient, and that's nearly as tough.
The alternative is to pay NFS to write it for you,and they will...but at a cost. The expectation that you can write code to enact a tool changer and that it be
as easy as falling off a log is unrealistic.

Craig
Title: Re: lua in Mach 4
Post by: KatzYaakov on August 19, 2022, 02:35:32 AM
i must agree with Craig spechieli in such cycle like change tool
that involve movement ,output comand,input check...
and there is big rule you must put on yours mnd " the unexpected behavior always will happened"
you must think in every movement what can be fault , not only by machine but also with worker ,
remember very important  point ( for me it was big headache)if you press stop while m function run ,Mach will stop
only movement ,all other continue run
in other word if you press stop while change tool ,the machine will stop move ,but the spindle will open and tool will fall and beake
so this one sample  why you must think about every option in every point of time in yours cycle


Title: Re: lua in Mach 4
Post by: SwiftyJ on August 19, 2022, 07:45:12 AM
Did a bit more searching and I think these guys are going to do what I need once I get the usage through my thick skull. Me smart like tractor.

These next two functions are very helpful inside lua macros (scripts) since they will stop the machine if an error condition occurs (provided you check for and handle that error condition). Either of these functions will stop the execution of the GCode after the macro ends, but will not halt the execution of the macro itself. You will need to put "return" statements in after the calling of these functions, in order to halt the macro itself.
mcCntlMacroAlarm(...)
number: rc = mc.mcCntlMacroAlarm(number: inst, number: error_number, string: error_message)

mcCntlMacroStop(...)
    number: rc = mc.mcCntlMacroStop(number: inst, number: error_number, string: error_message)

I used mcCntlMacroStop in the example I gave you. It sounds like you were trying to use another API call where I put "some gcode string". In the script you posted, you created a gcode string using the string.format and assigned it to the variable Gcode and then use used it in this API call - mc.mcCntlGcodeExecuteWait(inst, GCode). It is here where you would check the return code as I showed in my example.
Title: Re: lua in Mach 4
Post by: alanbredbury on August 22, 2022, 09:21:53 AM
hey you know what would have been really handy is a link to this topic. pretty much what I have been struggling with so anyone following this for a solution try the one below. looks like it will do the trick. I learn best from examples.

https://www.machsupport.com/forum/index.php?topic=36577.msg251136#msg251136
Title: Re: lua in Mach 4
Post by: SwiftyJ on August 22, 2022, 01:47:00 PM
That is just the same as the example I posted earlier in this thread, in fact the link you posted is sort of out of date now as there are better ways.  Oh well..