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

real number to Timestamp

Inna
2021-10-27
2021-10-28
  • Inna - 2021-10-27

    Hi,
    I have two real numbers that represent timestamp - see the attached screenshot. Is there codesys function or structure that converts 2 real numbers into appropriate timestamp in a resolution of mSec. If codesys has no build-in support for this, what is the better way to perform it in ST?

    Thanks

     
  • tvm - 2021-10-28

    What about this?

    TYPE TIMESTAMP_UNION :
    UNION
        As2RealS:       TIMESTAMPASREAL;
        AsTimeStamp:    TIMESTAMP;
        AsHex:          TIMESTAMPASHEX;
    END_UNION
    END_TYPE
    
    TYPE TIMESTAMPASREAL :
    STRUCT
        Real1:      REAL;
        Real2:      REAL;
    END_STRUCT
    END_TYPE
    
    TYPE TIMESTAMPASHEX :
    STRUCT
        {attribute 'displaymode':='hex'}Byte1:      BYTE;
        {attribute 'displaymode':='hex'}Byte2:      BYTE;
        {attribute 'displaymode':='hex'}Byte3:      BYTE;
        {attribute 'displaymode':='hex'}Byte4:      BYTE;
        {attribute 'displaymode':='hex'}Byte5:      BYTE;
        {attribute 'displaymode':='hex'}Byte6:      BYTE;
        {attribute 'displaymode':='hex'}Byte7:      BYTE;
        {attribute 'displaymode':='hex'}Byte8:      BYTE;
    END_STRUCT
    END_TYPE
    
    TYPE TIMESTAMP :
    STRUCT
        Hour:       USINT;  
        Year:       USINT;
        Month:      USINT;  
        Day:        USINT;
        mSec:       UINT;   
        Second:     USINT;  
        Minute:     USINT;
    END_STRUCT
    END_TYPE
    
    PROGRAM Test_PRG
    VAR
        TestTimestamp:      TIMESTAMP_UNION:= (As2Reals:= ( 
            Real1:= 4.14855132E-28,
            Real2:= 1.0501924E-25
        ));
    END_VAR
    

    I had to play around with the byte order in the TIMESTAMP structure, depending on your platform, it might be different, I don't know. And the TIMESTAMPASHEX struct was just for information, you wouldn't really need it.

     
  • Inna - 2021-10-28

    Thanks.
    I'll try it.

     

Log in to post a comment.