Hello Guest it is April 29, 2024, 01:46:25 AM

Author Topic: Making use of P values in macros  (Read 282 times)

0 Members and 1 Guest are viewing this topic.

Making use of P values in macros
« on: July 27, 2023, 06:46:24 AM »
Hi,

Mach4/Lua/Gcode beginner here. I'm trying to write a macro for Mach4Hobby, that I've had a look at the exemplary m162.mcs in Profiles/Mach4Mill/Macros/ folder and I came across this in line 10
Code: [Select]
function m162(hParam) --The param is a P value called from Gcode. M162P3 for example.
which leads me to believe that I can specify parameters that are passed to my macros with a "P", following my M-code. Now I've written the following for M100 into m100.mcs:
Code: [Select]
local inst = mc.mcGetInstance()

function m100(hParam)
if (hParam ~= nil) then
mc.mcCntlLog(inst, string.format("hParam %d", hParam), "",0)
end
end

if (mc.mcInEditor() == 1) then
    m100(1)
end
From what I inferred from m162.mcs, if I add a line "M100P10" to my MDI, the called macro should get a numeric 10 as an argument and this should show up in the log. However, when I run it, the output is always "hParam 466550640", no matter what value I add as a "P value", even if I don't pass a parameter at all (i.e., just putting "M100").

There is apparently an argument passed to the macro, but it seems like is contains the wrong value, or some handle/reference or whatever. Why is that? Is this not how it's supposed to be used? If not, what is a P value and how can I get a parameter into my macro from G-code/MDI? Unfortunately, I cannot find any documentation on that, but maybe I just missed it?

(Using Mach4Hobby 4.2.0.5036)
Re: Making use of P values in macros
« Reply #1 on: July 27, 2023, 09:40:49 AM »
Was struggling for some time now... shortly after asking here I got it: I am however not able to delete or edit, so I will post the solution here:

I did not expect you have to look up the P-value. The solution is actually in the remainder of the m162.mcs code, I just didn't realize what this part of the script was doing. So, for anyone reading this, facing the same question, here's how to retrieve the P-value passed to an M-code, works with the format "M100P10" in the MDI:

Code: [Select]
local inst = mc.mcGetInstance()
function m100(hParam)
  if (hParam ~= nil) then
    local varP = 0
    local flagP, rc = mc.mcCntlGetLocalVarFlag(inst, hParam, mc.SV_P)
    if (flagP == 1) then --Check that the flag has been set so we do not get an unexpected value for mc.SV_P
      varP = mc.mcCntlGetLocalVar(inst, hParam, mc.SV_P) --Request P value

      mc.mcCntlLog(inst, string.format("hParam %i", varP), "",0) --do whatever you like with the value (e.g., logging it ;))

    end
  end
end