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

强颜欢笑 提交于 2020-07-20 04:18:11

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/61081586/how-do-i-use-system-time-as-a-trigger-in-codesys-ladder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!