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

Question about struct arrays

epinikion
2020-04-21
2020-04-23
  • epinikion

    epinikion - 2020-04-21

    Hi,

    I made a FB which has as input several string variables. Inside the FB I declare a variable consisting of a struct array of said inputs (this struct is made just of strings). These are later formated in a specific way and saved in a .txt file.

    My problem is when I go online, even I see the desired strings on the input variables, on the array theyΒ΄re all empty and my text file comes out as just comas and line breaks (which I write on my FB). It only works if I manually write the strings on the array.

    HereΒ΄s the DUT, just some strings:

    TYPE TestData :
    STRUCT
        index:                  STRING:='';
    
        data:                   STRING:='';
    
    
    
    
    
    END_STRUCT
    END_TYPEde
    

    And here a snippet of the variables on my FB:

    VAR_INPUT
    
        sTagOben:                       STRING;
        sTagUnten:                      STRING;
        sSerialNummer:                  STRING;
        sAssemblyName:                  STRING;
        sTesterType:                    STRING;
        sTesterID:                      STRING;
        sTestName:                      STRING;
        sTestResult:                    STRING;
        dDate:                          DATE;
        todTime:                        TIME_OF_DAY;
    
    
    END_VAR
    
    VAR
    
    Daten:                          ARRAY[0..7] OF TestData := [(index:='2',data:=sSerialNummer),(index:='4',data:=sAssemblyName),(index:='6',data:=sTesterType),(index:='7',data:=sTesterID),( index:='8',data:=sTestName),(index:='13',data:=DATE_TO_STRING(dDate)),(index:='14',data:=TOD_TO_STRING(todTime)),(index:='16',data:=sTestResult)];
    

    The index shows properly on my test file, as I defined them there manually. Any suggestions? Is it not possible to define inputs and right away call them in a defined variable?

     

    Last edit: epinikion 2020-04-21
  • Lo5tNet - 2020-04-21

    The Daten array gets initialized before your input variables are set. You need somewhere to update the Daten array "data" to match the input variables that are defined in your function block call or initialization. If you want to set them on intialization you might want to look into setting up a fb_init method for that function block.
    More on fb_init can be found here: https://help.codesys.com/webapp/_cds_method_fb_init_fb_reinit;product=codesys;version=3.5.15.0#interface-of-method-fb-init

     
    πŸ‘
    2
    • epinikion

      epinikion - 2020-04-22

      Thank you for your answer, I will take a look. Is it possible to define the variable in the program instead of in the variable definitions in order to spare me the fb_init? I think not, right?

       
  • Lo5tNet - 2020-04-22

    You can try using VAR_TEMP as that will assign the values every time your function block is called. So as long as your input variables have been set, Daten will update on your next call to your FB. You won't be able to see those values during runtime as once the function block has finished it's call it will destroy the variable. Otherwise I'm not sure what your end goal is it is harder to make suggestions.

    //You can try moving your var here
    VAR_TEMP
        Daten:                          ARRAY[0..7] OF TestData := [(index:='2',data:=sSerialNummer),(index:='4',data:=sAssemblyName),(index:='6',data:=sTesterType),(index:='7',data:=sTesterID),( index:='8',data:=sTestName),(index:='13',data:=DATE_TO_STRING(dDate)),(index:='14',data:=TOD_TO_STRING(todTime)),(index:='16',data:=sTestResult)];
    END_VAR
    
     
    πŸ‘
    1
    • epinikion

      epinikion - 2020-04-23

      Great, that works too! Many thanks.

       

Log in to post a comment.