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

Equivalent to printf in ST

williamc
2013-10-23
2013-11-06
  • williamc - 2013-10-23

    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?

     
  • alex87 - 2013-10-27

    Use a simple UDP protocol.

    Alex

     
  • williamc - 2013-10-29

    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???

     
  • Andreaz - 2013-11-06

    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.

    FUNCTION FormatString : STRING
    VAR_INPUT
       Format: POINTER TO STRING;
       Value : POINTER TO STRING;
    END_VAR
    VAR
       CurResult: POINTER TO BYTE;
       CurValue : POINTER TO BYTE;
    END_VAR
    // Make a copy of the input string
    FormatString:= Format^;
    CurValue := Value;
    CurResult:= ADR(FormatString);
    WHILE (CurResult^ <> 0) AND (CurValue^ <> 0) DO
       
       // Found a placeholder (#)
       IF CurResult^ = 35 THEN
          CurResult^:= CurValue^;
          CurValue:= CurValue + 1;      
       END_IF;   
       
       CurResult:= CurResult + 1;
    END_WHILE
    

    Example:

    a:= 'Hello ####';
    b:= 'World';
    c:= FormatString(Adr(a), Adr(b));
    

    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.

     

Log in to post a comment.