Search code examples
plccodesys

CODESYS, TON not resetting


I have an INT and TON defined

VAR
    state : INT := 0;
    timer_on : TON;
END_VAR

Now. The program is like this

IF (state = 0) THEN
    timer_on(IN := TRUE, PT := T#5s);

    IF (timer_on.Q = TRUE) THEN
        timer_on.IN := FALSE;
        state := 1;
    END_IF
END_IF

When it is executed state is changed to 0 after 5 seconds. That's ok. But when I change state back to 0 it immediately goes back to 1. According to the documentation

Q is TRUE when IN is TRUE and ET is equal to PT. Otherwise it is FALSE.

In my case, after 5 seconds Q is always TRUE even when IN is FALSE


Solution

  • Changing the input variables of a function block does NOT execute the function block. You need to run it to see the change.

    What happens when you run your program:

    1. state is 0, so first IF is entered
    2. timer_on is started for 5 seconds
    3. after 5 seconds, state is still 0 and timer_on.Q becomes true, so second if is also entered.
    4. timer_on.IN is set to false, but timer_on is not executed, state is also set to 1
    5. state is 1 so code is skipped
    6. after state is set to 0 manually, the first if is enered. timer_on is run with IN true, but because this timer was never reset, it immidiately finishes.

    The solution is to change the timer_on.IN := FALSE; line to timer_on(IN := FALSE);. This will run the timer with IN false which, as the documentation states, will reset itself for the next use.