| Description | The object-oriented SEMA function block is used to control the access to a limited number of resources. The function block handles the logic of a semaphore.  It ist used  as counting semaphore  that allows more than one task to access a resource at the same time. The counter of the semaphore counts how many tasks can access  a resource at the same time.  The initial value is the initial  number of resources that can be accessed by the tasks.
Counting starts at 0. The INT value at the FB input  MaximumCount specifies the maximum number of times the resource can be accessed simultaneously (default value is 0). If this value is reached, the semaphore blocks the access to the resource until  another task releases the semaphore. For example, a semaphore with a MaximumCount value of two can handle up to three concurrent accesses.The function block provides the following methods and an initial value that are used for interacting with a semaphore: 
ACQUIRETries to get access to the resource.
 If the maximum number of permitted tasks that are allowed to access the semaphore is not reached, the resource is accessed and task execution is continued.  If the maximum number of permitted tasks that are allowed to access the semaphore is reached, the tasks waits (task is blocked) until a resource becomes available. 
 
| Note In case of a semaphore acquire, a task watchdog can occur. It is therefore recommended to use a TRY_ACQUIRE method instead of an ACQUIRE method (see also the note above).
 | RELEASEReleases (frees) the previously accessed resource by one count. This allows other tasks to acquire it later.TRY_ACQUIRETries to get access to the resource without waiting (blocking). 
 If the maximum number of permitted tasks that are allowed to access the semaphore is not reached, the resource is accessed and task execution is continued. If the maximum number of permitted tasks that are allowed to access the semaphore is reached, the task continues with a return value FALSE. (The caller shall not access the 
locked code.) 
 
 |