Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Function block with an array of unknown size as a VAR_IN_OUT

Anonymous
2011-01-20
2017-03-07
  • Anonymous - 2011-01-20

    Originally created by: per@fluidtronic.com

    Hello
    I'm trying to write a function block (in ST) that shall do some operation on an array.
    Preferbly the size of the array should be defined by a VAR_INPUT

    Currently the variable declarations are as follows.
    VAR_INPUT
    Ellements:INT;
    NewVal:INT;
    END_VAR
    VAR_IN_OUT
    History: ARRAY [1.. 10] OF INT;
    END_VAR

    My intention is to replace the fixed size ARRAY[1..10] to [1..Ellements] e.g. a programatically
    changeable number of ellements in the array.
    Anyone ?

     
  • Andreaz - 2011-01-24

    use a integer pointer as input;

    VAR_INPUT
      Size:INT;
      Value:INT;
      History: POINTER TO INT;
    END_VAR
    VAR
      Item: POINTER TO INT;
    END_VAR
    ...
    Item:= Temp;
    FOR I:= 1 to Size do
      Temp^:= Value;
      Temp:= Temp + SIZEOF(Temp^);
    END_FOR;
    
     
  • jason.the.adams - 2011-01-24

    I've never thought to accomplish an array this way. Would you mind clearing up a few details?

    1. What is the purpose of the input variable, History?
    2. Where are you defining "temp" and is it another Pointer?
    3. If you wouldn't mind briefly explaining what's going on in your example, that'd be very helpful; it'd be great to really understand what's going on here.

    Thanks!

     
  • Anonymous - 2011-01-24

    Originally created by: per@fluidtronic.com

    Hello
    I've not tried the suggestion by Andreas yet, will do when time let me.

    The variable History is a set of samples of an input. I want to do some calculation on them
    and treat the array as a FIFO. All this is not a problem.

    The question is related to how to declear an input as an array of unknown size at compile time.
    All I've tried so far is not valid syntax

     
  • Andreaz - 2011-01-28

    jason.the.adams hat geschrieben:
    I've never thought to accomplish an array this way. Would you mind clearing up a few details?
    1. What is the purpose of the input variable, History?
    2. Where are you defining "temp" and is it another Pointer?
    3. If you wouldn't mind briefly explaining what's going on in your example, that'd be very helpful; it'd be great to really understand what's going on here.
    Thanks!

    #1: It's the same as the history in your example, a input array of values, but instead of just writing History as the input you write Adr(History[0])
    #2: My bad, all Temp should be Item
    #3: Basically my example just fills the input history with the value;

    VAR_INPUT
      Size:INT; (* Size of the array *)
      Value:INT; 
      History: POINTER TO INT; (* Same as History: ARRAY[0..Size-1] OF INT; *)
    END_VAR
    VAR
      Item: POINTER TO INT;
    END_VAR
    ...
    (* Just save a local copy of History[0] *) 
    Item:= History;
    FOR I:= 1 to Size do
      (* History[I]:= Value; *)
      Item^:= Value;
      (* Goto the next item in the array *)
      Item:= Item+ SIZEOF(Temp^);
    END_FOR;
    
     
  • hugo - 2011-01-31

    if you are interested you can find many array functions in the open source library OSCAT.
    many of the array functions use pointer to operate on unknown size of arrays.
    you can use our source code as sample to develop your own functions

     
  • gabriele-eureka - 2017-03-07
    VAR_INPUT
       NewVal:INT;
    END_VAR
    VAR_IN_OUT CONSTANT
       Ellements:INT;
    END_VAR
    VAR_IN_OUT
       History: ARRAY [1..Ellements] OF INT;
    END_VAR
    
     

Log in to post a comment.