ZPLC Standard Library Reference
ZPLC provides a complete suite of standard function blocks (FBs) and functions compliant with IEC 61131-3. These are available across all editors (ST, IL, LD, FBD, SFC).
Below is the exhaustive reference to every standard function available in the ZPLC runtime.
1. Timers
TON (On-Delay Timer)
Delays a rising edge. The output Q goes TRUE only after IN has been TRUE for the duration of PT.
- Inputs:
IN(BOOL): Trigger condition.PT(TIME): Preset Time (duration to wait).
- Outputs:
Q(BOOL): High ifINhas been continuously high forPT.ET(TIME): Elapsed Time sinceINwent high.
TOF (Off-Delay Timer)
Preserves a high state for a duration after a falling edge. Q follows IN to TRUE immediately, but delays going FALSE until PT has elapsed.
- Inputs:
IN(BOOL): Trigger condition.PT(TIME): Preset Time (duration to keep high).
- Outputs:
Q(BOOL): True whileINis true, plusPTduration afterINgoes false.ET(TIME): Elapsed Time sinceINwent low.
TP (Pulse Timer)
Generates a pulse of fixed duration regardless of how long the input stays high.
- Inputs:
IN(BOOL): Trigger condition (rising edge starts the pulse).PT(TIME): Preset Time (pulse width).
- Outputs:
Q(BOOL): Pulses true for exactlyPTduration uponINrising edge.ET(TIME): Elapsed Time since pulse started.
2. Counters
CTU (Count Up)
Increments a value on every rising edge of the input.
- Inputs:
CU(BOOL): Count up trigger (rising edge).R(BOOL): Reset input; when TRUE,CVresets to 0.PV(INT): Preset Value (target).
- Outputs:
Q(BOOL): TRUE ifCV >= PV.CV(INT): Current counter value.
CTD (Count Down)
Decrements a value on every rising edge of the input.
- Inputs:
CD(BOOL): Count down trigger (rising edge).LD(BOOL): Load input; when TRUE,CVis set toPV.PV(INT): Preset Value (initial value).
- Outputs:
Q(BOOL): TRUE ifCV <= 0.CV(INT): Current counter value.
CTUD (Count Up/Down)
Combines the features of CTU and CTD.
- Inputs:
CU(BOOL): Count up trigger.CD(BOOL): Count down trigger.R(BOOL): Reset to 0.LD(BOOL): LoadPVtoCV.PV(INT): Preset value.
- Outputs:
QU(BOOL): TRUE ifCV >= PV.QD(BOOL): TRUE ifCV <= 0.CV(INT): Current counter value.
3. Bistable (Flip-Flop) Elements
RS (Reset-Dominant Flip-Flop)
- Inputs:
S(BOOL, Set),R1(BOOL, Reset). - Outputs:
Q1(BOOL). - Behavior: Latches
Q1to TRUE whenSis TRUE. ResetsQ1to FALSE whenR1is TRUE. If bothSandR1are TRUE, the output is FALSE (Reset wins).
SR (Set-Dominant Flip-Flop)
- Inputs:
S1(BOOL, Set),R(BOOL, Reset). - Outputs:
Q1(BOOL). - Behavior: Latches
Q1to TRUE whenS1is TRUE. ResetsQ1to FALSE whenRis TRUE. If bothS1andRare TRUE, the output is TRUE (Set wins).
4. Edge Detection
R_TRIG (Rising Edge Detector)
- Inputs:
CLK(BOOL). - Outputs:
Q(BOOL). - Behavior:
Qis TRUE for exactly one execution cycle whenCLKtransitions from FALSE to TRUE.
F_TRIG (Falling Edge Detector)
- Inputs:
CLK(BOOL). - Outputs:
Q(BOOL). - Behavior:
Qis TRUE for exactly one execution cycle whenCLKtransitions from TRUE to FALSE.
5. Math Functions
Arithmetic Operators
- ADD(IN1, IN2): Returns
IN1 + IN2. Supports ANY_NUM (INT, REAL). - SUB(IN1, IN2): Returns
IN1 - IN2. Supports ANY_NUM. - MUL(IN1, IN2): Returns
IN1 * IN2. Supports ANY_NUM. - DIV(IN1, IN2): Returns
IN1 / IN2. Supports ANY_NUM. - MOD(IN1, IN2): Returns the integer remainder of
IN1 / IN2. Supports ANY_INT.
Advanced Math
ABS
Calculates the absolute value.
- Inputs:
IN(ANY_NUM). - Outputs: (ANY_NUM) Absolute value of IN.
SQRT
Calculates the square root of a number.
- Inputs:
IN(REAL). - Outputs: (REAL) Square root of IN.
SIN / COS / TAN
Standard trigonometric functions (evaluated in radians).
- Inputs:
IN(REAL). - Outputs: (REAL) Sine, Cosine, or Tangent of IN.
6. Selection Functions
MAX
Returns the maximum of two values.
- Inputs:
IN1(ANY_NUM),IN2(ANY_NUM). - Outputs: (ANY_NUM) The larger of the two inputs.
MIN
Returns the minimum of two values.
- Inputs:
IN1(ANY_NUM),IN2(ANY_NUM). - Outputs: (ANY_NUM) The smaller of the two inputs.
LIMIT
Constrains a value within a specified range.
- Inputs:
MN(ANY_NUM, minimum),IN(ANY_NUM, input),MX(ANY_NUM, maximum). - Outputs: (ANY_NUM) Returns
MNifIN < MN, returnsMXifIN > MX, otherwise returnsIN.
SEL
Selects one of two values based on a boolean condition.
- Inputs:
G(BOOL, condition),IN0(ANY, returned if G is FALSE),IN1(ANY, returned if G is TRUE). - Outputs: (ANY)
IN0orIN1.
MUX
Multiplexer. Selects one of N inputs based on an integer index.
- Inputs:
K(INT, selector index),IN0,IN1,...(ANY). - Outputs: (ANY) Returns
INk.
7. Bitwise Functions
Logical Operators
- AND(IN1, IN2): Bitwise AND. Supports ANY_BIT (BYTE, WORD, DWORD).
- OR(IN1, IN2): Bitwise OR. Supports ANY_BIT.
- XOR(IN1, IN2): Bitwise Exclusive OR. Supports ANY_BIT.
- NOT(IN): Bitwise Inversion. Supports ANY_BIT.
Shifting and Rotating
SHL
Shift Left mathematically multiplies by 2 for each shifted bit.
- Inputs:
IN(ANY_BIT),N(INT, number of bits to shift). - Outputs: (ANY_BIT)
INshifted left. Rightmost bits filled with 0.
SHR
Shift Right mathematically divides by 2 for each shifted bit.
- Inputs:
IN(ANY_BIT),N(INT, number of bits to shift). - Outputs: (ANY_BIT)
INshifted right. Leftmost bits filled with 0.
ROL
Rotate Left. Shift bits out of the left side re-enter on the right side.
- Inputs:
IN(ANY_BIT),N(INT). - Outputs: (ANY_BIT) Rotated value.
ROR
Rotate Right. Shift bits out of the right side re-enter on the left side.
- Inputs:
IN(ANY_BIT),N(INT). - Outputs: (ANY_BIT) Rotated value.
8. String Functions
ZPLC strings use bounded buffers (STRING[80]) with built-in overflow protection. Memory holds current length and maximum capacity.
- LEN(s): Returns
INTrepresenting current character count of strings. - CONCAT(s1, s2): Appends
s2tos1(modifiess1). - COPY(src, dst): Copies
srcstring intodst. - CLEAR(s): Empties string
sto 0 length. - LEFT(s, n): Modifies
sto keep only the leftmostncharacters. - RIGHT(s, n): Modifies
sto keep only the rightmostncharacters. - MID(s, pos, n): Modifies
sto keepncharacters starting at 1-based indexpos. - FIND(s1, s2): Returns
INTposition (1-based) ofs2withins1, or 0 if not found. - INSERT(s1, s2, pos): Inserts
s2intos1starting at indexpos. - DELETE(s, pos, n): Removes
ncharacters fromsstarting at indexpos. - REPLACE(s1, s2, pos, n): Overwrites
ncharacters ins1starting at indexposusings2. - STRCMP(s1, s2): Lexicographical comparison. Returns
INT(-1 if s1 < s2, 0 if s1 == s2, 1 if s1 > s2).
Note: You can also use standard = and < > operators directly on STRING variables to compare them for equality.
9. Type Conversions
Use type conversion functions to safely move payloads between memory classes.
- REAL_TO_INT(IN) / INT_TO_REAL(IN): Input INT or REAL, outputs corresponding conversion.
- BOOL_TO_INT(IN) / INT_TO_BOOL(IN): TRUE becomes 1; FALSE becomes 0.
- TIME_TO_DINT(IN) / DINT_TO_TIME(IN): Conversion between milliseconds integer and IEC TIME definitions.
10. System Functions
UPTIME
Returns the millisecond representation of the hardware/processor uptime.
- Inputs: None.
- Outputs: (UDINT / TIME) Uptime in milliseconds.
CYCLE_TIME
Retrieves the execution time taken for the previous complete PLC scheduling scan.
- Inputs: None.
- Outputs: (UDINT / TIME) Last cycle duration in milliseconds. useful for profiling cyclic logic.
WATCHDOG_RESET
Forces a clear on the hardware/software task watchdog. Use internally inside extended loops.
- Inputs: None.
- Outputs: (BOOL)
TRUEif successful.