How to work with real time in custom function block on ST in Codesys 3.5

Sergey Romanov picture Sergey Romanov · Jul 26, 2015 · Viewed 9.2k times · Source

I am trying to accomplish very simple task, but it looks like I cannot figure our how.

My task is daily schedule. output Q1 have to be HIGH every day from 1:00 to 2:00. I could not find function block that would help me. I decided to create my own.

Here are my definitions.

FUNCTION_BLOCK Shedule
VAR_INPUT
    EN: BOOL;

    MO: INT;
    TU: INT;
    WE: INT;
    TH: INT;
    FR: INT;
    SA: INT;
    SU: INT;

    T1: STRING;
END_VAR
VAR_OUTPUT
    Q: BOOL;
END_VAR

The idea is this. EN is input. It should be HIGH for output Q to become HIGH too.

Others are days of the week. I can set 1 or 0 if I want Schedule work on that day. Pretty much universal function block to create any type of schedules.

Question 1: When I make week day variables type BOOL, when I add block to the LD line, it creates contact for every week day parameter.

enter image description here

What can I do to make only EN contact connected to the line?

If I try to add function block with EN/ENO, then I do not know

1) How do I know inside function block that EN is HIGh or how do I get access to that variable? 2) I cannot connect coil to my Q output. it only connects to ENO. how do i control state of ENO?

But that is not all. I added Time and Date library

enter image description here

But only thing I found to work with time is RTCLK.GetDateAndTime function block. And i cannot figure out how to work with it.

My simple task is to get current TIME only and current day of the week and if this week is enabled and current time in the range in input T1 - make Q = HIGH otherwise LOW.

Any hints on that?

Answer

dergroncki picture dergroncki · Jul 28, 2015

Write a function block as follows which will be called cyclically (e.g. every 60sec):

  1. Reset your control bit at the beginning (you will set this bit too high if the current day and time is inside your specified interval)
  2. Read the current date and current time of the system clock of the CPU (Controller)
  3. Extract the actual time and actual day. Check if the actual time is inside your specified interval (e.g. 1:00 to 2:00) and set your control bit appropriate

You should use library functions for the handling of the day and time values. Maybe you should check out the free library OSCAT BASIC. The website is in German but the documentation and the software is in English.