How to "communicate" between macro & plugin/macroloop

This is where you talk about Macros, show examples of your macro scripting and SHARE handy segments of script code as examples.

How to "communicate" between macro & plugin/macroloop

Postby beefy » Tue May 26, 2020 2:25 pm

Let's say you have a plugin/macroloop running and when a macro is run in gcode, it changes the operation of the plugin/macroloop.

So the gcode macro would have to set a "flag" that the plugin/macroloop is monitoring, or visa versa.

Any idea what is the best way of "communicating" like this between a macro & plugin/macroloop ??? A screen LED, or internal variables e.g. Getvar() and Setvar().

And would using screen elements like an LED as a on/off flag, be a slow method, or is an LED written and read fast internally.

Cheers,

Keith
beefy
 
Posts: 404
Joined: Mon Sep 05, 2016 10:34 am

Re: How to "communicate" between macro & plugin/macroloop

Postby dezsoe » Tue May 26, 2020 7:11 pm

The easy way is when a macro or macroloop has to talk to a plugin: you can use the Informplugin call. With Informplugin you can send and receive data. To see how it works, here is one of my test macros which calls the On-Screen keyboard plugin:

Code: Select all
object Returnvalue;

double? result;
string mystring;

// test0 - check if plugin is enabled

Returnvalue = exec.Informplugin("UCCNC_OSK.dll", (object)null);

bool isrunning = false;

if (Returnvalue is bool) isrunning = (bool)Returnvalue;
if (!isrunning)
{
  exec.AddStatusmessage("On-Screen Keyboard plugin not enabled!");
  return;
}

// test1 - numeric input with default value

Returnvalue = exec.Informplugin("UCCNC_OSK.dll", (object)"calc:1.23");

result = null;

if (Returnvalue is double?)
{
  result = (double?)Returnvalue;
  if (result != null)
    exec.AddStatusmessage("Result: " + result.ToString());
  else
    exec.AddStatusmessage("Result: none");
}
else
    exec.AddStatusmessage("Result: not double?");

// test2 - numeric input

Returnvalue = exec.Informplugin("UCCNC_OSK.dll", (object)"calc");

result = null;

if (Returnvalue is double?)
{
  result = (double?)Returnvalue;
  if (result != null)
    exec.AddStatusmessage("Result: " + result.ToString());
  else
    exec.AddStatusmessage("Result: none");
}
else
    exec.AddStatusmessage("Result: not double?");

// test3 - string input with default value

Returnvalue = exec.Informplugin("UCCNC_OSK.dll", (object)"text:blabla");

mystring = "";

if (Returnvalue is string)
{
  mystring = (string)Returnvalue;
  exec.AddStatusmessage("Result: '" + mystring + "'");
}
else
    exec.AddStatusmessage("Result: not string");

// test4 - string input

Returnvalue = exec.Informplugin("UCCNC_OSK.dll", (object)"text");

mystring = "";

if (Returnvalue is string)
{
  mystring = (string)Returnvalue;
  exec.AddStatusmessage("Result: '" + mystring + "'");
}
else
    exec.AddStatusmessage("Result: not string");

If you have to connect macros and/or macroloops then I advice using a plugin to communicate between them. You could skip the plugin and use LEDs and #variables (or even the Modbus data array), but if you run codes that are not made by you then there is the possibility to use the same variables. Also, with screensets and plugins you may use the same LEDs, fields, variables.
dezsoe
 
Posts: 1146
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: How to "communicate" between macro & plugin/macroloop

Postby beefy » Wed May 27, 2020 12:17 am

Thanks very much Dezsoe.

So it looks like the macro can call the plugin event to pass info to the plugin and have the plugin immediately return some info.

BUT.......going the other way around, when the plugin wants to inform the macro about something, it can't do that, i.e the macro must continually "poll" the plugin (via the Informplugin call), asking it if a certain condition in the plugin has happened.

Example:

Macro sends info to plugin to start doing something.
Macro is now waiting for plugin to finish this task, so keeps asking (polling) plugin if it has finished that task.

Does that make sense ?

Keith
beefy
 
Posts: 404
Joined: Mon Sep 05, 2016 10:34 am

Re: How to "communicate" between macro & plugin/macroloop

Postby dezsoe » Wed May 27, 2020 9:12 am

Hello Keith,

Yes, you have to poll the plugin. You also have to know that there are some special cases. If you call Informplugin then the macro will wait for the job to complete. Also, if you make a plugin which executes something when a button is pressed (Buttonpress_event) then the same happens if you call exec.Callbutton. In these cases you don't have to program anything, the execution comes back when the plugin finished its job. (If you have to call Code, CodeSync or Codelist in the plugin then use the button event, because Informplugin will not do these.)

The other case is if you start a new thread in the plugin to do a longer job. You can start it and then you can check a flag if it is finished. The flag can be checked by an Informplugin call, but it can be a buttons state or an LED too. (Also start it with button event if it contains gcode!) Here is a sample for both from the same macro:

Code: Select all
exec.Callbutton(848);                                                           // Goto probe pos
if (exec.Ismacrostopped()) return;

exec.Callbutton(821);                                                           // Start probing
while (!AS3.Getbuttonstate(821) && !exec.Ismacrostopped()) Thread.Sleep(10);
while (AS3.Getbuttonstate(821) && !exec.Ismacrostopped()) Thread.Sleep(10);
exec.Wait(500);
if (exec.Ismacrostopped()) return;

First I send the machine to the probe position: the Callbutton will return only if the movement is finished or stopped. Then I press the start probing button and first I wait for the button to turn on then to turn off. This is because the probing runs in a separate thread which is started by the start probing button and the state of the buttons shows if it is still running.
dezsoe
 
Posts: 1146
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: How to "communicate" between macro & plugin/macroloop

Postby beefy » Wed May 27, 2020 12:02 pm

Cheers Dezsoe,

fantastic information for us amateurs.

It never crossed my mind about a macro button activation also causing the button press event in the plugin to trigger. That little trick could come in handy some time, so thanks for that tip.
I'll have to have another look through all the available events in a plugin and see if there's anything else I could use like that.

When you say:
If you call Informplugin then the macro will wait for the job to complete.

Do you mean the macro will wait for the return value from the Informplugin_event() in the plugin ?

Keith
beefy
 
Posts: 404
Joined: Mon Sep 05, 2016 10:34 am

Re: How to "communicate" between macro & plugin/macroloop

Postby dezsoe » Wed May 27, 2020 12:15 pm

Hi Keith,

Yes. Try to run the first macro I posted here: it will show the result of the OSK to the status box, so it has to wait for the plugin. You can also call the OSK plugin from another plugin with the same parameters. You have to check the type of the result, because it is an object. If the type is what you need then you can cast it to the needed type and use the result. All of my plugins are written to return bool true if the Informplugin is called with a null parameter, this way you can check from a macro or other plugin if the called plugin is enabled and running. That is the first call in the sample macro. The others return double for the numeric input and string for the text input.
dezsoe
 
Posts: 1146
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: How to "communicate" between macro & plugin/macroloop

Postby beefy » Wed May 27, 2020 11:10 pm

I ran your example and noted how the status box only got another update only after I closed each window of the OSK.

You've taught me a lot of new stuff in this one thread Dezsoe. I think I'm going to have to learn a little about passing objects, as I've had nothing to do with that part of C#, but your example helps a lot.

A big thanks again Dezsoe. You've took me from LEDs and Vars, to the Informplugin() call and object passing :D

Keith
beefy
 
Posts: 404
Joined: Mon Sep 05, 2016 10:34 am

Re: How to "communicate" between macro & plugin/macroloop

Postby dezsoe » Thu May 28, 2020 7:54 am

Life is learning... It was only a few days ago when I realized that the Callbutton function waits for all plugins to do their events. (I was working on the macro which has the second sample code in it.) :)
dezsoe
 
Posts: 1146
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: How to "communicate" between macro & plugin/macroloop

Postby Vmax549 » Thu May 28, 2020 1:43 pm

You also have to learn when UCCNC will and will not let you run a macro/macroloop/plugin. There are restrictons built in tha can prevent functions from running.

Just a thought , Been There (;-) TP
Vmax549
 
Posts: 285
Joined: Sun Nov 22, 2015 3:25 am
Location: USA

Re: How to "communicate" between macro & plugin/macroloop

Postby beefy » Fri May 29, 2020 12:10 am

Hi Dezsoe,

I suppose it makes some sense, and I could think of it as plugin events are just like interrupts in a microcontroller. They must complete and return before functions can run.

I know it's not quite the same but I'll try and think of it that way to remember.

Regarding what Terry said, do you have any idea what would cause UCCNC to not run a macro/macroloop/plugin. That's a new one to me.

Keith
beefy
 
Posts: 404
Joined: Mon Sep 05, 2016 10:34 am

Next

Return to Macros

Who is online

Users browsing this forum: No registered users and 2 guests