<< Click to Display Table of Contents >> Navigation: »No topics above this level« Coroutines |
mapp Services V5.16
The code within a program can be divided up into processes via coroutines. Various actions can be performed within a coroutine, such as moving an axis or performing calculations. For information about certain basic conditions that apply to using coroutines, see here.
Coroutines can be started, paused, resumed and deleted at a desired position in the program via statements.
The following statements are used:
•COROUTINE_CREATE: Statement "Create" is used to create a coroutine.
•COROUTINE_RESUME: A coroutine is resumed via statement "Resume"; a coroutine paused via statement "Yield" can be resumed again.
•COROUTINE_YIELD: Statement "Yield" pauses a coroutine.
•COROUTINE_DELETE: Statement "Delete" can be used to delete a coroutine.
•COROUTINE_STATE: Statement "State" can be used to query the current state of the coroutine.
For information about how the individual statements are used in the program, see section Statements.
A coroutine is always created from a function definition. The following example will explain how a coroutine works:
Example
In this example, the return value is set to different values within the coroutine.
VAR
handle1 : UDINT;
result : DINT;
END_VAR
FUNCTION myCoroutineDef : UDINT
myCoroutineDef := 5;
COROUTINE_YIELD();
myCoroutineDef := 10;
COROUTINE_YIELD();
END_FUNCTION
PROGRAM _MAIN
handle1 := COROUTINE_CREATE(myCoroutineDef,'Routine1');
result := COROUTINE_RESUME(handle1); // result = 5
result := COROUTINE_RESUME(handle1); // result = 10
result := COROUTINE_RESUME(handle1); // result still 10
COROUTINE_DELETE(handle1);
END_PROGRAM
The following shows the sequence of the program:
1: Creating the coroutine
A coroutine must first be created via statement "Create". This creates a coroutine from the function defined in the program. The coroutine is in a waiting state ("Suspend"), i.e. the coroutine has been created but not yet started.
2 and 3: Starting the coroutine
Statement "Resume" (2) starts the desired coroutine. The coroutine is in state "Running". The program now jumps to the coroutine (3) and processes the defined actions. Return value "myCoroutineDef" is set to 5 in this example. The coroutine is always executed in the idle time of the program. The next steps defined under "PROGRAM_MAIN" will not be executed any further.
4 and 5: Pausing the coroutine
If statement "Yield" appears (4), the coroutine is paused. The program sequence in "PROGRAM_MAIN" is continued after the line the coroutine was delivered on (5). The return value of the function ("result") is 5 at this point. The coroutine is in the waiting state ("Suspend") again.
6 and 7: Resuming the coroutine
The paused coroutine is resumed via statement "Resume" (6). The program sequence jumps back to the coroutine (7). In this case, the coroutine is continued at the point where it was last paused. Return value "myCoroutineDef" is set to 10.
8 and 9: Pausing the coroutine again
The coroutine is paused again by statement "Yield" (8). The program sequence jumps back to the main program (9). The return value ("result") of the coroutine is now 10.
10, 11 and 12: Resuming and ending the coroutine
The coroutine is started again via statement "Resume" (10). The main program ("PROGRAM_MAIN") is paused again and the coroutine is resumed (11). In the example, no further actions have been defined, and the end of the function has been reached ("END_FUNCTION"). The coroutine has now completed execution and jumps back to the main program (12). The return value ("result") of the coroutine is still 10 since no further changes were made to "myCoroutineDef".
13: Deleting the coroutine
The coroutine has completed execution and cannot be re-enabled and restarted. For this reason, the coroutine must be deleted via statement "Delete".
For information about how the coroutine can still be used, see here. |
A coroutine was created in the example above. Multiple routines can also be created from a function definition, however. The coroutines work independently of each other. This could look like this:
In this case, coroutine "Routine1" is executed first. If this is paused, the main program "PROGRAM_MAIN" continues to run and starts coroutine "Routine2", etc.
It is also possible to create coroutines from various function definitions:
Topics in this section: