HosseinAgha
Hello everyone

I want to create a custom function block that allows the user to set the number of inputs in the software environment favorably.

For example, the ADD function is available in the software by default, and the user can set any number of inputs for it.

I also want to create such a function.

Please guide me if anyone knows how to do this.
Reply 0 0
r2k-in-the-vortex
Structured text syntax for that is something like that:
    {external}
    FUNCTION MAX<T : ANY> : T
        VAR_INPUT
            args : {sized} T...;
        END_VAR
    END_FUNCTION


With rusty you can do that and then do the implementation externally. But openplc uses matiec, which doesn't quite work that way. Matiec is translator of iec to c, so just make sure there is a matching c funtion declaration when you call smth(a, b, c, d); from st and that implementation can be linked to, matiec shouldnt't know or care that there is no such declaration in ST.

Maybe something like this?

{
#include <stdarg.h>
double average(int num,...) {
   va_list valist;
   double sum = 0.0;
   int i;
   /* initialize valist for num number of arguments */
   va_start(valist, num);
   /* access all the arguments assigned to valist */
   for (i = 0; i < num; i++) {
      sum += va_arg(valist, int);
   }
  
   /* clean memory reserved for valist */
   va_end(valist);
   return sum/num;
}
}
SomeLrealVar := average(1.1, 1.2, 1.3);
Reply 0 0
HosseinAgha
Thank you

I want to create a permanent block in the editor and my purpose is not to create a block for a specific project. 
So I need to create an XML file for it and specify the inputs and outputs of the block in it. While as far as I understand, it is not possible to add such a thing in the XML file, and for this, a CSV file like the existing CSV file for Standard Functions must be created, which has its own complications. Please guide me which method should I adopt.

Thank you very much!
Reply 0 0
r2k-in-the-vortex

you add c extension, do this

/////////////////////////////// globals section ////////////////////////////////
int average(int a, ...){
    return 7;
}


and then in pou-s you can do this
{average(1);}
{average(1, 2);}
{average(1, 2, 3);}

Reply 0 0
thiagoralves
I think what @HosseinAgha wants to do is more like an editor block you can reuse anywhere. You can follow these steps to create your own editor block. It is not easy, but it works:  https://openplc.discussion.community/post/how-to-add-my-fbd-to-default-pouslibrary-10408620?pid=1310581219

As for variable inputs, that will be very tricky. I don't know if there is an easy way to implement that.
Reply 0 0
HosseinAgha
Thank you Thiago!

I have tried this method that you have mentioned long ago and there is no problem in implementing it.

But as I said, it is important for me that my function block inputs can be changed just like standard functions.
with the difference that in addition to variable inputs, it also has fixed inputs and its outputs are not only one and can be expanded.

I am well aware that this cannot be easily done and I request that if you know the details of the "tricky" method you mentioned, please let me know.

Thank you very much
Reply 0 0
Reply