How can I put a delay in a st code with a for cycle?

2019-11-09
2019-11-12
  • Carlos Barría

    Carlos Barría - 2019-11-09

    I need to change the time interval with which I send messages. Is it possible to do using a counter within the cycle for?

     
  • Anonymous - 2019-11-12

    Originally created by: scott_cunningham

    You cannot hang the program with a for loop as a pause. This is PLC code, which means the program must run a complete execution within a certain amount of time, then the PLC does some IO scanning and then call your code again. If you hang it, then you will get an execution error.

    Instead, increment a variable every time your code executed and simply use an if statement to trigger a call to your send routine and reset your counter.

    Count := Count + 1;
    IF Count > MyTrig THEN
    SendMsg();
    Count := 0;
    END_IF

    You can also look at a state machine:

    CASE State OF

    0:
    Count := Count + 1;
    IF Count > MyTrig THEN
    Count := 0;
    State := State + 1;
    END_IF

    1:
    SendMsg();
    State := State + 1;

    2:
    Ans = RecAck();
    IF Ans THEN
    State := 0;

    END_CASE

    It is better to use an Enumeration instead of just numbers for the case states in real life, like (WAIT_TO_SEND, SEND, GET_ACK)

     

Log in to post a comment.