FB string and naming

2024-09-26
2024-10-02
  • andrebrandt - 2024-09-26

    Hi all.
    I have a FB written in ST.

    FUNCTION_BLOCK NTC10k
    VAR_INPUT
        Syst:STRING;
        In:REAL;
    END_VAR
    VAR_OUTPUT
        Out:REAL;
        OTag:STRING;
        Out_St:Struct_NTC10K;
    END_VAR
    
    VAR
        Tag:STRING;
        InstanceName: STRING;
        Structure:Struct_NTC10K;
    END_VAR
    

    What i'm trying to do, is to pass data to structure. In structure i want to add a tag with systemnumber and sensorname.
    '320.001-RT401'

    In pou i use this RT401: NTC10k;, and tried this RT401: NTC10k(Tag:='RT401');

    Anyone done this?

     
  • TimvH

    TimvH - 2024-09-28

    Really not clear what you are trying to do, but isn't a Struct enough?
    So add an object of the type DUT to the Application.
    Then create a structure

    TYPE ST_Sensor :
    STRUCT
        sName : STRING;
        uiNumber : UINT;
    END_STRUCT
    END_TYPE
    

    Then in you application add an instance of this Structure

    stSensor1 : ST_Sensor := (sName := 'my sensor', uiNumber := 1);
    

    or use it like this

    stSensor1.sName := 'my sensor';
    
     
    • andrebrandt - 2024-09-30

      Hi Tim.
      Not quite correct.
      What i'd like, is when you place the FB in prog, i choose a block NTC10k, and name this RT401.
      What i'm trying to do, is to get the name RT401 automaticly into the FB, so i can put this into the string i pass in a struct.

      So in var in POU, i saw this a place i could do this, RT401: NTC10k(Tag:='RT401'), but i can't get this to work.

      I shurly must have had something like this in FB:

      FUNCTION_BLOCK NTC10k String TAG

      But it's here where I'm stuck...

       
  • TimvH

    TimvH - 2024-09-30

    I see, you want to initialize the FB.
    To be able to initialise it like you described, you need to add the FB_Init method to your FB.
    (right click on the FB, select add object --> method).
    Then press the arrow down, to select the FB_Init (overwrite default implementation).

    In the VAR_INPUT section of this method, add the variable --> Tag : STRING;
    Then in the code section of this method add:
    THIS^.Tag := Tag; // copy initial value to local variable in FB

    Search Google if you want to know more about FB_Init.

    PS, reflection + instance path, is also an option if you want to get the full name of the instance (path) of the Function Block.
    See: https://content.helpme-codesys.com/en/CODESYS%20Development%20System/_cds_pragma_attribute_instance_path.html

     
    • andrebrandt - 2024-10-01

      Thank you so much. This worked.

      Waht im trying to do here is to automatically tag each sensors, valves and so on. There is a preatty nasty tag system i working on.

      "_320" is not alloved. I can have a tag "+4f=320.004-RT001V"
      In codesys I want to build the FB in folders.

      -4f
      |
      --320
      |
      --001
      This is the system structure.
      Inside here is all of the sensors and so on. But with Codesys, I cannot tag this like this.
      So what I'm trying to do now is to Make a folder for the building "4f" and one for the system "320", and a POU _001.
      Is there a way in init to get folder names?

       

Log in to post a comment.