There is no variant types in codesys so creating a real printf with placeholders like %.3f and so will be tricky.
I have made this function though that takes a input string and replaces all # characters with a value.
Note that this is written for CodeSys 3 so uses // as comments.
FUNCTIONFormatString:STRINGVAR_INPUTÂ Â Format:POINTERTOSTRING;Â Â Value:POINTERTOSTRING;END_VARVARÂ Â CurResult:POINTERTOBYTE;Â Â CurValue:POINTERTOBYTE;END_VAR//MakeacopyoftheinputstringFormatString:=Format^;CurValue:=Value;CurResult:=ADR(FormatString);WHILE(CurResult^<>0)AND(CurValue^<>0)DOÂ Â Â Â //Foundaplaceholder(#)Â Â IFCurResult^=35THENÂ Â Â Â CurResult^:=CurValue^;Â Â Â Â CurValue:=CurValue+1;Â Â Â Â Â Â END_IF;Â Â Â Â Â Â CurResult:=CurResult+1;END_WHILE
Note that if there are more # in the format string then the value it wont replace them with spaces. It would be an idea to add code for replacing the remaining, connected # with spaces.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi is there an equivalent to printf in Structured Text. I have to currently create strings by nested CONCAT statements.
Is there a better way?
Use a simple UDP protocol.
Alex
UDP - are you talking about User Datagram Protocol...can't quite see the relevance with writing several strings into a single string.
Could you elaborate???
There is no variant types in codesys so creating a real printf with placeholders like %.3f and so will be tricky.
I have made this function though that takes a input string and replaces all # characters with a value.
Note that this is written for CodeSys 3 so uses // as comments.
Example:
String c will contain "Hello World" after this.
Note that if there are more # in the format string then the value it wont replace them with spaces. It would be an idea to add code for replacing the remaining, connected # with spaces.