Notes on the documentation

This description is only intended for the use of trained specialists in control and automation engineering who are familiar with the applicable national standards.
It is essential that the following notes and explanations are followed when installing and commissioning these components.

The responsible staff must ensure that the application or use of the products described satisfy all the requirements for safety, including all the relevant laws, regulations, guidelines and standards.

Disclaimer

The documentation has been prepared with care. The products described are, however, constantly under development.
For that reason the documentation is not in every case checked for consistency with performance data, standards or other characteristics.
In the event that it contains technical or editorial errors, we retain the right to make alterations at any time and without warning.
No claims for the modification of products that have already been supplied may be made on the basis of the data, diagrams and descriptions in this documentation.

Trademarks

Beckhoff®, TwinCAT®, EtherCAT®, Safety over EtherCAT®, TwinSAFE® and XFC® are registered trademarks of and licensed by Beckhoff Automation GmbH.
Other designations used in this publication may be trademarks whose use by third parties for their own purposes could violate the rights of the owners.

Patent Pending

The TwinCAT Technology is covered, including but not limited to the following patent applications and patents:
EP0851348, US6167425 with corresponding applications or registrations in various other countries.

Copyright

© Beckhoff Automation GmbH & Co. KG, Germany.
The reproduction, distribution and utilization of this document as well as the communication of its contents to others without express authorization are prohibited.
Offenders will be held liable for the payment of damages. All rights reserved in the event of the grant of a patent, utility model or design.


Overview

The Standard Library includes all IEC61131-3 POUs. The POUs can be classified in:

  • Bistable Function Blocks
  • Trigger Function Blocks
  • Counter
  • Timer
  • String Functions


F_TRIG

TwinCAT PLC Library: Standard

FUNCTION_BLOCK F_TRIG

Detector for a Falling Edge

VAR_INPUT

VAR_INPUT
    CLK	: BOOL;	(* Signal to detect *)
END_VAR

VAR_OUTPUT

VAR_OUTPUT
    Q	: BOOL;	(* Edge detected *)
END_VAR
VAR
    M	: BOOL;
END_VAR

The output Q and the help variable M will remain FALSE as long as the input variable CLK returns TRUE. As soon as CLK returns FALSE, Q will first return TRUE, then M will be set to TRUE. This means each time the function is called up, Q will return FALSE until CLK has a rising followed by a falling edge.

Development environment | Target system type | PLC libraries to include
TwinCAT Version >= 2.6.0 | PC or CX (x86) | Standard.Lib
TwinCAT Version >= 2.6.0 | BC (165) | Standard.Lb6
TwinCAT Version >= 2.9.0 | BCxx50 or BX | Standard.lbx
TwinCAT Version >= 2.10.0 Build >= 1301 | CX (ARM) | Standard.lib


R_TRIG

FUNCTION_BLOCK R_TRIG

Detector for a Rising Edge

VAR_INPUT

VAR_INPUT
    CLK	: BOOL;	(* Signal to detect *)
END_VAR

VAR_OUTPUT

VAR_OUTPUT
    Q	: BOOL;	(* Edge detected *)
END_VAR
VAR
    M	: BOOL;
END_VAR

The output Q and the help variable M will remain FALSE as long as the input variable CLK is FALSE. As soon as CLK returns TRUE, Q will first return TRUE, then M will be set to TRUE. This means each time the function is called up, Q will return FALSE until CLK has falling edge followed by an rising edge.


RS

FUNCTION_BLOCK RS

Resetting Bistable Function Blocks

Q1 = RS (SET, RESET1)

means:Q1 = NOT RESET1 AND (Q1 OR SET)

VAR_INPUT

VAR_INPUT
    SET	: BOOL;
    RESET1	: BOOL;
END_VAR

VAR_OUTPUT

VAR_OUTPUT
    Q1	: BOOL;
END_VAR

Internal implementation of fb:

Q1: = NOT RESET1 AND (Q1 OR SET);


SR

FUNCTION_BLOCK SR

Making Bistable Function Blocks Dominant:

Q1 = SR (SET1, RESET)

means:Q1 = (NOT RESET AND Q1) OR SET1Q1, SET1 and RESET are BOOL variables.

VAR_INPUT

VAR_INPUT
    SET1	: BOOL;
    RESET	: BOOL;
END_VAR

VAR_OUTPUT

VAR_OUTPUT
    Q1	: BOOL;
END_VAR

Internal implementation of fb:

Q1 := (NOT RESET AND Q1) OR SET1;


SEMA

FUNCTION_BLOCK SEMA

A Software Semaphore (Interruptible).

VAR_INPUT

VAR_INPUT
    CLAIM	: BOOL;
    RELEASE	: BOOL;
END_VAR

VAR_OUTPUT

VAR_OUTPUT
    BUSY	: BOOL;
END_VAR

If BUSY is TRUE when SEMA is called up, this means that a value has already been assigned to SEMA (SEMA was called up with CLAIM = TRUE). If BUSY is FALSE, SEMA has not yet been called up or it has been released (called up with RELEASE = TRUE).

Internal implementation of fb:

BUSY := X;
IF CLAIM
THEN
X:=TRUE;
ELSIF RELEASE
THEN
BUSY := FALSE;
X:= FALSE;
END_IF


CTD

FUNCTION_BLOCK CTD

Decrementer

VAR_INPUT

VAR_INPUT
    CD	: BOOL;	(* Count Down on rising edge *)
    LOAD	: BOOL;	(* Load Start Value *)
    PV	: WORD;	(* Start Value *)
END_VAR

VAR_OUTPUT

VAR_OUTPUT
    Q	: BOOL;	(* Counter reached 0 *)
    CV	: WORD;	(* Current Counter Value *)
END_VAR

When LOAD is TRUE, the counter variable CV will be initialized with the upper limit PV. If CD has a rising edge from FALSE to TRUE, CV will be lowered by 1 provided CV is greater than 0 (i.e., it doesn't cause the value to fall below 0).Q returns TRUE when CV is less than or equal to 0.


CTU

FUNCTION_BLOCK CTU

Incrementer:

VAR_INPUT

VAR_INPUT
    CU	: BOOL; (* Count Up *)
    RESET	: BOOL; (* Reset Counter to 0 *)
    PV	: WORD; (* Counter Limit *)
END_VAR

VAR_OUTPUT

VAR_OUTPUT
    Q	: BOOL; (* Counter reached the Limit *)
    CV	: WORD; (* Current Counter Value *)
END_VAR

The counter variable CV will be initialized with 0 if RESET is TRUE. If CU has a rising edge from FALSE to TRUE, the function block CV will be raised by 1 provided CV is smaller than PV (i.e., it doesn't cause an overflow). Q will return TRUE when CV is greater than or equal to the upper limit PV.


CTUD

FUNCTION_BLOCK CTUD

Incrementer and Decrementer

VAR_INPUT

VAR_INPUT
    CU	: BOOL;	(* Count Up *)
    CD	: BOOL;	(* Count Down *)
    RESET	: BOOL;	(* Reset Counter to Null *)
    LOAD	: BOOL;	(* Load Start Value *)
    PV	: WORD;	(* Start Value / Counter Limit *)
END_VAR

VAR_OUTPUT

VAR_OUTPUT
    QU	: BOOL;	(* Counter reached Limit *)
    QD	: BOOL;	(* Counter reached Null *)
    CV	: WORD;	(* Current Counter Value *)
END_VAR

If RESET is valid, the counter variable CV will be initialized with 0. If LOAD is valid, CV will be initialized with PV. If CU has a rising edge from FALSE to TRUE, CV will be raised by 1 pro-vided CV does not cause an overflow. If CD has a rising edge from FALSE to TRUE, CV will be lowered by 1 provided this does not cause the value to fall below 0.QU returns TRUE when CV has become greater than or equal to PV. QD returns TRUE when CV has become less than or equal to 0.


TOF

FUNCTION_BLOCK TOF

Timer off-delay

VAR_INPUT

VAR_INPUT
    IN	: BOOL;	(* starts timer with falling edge, resets timer with rising edge *)
    PT	: TIME;	(* time to pass, before Q is set *)
END_VAR

VAR_OUTPUT

VAR_OUTPUT
    Q	: BOOL;	(* is FALSE, PT seconds after IN had a falling edge *)
    ET	: TIME;	(* elapsed time *)
END_VAR

When IN is TRUE, Q is TRUE and ET is 0.As soon as IN becomes FALSE, the time will begin to be counted in milliseconds in ET until its value is equal to that of PT. It will then remain constant. Q is FALSE when IN is FALSE and ET is equal to PT. Otherwise it is TRUE. Thus, Q has a falling edge when the time indicated in PT in milliseconds has run out. Graphic Display of TOF Behavior Over Time:

The function TOF requires 15 byte data


TON

FUNCTION_BLOCK TON

Timer on-delay

VAR_INPUT

VAR_INPUT
    IN	: BOOL;	(* starts timer with rising edge, resets timer with falling edge *)
    PT	: TIME;	(* time to pass, before Q is set *)
END_VAR

VAR_OUTPUT

VAR_OUTPUT
    Q	: BOOL;	(* is TRUE, PT seconds after IN had a rising edge *)
    ET	: TIME;	(* elapsed time *)
END_VAR

If IN is FALSE, Q is FALSE and ET is 0.As soon as IN becomes TRUE, the time will begin to be counted in milliseconds in ET until its value is equal to PT. It will then remain constant. Q is TRUE when IN is TRUE and ET is equal to PT. Otherwise it is FALSE. Thus, Q has a rising edge when the time indicated in PT in milliseconds has run out.
Graphic Display of TON Behavior Over Time:

The function TON requires 15 byte data


TP

FUNCTION_BLOCK TP

The pulse timer block can be used to generate output pulses of a given time duration.

VAR_INPUT

VAR_INPUT
    IN	: BOOL; (* Trigger for Start of the Signal *)
    PT	: TIME; (* The length of the High-Signal in ms *)
END_VAR

VAR_OUTPUT

VAR_OUTPUT
    Q	: BOOL; (* The pulse *)
    ET	: TIME; (* The current phase of the High-Signal *)
END_VAR

If IN is FALSE, Q is FALSE and ET is 0.

As input IN goes TRUE, tho output Q follows and remains TRUE for the pulse duration as specified by time input PT.

While the pulse output ist TRUE, the elapsed time ET ist increased.

On the termination of the pulse, the elapsed time is held until the beginning of the next pulse, at which point it is reset.

The output Q will remain TRUE until the pulse time has elapsed, irrespective of the state of the input IN.

Graphic Display of the TP Time Sequence:

The function TP requires 14 byte data


String functions overview

String functions

Note:

Plaese note, string functions are not "thread safe": When using tasks, string functions may only be used in a single task. If the same function is used in different tasks, there is a danger of overwriting.


CONCAT

FUNCTION CONCAT

Concatenation (combination) of two strings.

FUNCTION CONCAT :STRING(255)

VAR_INPUT
    STR1:STRING(255);
    STR2:STRING(255);
END_VAR

Example in IL:

LD 'SUSI'
CONCAT 'WILLI'
ST Var1 (* Result is 'SUSIWILLI' *)

Example in ST:

Var1 := CONCAT ('SUSI','WILLI');


DELETE

FUNCTION DELETE

The function DELETE removes a partial string from a larger string at a defined position. The input variable STR is type STRING, LEN and POS are type INT, the return value of the function is type STRING. DELETE(STR, LEN, POS) means: Delete LEN characters from STR beginning with the character in the POS.

FUNCTION DELETE :STRING(255)

VAR_INPUT
    STR	:STRING(255);
    LEN	:INT;
    POS	:INT;
END_VAR

Example in IL:

LD 'SUXYSI'
DELETE 2,3
ST Var1 (* Result ist 'SUSI' *)

Example in ST:

Var1 := DELETE ('SUXYSI',2,3);


FIND

FUNCTION FIND

FIND searches for a partial string within a string. FIND(STR1, STR2) means: Find the position of the first character where STR2 appears in STR1 for the first time. If STR2 is not found in STR1, then OUT:=0.

FUNCTION FIND :INT

VAR_INPUT
    STR1	:STRING(255);
    STR2	:STRING(255);
END_VAR

Example in IL:

LD 'SUXYSI'
FIND 'XY'
ST Var1 (* Result is 3 *)

Example in ST:

Var1 := FIND ('SUXYSI','XY');


INSERT

FUNCTION INSERT

INSERT inserts a string into another string at a defined point. INSERT(STR1, STR2, POS) means: insert STR2 into STR1 after position POS.

FUNCTION INSERT :STRING(255)

VAR_INPUT
    STR1	:STRING(255);
    STR2	:STRING(255);
    POS	:INT;
END_VAR

Example in IL:

LD 'SUSI'
INSERT 'XY',2
ST Var1 (* Result is 'SUXYSI' *)

Example in ST:

Var1 := INSERT ('SUSI','XY',2);


LEFT

FUNCTION LEFT

Left returns the left, initial string for a given string. LEFT (STR, SIZE) means: Take the first SIZE character from the right in the string STR.

FUNCTION LEFT :STRING(255)

VAR_INPUT
    STR	:STRING(255);
    SIZE	:INT;
END_VAR

Example in IL:

LD 'SUSI'
LEFT 3
ST Var1 (* Result is 'SUS' *)

Example in ST:

Var1 := LEFT ('SUSI',3);


LEN

FUNCTION LEN

Returns the length of a string.

FUNCTION LEN : INT

VAR_INPUT
    STR	:STRING(255);
END_VA

Example in IL:

LD 'SUSI'
LEN
ST Var1 (* Result is 4 *)

Example in ST:

Var1 := LEN ('SUSI');


MID

FUNCTION MID

Mid returns a partial string from within a string. MID (STR, LEN, POS) means: Retrieve LEN characters from the STR string beginning with the character at position POS.

FUNCTION MID :STRING(255)

VAR_INPUT
    STR	:STRING(255);
    LEN	:INT;
    POS	:INT;
END_VAR

Example in IL:

LD 'SUSI'
MID 2,2
ST Var1 (* Result is 'US' *)

Example in ST:

Var1 := MID ('SUSI',2,2);


REPLACE

FUNCTION REPLACE

REPLACE replaces a partial string from a larger string with a third string. REPLACE(STR1, STR2, L, P) means: Replace L characters from STR1 with STR2 beginning with the character in the P position.

FUNCTION REPLACE :STRING(255)

VAR_INPUT
    STR1	:STRING(255);
    STR2	:STRING(255);
    L	:INT;
    P	:INT;
END_VAR

Example in IL:

LD 'SUXYSI'
REPLACE 'K',2,2
ST Var1 (* Result is 'SKYSI' *)

Example in ST:

Var1 := REPLACE ('SUXYSI','K',2,2);


RIGHT

FUNCTION RIGHT

Right returns the right, initial string for a given string. RIGHT (STR, SIZE) means: Take the first SIZE character from the right in the string STR.

FUNCTION RIGHT :STRING(255)

VAR_INPUT
    STR	:STRING(255);
    SIZE	:INT;
END_VAR

Example in IL:

LD 'SUSI'
RIGHT 3
ST Var1 (* Result is 'USI' *)

Example in ST:

Var1 := RIGHT ('SUSI',3);