Hello Guest it is May 16, 2021, 09:03:37 AM

Author Topic: M Code Parameters  (Read 1518 times)

0 Members and 1 Guest are viewing this topic.

M Code Parameters
« on: May 28, 2016, 02:48:05 PM »
Can any one explain why this is happening?  In the code below, it always jumps to ....

mc.mcCntlSetLastError(inst, 'Output out of range' .. 'A' .. x1 .. 'B' .. x2)

Yet x1 does equal 1.  I've tried converting to and from string and number and I either get the same result or M4 just crashes when the M code is called.  The code below runs, but it doesn't recognize that x1 == '1'.  If I change x1 == '1' to x1 == 1, M4 crashes.   mc.mcCntlGetLocalVar is retrieving the correct parameters.  Just can't seem to use them.


Code: [Select]
   if (args ~= nil) then
        x1  = mc.mcCntlGetLocalVar(inst, args, mc.SV_A)
        x2  = mc.mcCntlGetLocalVar(inst, args, mc.SV_B)
        if (x1 == '1') then
            if (x2 == '1') then
                mc.mcSignalSetState(mc.OSIG_OUTPUT1, true)
            else
                mc.mcSignalSetState(mc.OSIG_OUTPUT1, false)
            end    
        else
            mc.mcCntlSetLastError(inst, 'Output out of range' .. 'A' .. x1 .. 'B' .. x2)
        end
    else
        mc.mcCntlSetLastError(inst, 'No Arguments Found')
    end
« Last Edit: May 28, 2016, 02:57:50 PM by rrc1962 »

Offline DazTheGas

*
  •  753 753
  • DazTheGas
    • View Profile
Re: M Code Parameters
« Reply #1 on: May 28, 2016, 03:36:26 PM »
Can you elaborate a bit as to what is your gcode is passing as the args?

DazTheGas
Theres always time for a choccy biccy
Re: M Code Parameters
« Reply #2 on: May 28, 2016, 05:13:44 PM »
When I run the M code  "M162 A1 B1", it displays "Output out of rangeA1B1".  A1 clearly = 1, so...

if (x1 == '1') then

Should evaluate true, but it's not.

If I call M162 A2 B3, it displays "Output out of rangeA2B3".  I would expect it to do this since x1 does not equal 1, but it demonstrates that it is seeing the parameters being passed to the macro.

What data type does mc.mcCntlGetLocalVar return?  Maybe it just mismatched data type.  I know a string 1 does not equal a number 1.  I tried converting x1 to a string with the same results.
Re: M Code Parameters
« Reply #3 on: May 28, 2016, 06:34:46 PM »
Finally got it.  The return data type is a number, which is why 1 didn't equal 1.  The other issue was this....

mc.mcSignalSetState(mc.OSIG_OUTPUT1, true)

I thought that when I changed the if statement to evaluate 1 instead of '1', it was crashing M4.  What it was doing was finally working correctly.  mc.mcSignalSetState needs the handle to the output, not the output.  That was crashing M4.  Don't know how I missed that one.  I've written plenty of macros to manipulate outputs.

Here's the code.  When you call this M code, like M162 A3 B1, it turns on output 3.  M162 A3 B0 would turn it off.  I only use outputs 0 - 20 but the table could be expanded to include all of them.

Code: [Select]
function m162(args)
    inst = mc.mcGetInstance()
    ---------------------------------------------------------------
    -- A = OutputNumber, B = OutputState
    ---------------------------------------------------------------
       OutputTable = {
        [0] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0),
        [1] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1),
        [2] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2),
        [3] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3),
        [4] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4),
        [5] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5),
        [6] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6),
        [7] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7),
        [8] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8),
        [9] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9),
        [10] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10),
        [11] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT11),
        [12] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT12),
        [13] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT13),
        [14] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT14),
        [15] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT15),
        [16] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT16),
        [17] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT17),
        [18] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT18),
        [19] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT19),
        [20] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT20)
        }
    if (args ~= nil) then
        OutputNumber = mc.mcCntlGetLocalVar(inst, args, mc.SV_A)
        OutputState = mc.mcCntlGetLocalVar(inst, args, mc.SV_B)
        if (OutputState == 1) then
            mc.mcSignalSetState(OutputTable[OutputNumber], true)
        else
            mc.mcSignalSetState(OutputTable[OutputNumber], false)
        end
    else
        mc.mcCntlSetLastError(inst, 'No Arguments Found')
    end
end

if (mc.mcInEditor() == 1) then
    m162(nil)
end

Offline DazTheGas

*
  •  753 753
  • DazTheGas
    • View Profile
Re: M Code Parameters
« Reply #4 on: May 28, 2016, 06:43:31 PM »
Quote
Finally got it.  The return data type is a number, which is why 1 didn't equal 1.

Best place to look is in the api docs thats in the docs directory, all commands are there and tell if you need int or string

DazTheGas
Theres always time for a choccy biccy