Machsupport Forum

Mach Discussion => General Mach Discussion => Topic started by: moorea21 on April 26, 2015, 04:09:38 PM

Title: passing variable values from macro to macro , using parameters?
Post by: moorea21 on April 26, 2015, 04:09:38 PM
I want to be able to preserve the value of 'FileNum', (variable used in a macro), and access it from other macros after the macro that generated it is switched off, or use it in the same macro called again later in my Gcode. I tried this:-

Dim FileNum As Double

'Code arriving at a value for FileNum...

Param1() = FileNum

It tells me I have a type mismatch at the last line, which I don't understand, as the manual says that parameters are doubles, like my variable.

My variable is never more than 2 digits long, so doesn't have to be a double; I just thought it needed to be, in order to transfer its value to parameter1.

Am I not assigning the value of FileNum correctly to Param1()?
How can I pass values from one macro to another?

Thanks,

M21

Title: Re: passing variable values from macro to macro , using parameters?
Post by: ger21 on April 26, 2015, 04:16:56 PM
One way is to write the value to a user DRO, and read it from there.
Title: Re: passing variable values from macro to macro , using parameters?
Post by: moorea21 on April 26, 2015, 04:43:41 PM
That works a treat, Thanks.
Title: Re: passing variable values from macro to macro , using parameters?
Post by: BR549 on April 26, 2015, 05:24:46 PM
Param1() is a read only value.

Another way it to use the Gcode Vars 500-600

GetVar(500)
Setvar(500,22)

(;-) TP
Title: Re: passing variable values from macro to macro , using parameters?
Post by: moorea21 on April 27, 2015, 03:41:47 AM
Ok, ta. Manual wasnt too clear on params being read only.
Title: Re: passing variable values from macro to macro , using parameters?
Post by: stirling on April 27, 2015, 04:26:15 AM
Param1() is a FUNCTION and therefore is an rvalue (i.e. simply put, it can only appear on the right hand side of an expression).

e.g. x = Param1();
Title: Re: passing variable values from macro to macro , using parameters?
Post by: moorea21 on April 27, 2015, 02:36:09 PM
I've tried both UserDro and Var ideas...

Var loses its value after Mach is shut down and restated, UserDro keeps its value.

Is this how it's supposed to work? Really didn't expect UserDro to keep its value...
Title: Re: passing variable values from macro to macro , using parameters?
Post by: moorea21 on April 27, 2015, 03:01:44 PM
Although that appeared to be the case, I now find the following:-

It seems the value of Var is retained after Mach is shut down, but not when the PC itself is shut down, whereas UserDRO keeps its value in both cases.

I was hoping to set a variable to initialise at 0 any time Mach is started, and set to 1 after the first file has been loaded ( So another macro can tell if this is the first file loaded since Mach started up or not), but if both the above types retain their value after shutting down and restarting Mach, it won't work.

Is there a reliable way to clear the value of a Var or a UserDRO when Mach shuts down? Also, it would be useful to set up another 'flag' to show if reset has been pressed at any time since Mach was opened.

The macro (called from a Gcode test file) is this:-

Dim FirstFile As Double

FirstFile = GetVar(500)
Message FirstFile
If FirstFile <> 1 Then
   SetVar(500,1)
End If
Title: Re: passing variable values from macro to macro , using parameters?
Post by: ger21 on April 27, 2015, 03:28:52 PM
You can make a macro to set the USerDRO to 0, and call it from the Initialization string when you first start Mach3.
Then add another macro to all your g-code files, to set the USerDRO to 1 if it's 0.
Title: Re: passing variable values from macro to macro , using parameters?
Post by: moorea21 on April 27, 2015, 03:43:10 PM
That sounds good. It looks like I can also set another UserDRO to 1, and it can then be reset to 0 with a similar macro if Mach is reset, if I check 'Use init. on all resets', so hopefully both of these tasks should be do-able.

Is checking 'Use init on all resets' likely to have any unforeseen effects, if the initialisation string only contains the default string values, and a couple of calls to macros that reset the value of userDRO's or Vals? I don't want to find that this means something else won't work. Probably being over cautious now?

Thanks.
Title: Re: passing variable values from macro to macro , using parameters?
Post by: BR549 on April 27, 2015, 04:12:00 PM
#vars 500 -600 are persistance (saved to the XML) on shutdown. The value should be there when you start up again.
IF you want a VAR that clears back to NILL on shut down use any other VAR. On shut down it is NOT saved and will be NILL when you start up again.

IF you need the Nill Var to be populated on start then in the initiation string add the code

#300 = 1    That will set the var #300 to 1

(;-) TP



Title: Re: passing variable values from macro to macro , using parameters?
Post by: moorea21 on April 27, 2015, 05:01:09 PM
Thanks, that sounds simpler still.
Title: Re: passing variable values from macro to macro , using parameters?
Post by: BR549 on April 27, 2015, 05:08:35 PM
#vars 500 -600 are persistance (saved to the XML) on shutdown. The value should be there when you start up again.
IF you want a VAR that clears back to NILL on shut down use any other VAR. On shut down it is NOT saved and will be NILL when you start up again.

IF you need the Nill Var to be populated on start then in the initiation string add the code

#300 = 1    That will set the var #300 to 1

(;-) TP



Title: Re: passing variable values from macro to macro , using parameters?
Post by: Overloaded on April 27, 2015, 05:38:35 PM
Terry, you're stuttering. :)
Title: Re: passing variable values from macro to macro , using parameters?
Post by: ozwes007 on April 27, 2015, 06:17:38 PM
Just as a heads up, if you declare a variable as a global or Public (Static x as Double or Public x as Double)it will exist outside of a procedure(macro), any variable declared (Dim x as Double) inside a sub will always only exist within the sub. This is Visual Basic std operating procedure, so I would assume( is this going to make an ASS of U and ME ;-) ) it would stand true with Cypress as well.
Eg
' Your code here
Public x as double
' exists everywhere
Sub
' Your code here
Dim x as Double
' Only exists here
End Sub
Title: Re: passing variable values from macro to macro , using parameters?
Post by: BR549 on April 27, 2015, 08:01:04 PM
HUM,  I believe that even a public variable ceases to exist when you  end the script in CB.

(;-) TP
Title: Re: passing variable values from macro to macro , using parameters?
Post by: ozwes007 on April 27, 2015, 10:40:02 PM
Ouch. ;-) , I'll do some research.
Title: Re: passing variable values from macro to macro , using parameters?
Post by: ozwes007 on April 27, 2015, 11:04:14 PM
This is from the Cypress FAQ section
Can I reference variables in my applications? Cypress Enables sports a well designed powerful API that allows global variables to be referenced directly or to be set and the values retrieved as needed.

My understanding / interpretation on this would be that a Global  can be set then called later, however it shouldn't persist on shutdown unless the variable is stored in a DLL or exe variable. I.e. A DRO.