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

How to get the system time in milliseconds?

  • elsabz - 2016-04-30

    Hello everyone,
    I can not understand how I can get the system time in milliseconds, someone can give an example to do this?

    Thank you


  • Anonymous - 2016-05-01

    Originally created by: scott_cunningham

    For a PC, you need the SysTimeRtc library (part of the system library). Look for the function SysTimeRtcHighResGet. You can use the pTimestamp to get your time. See the library documentation. The most confusing part for me was the library implies pTimestamp is a pointer, but for my CoDeSys install, it is a ULINT. When you use other hardware besides a PC (i.e. PLC or embedded controller), the hardware vendor will have different libraries to access a realtime clock (if it exists on the hardware).

       Rtc : ULINT;
       Clock : TOD;
    SysTimeRtcHighResGet(pTimestamp:= Rtc);
    Clock := ULINT_TO_TOD(Rtc);

    For your needs, just use the ULINT - it is already in milliseconds.

  • elsabz - 2016-05-02

    Hy Scott,

    Tank you for the answer, but i need thys for RaspBerry pi 3 and not for PC.
    I try with thys:

       LTIME1: LTIME;
       TimeOFDay: TOD;
       milliseconds: DINT;
       LTIME1 := LTIME();
       TimeOFDay:= LTIME_TO_TOD(LTIME1);
       milliseconds:= TOD_TO_DINT(TimeOFDay);

    With thys not work... the value is not stable e not good increment,

  • elsabz - 2016-05-02


    I solved the problem!
    The value was not stable because it was testing the application from a webvisualization, and this was set by default to a 100 ms cycle time.
    Therefore, when I went to see the value it did not seem a plausible time. I apologize for the inconvenience, I hope it can serve to someone.

    The final code is as follows:

       TIME1: TIME;
       milliseconds: DINT;
       TIME1 := TIME();
       milliseconds:= TIME_TO_DINT(TIME1)/10;
  • nilo - 2024-02-28

    Hi, thanks for sharing your final results. may I ask why are you diving by 10?

  • nilo - 2024-02-28

    Last edit: nilo 2024-02-28

Log in to post a comment.