I need to change the time interval with which I send messages. Is it possible to do using a counter within the cycle for?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I need to change the time interval with which I send messages. Is it possible to do using a counter within the cycle for?
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)