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

Leading Zeros

davejc
2021-01-14
2021-01-22
  • davejc - 2021-01-14

    I am working on a project where I am trying to create a default string that is "RUNA#####", where ##### starts at "0001" and increments based on some other logic.

    I am able to make this work using the logic:
    GVLP.rBatchID := GVLP.rBatchID +1;
    GVLP.wsBatchID:= WConcat("RUNA",REAL_TO_WSTRING(GVLP.rBatchID));

    The problem I am having it getting the RealValue to always be 5 Digits.

    Has anyone tried to do this and/or have any tips what I can try to get this to work??

     
  • i-campbell

    i-campbell - 2021-01-14

    Why is your batchid a real, if you only have integer values?
    Anyway, for a REAL you can do:

    PROGRAM PLC_PRG
    VAR
        Format : WSTRING := "RUNA%05.0f";
        ID     : REAL    := 254.0;
        wsBatchID : WSTRING := "UNDEFINED";
    END_VAR
    
    //Requires StringUtils library
    StuSprintfW(
        wstFormat:= ADR(Format), 
        pVarAdr:= ADR(ID), 
        udiVarType:= __SYSTEM.TYPE_CLASS.TYPE_REAL, 
        pBuffer:= ADR(wsBatchID), 
        dwBufferSize:= SIZEOF(wsBatchID));
    
     
  • davejc - 2021-01-14

    "Why is your batchid a real, if you only have integer values?" because I don't necessarily know EXACTLY what I'm doing.

    How would that logic be modified if I were to use Integer rather than Real?

     
  • i-campbell

    i-campbell - 2021-01-14

    Ah ok. I guess a REAL value will still work as good, I just find it strange to use a REAL when I will never have any decimal places.
    Here is another one, which does exactly the same thing as the first.

    PROGRAM PLC_PRG_1
    VAR
        Format : WSTRING := "RUNA%05d";
        ID     : UDINT    := 254;
        wsBatchID : WSTRING := "UNDEFINED";
    END_VAR
    
    //Requires StringUtils library
    StuSprintfW(
        wstFormat:= ADR(Format), 
        pVarAdr:= ADR(ID), 
        udiVarType:= __SYSTEM.TYPE_CLASS.TYPE_UDINT, 
        pBuffer:= ADR(wsBatchID), 
        dwBufferSize:= SIZEOF(wsBatchID));
    
     
  • davejc - 2021-01-14

    I have to be missing something here. I am unsure what output I should be getting from the StuSprintfW function.

     
    • i-campbell

      i-campbell - 2021-01-14

      Are you in simulation mode? This only works when not in Simulation mode.
      You can use the Soft PLC that comes with the IDE installer. Just start it from the Systray.

       
  • davejc - 2021-01-14

    I may not be following what youre trying to explain and I apologize if that is the case. What I have found is...

    if I create a text block on my VISU and set the text to "RUNA%05d" then set the text variable to "GVLP.iBatchID", this outputs exactly what I need "RUNA0001, RUNA0002, etc.". I just don't know to get that string into a Variable that I can use elsewhere. It would be great if I could use the concat and somehow tell it to use the format %05d for the Integer when concat'ing.

     
  • davejc - 2021-01-22

    So I've had some more thoughts on this and I am wondering if commands exist that will let me do this....

    Take the iBatchID Integer and concat together "0000" and iBatchID to get "0000###". Then is there a command where I can take just the 5 characters from the Right of this? Thus removing the excess leading 0's? Then I can use another concat to put "RUNA" in front of it.

    Obviously I know concat is there, but is there a command that will allow me to just select the 5 characters from right to left ??

     
  • davejc - 2021-01-22

    Got it. Just incase any one else comes across this....

    GVLP.iBatchID := GVLP.iBatchID + 1;
    GVLP.wsBatchID := WConcat("0000", INT_TO_WSTRING(GVLP.iBatchID));
    GVLP.wsBatchID := WRight(GVLP.wsBatchID,5);
    GVLP.wsBatchID := WConcat("RUNA",GVLP.wsBatchID);

     
    πŸ‘
    1

Log in to post a comment.