0

Is there a way to declare a undefined length string as a constructor or a work around?

I need to write a log file for a function block. The file name needs to be declared in the FB declaration but there are compiler errors which don't allow me to use strings as constructors.

FUNCTION_BLOCK FB_LogFile
VAR
    sfileName : REFERENCE TO T_MaxString;
END_VAR

METHOD FB_init : BOOL
VAR_INPUT
    bFileName: REFERENCE TO T_MaxString;
END_VAR    
THIS^.sfileName:=sFileName;
PROGRAM Main
VAR
    LogTest:FB_LogFile(sFileName:='c:/config/config.csv';
END_VAR
LogTest();
Steve
  • 963
  • 5
  • 16
  • 1
    What error are you getting and what is your code? – Roald Aug 29 '22 at 11:07
  • `FUNCTION_BLOCK FB_LogFile VAR sfileName : REFERECE TO T_MaxString; END_VAR METHOD FB_init : BOOL VAR_INPUT bFileName: REFERENCE TO T_MaxString; END_VAR THIS^.sfileName:=sFileName; PROGRAM Main VAR LogTest:FB_LogFile(sFileName:='c:/config/config.csv'; END_VAR LogTest();` https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/36028799547735179.html&id= – Chris Smith Aug 30 '22 at 14:49
  • Can you please add it to your original question? Now it is very hard to read. – Roald Aug 30 '22 at 18:18

1 Answers1

1

To answer the question you asked, If you want to pass in a string of an undefined length then the generally accepted method is using a pointer to the dataspace occupied by the string, and the length of the string. Specifically:

VAR_INPUT
    pString : POINTER TO BYTE; // Pointer to the first char of a string
    nString : UDINT; // Length of the string
END_VAR

In regard to other points raised by your code, I believe the error you are specifically referring to is due to your reference handling. When using references it is necessary to understand that they handle dereferencing differently to pointers. To quote InfoSys:

refA REF= stA; // represents => refA := ADR(stA);  
refB REF= stB1; // represents => refB := ADR(stB1);
refA := refB; // represents => refA^ := refB^; 
(value assignment of refB as refA and refB are implicitly dereferenced) 
refB := stB2;    // represents => refB^ := stB2;
(value assignment of stB2 as refB is implicitly dereferenced)

This applies to FB_Init as well so your code should actually read:

FUNCTION_BLOCK FB_LogFile
VAR
    rFilePath: REFERENCE TO T_MaxString;
END_VAR

METHOD FB_init : BOOL
VAR_INPUT
    rLocalPath : REFERENCE TO T_MaxString;
END_VAR    
rFilePath REF= rLocalPath;
Steve
  • 963
  • 5
  • 16