raspberry-pisystemtimecodesysladder-logic

How do I use system time as a trigger in codesys ladder?


Programming a raspberry pi with codesys, using mostly ladder, basically I need to write all data that is currently in a couple arrays to a csv file at midnight, so i'd like to be able to use a dt value as a trigger. I can't figure out how to use that value in ladder, however. I can display the local time on visualizer, but if i wanted something like "if localTime=#value" then coil 'Write' turns on, where is the actual variable for system time?


Solution

  • As far as I know, you need to read the clock from local system using function blocks, for example GetDateAndTime from CAA DTUtil Extern Library. Then you need to keep it up-to-date by using a function block, for example RTC from Standard libary

    The following reads the system local time and then updates it with a RTC function block. Works at least on Windows, couldn't test with Raspberry. Please note that if the local time changes for some reason, this won't update it again. So you need to run the GetDateAndTime call every now and then, for example.

    First, a program that updates and provides the local time:

    PROGRAM PRG_UpdateSystemTime
    VAR_OUTPUT
        SystemDateTime      : DT;
    END_VAR
    VAR
        ReadLocalTime       : DTU.GetDateAndTime; //Reads local time from system
        RtcBlock            : RTC; //Real-time clock - updates the previously received local time
    END_VAR
    
    //NOTE: Output is UTC time
    
    //The block that reads local time. NOTE: Error handling is missing
    ReadLocalTime(xExecute:= TRUE);
    
    //Running real-time clock
    RtcBlock(
        EN  := ReadLocalTime.xDone AND NOT ReadLocalTime.xError,
        PDT := ReadLocalTime.dtDateAndTime,
        CDT => SystemDateTime 
    );
    

    And then one example for ladder. I think there are millions of ways. Note that the "DoSomething" will be TRUE for the whole second, so you should probably use rising edge detection.

    enter image description here