Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Standard Library

iec61131std provides the functions and function blocks of IEC 61131-3. This page says what is in it and which file declares each family. For the signature of a single function, read that file.

Using it

A release installs the library and its declarations:

FileContents
/usr/share/plc/include/*.stThe declarations
/usr/lib/<triplet>/libiec61131std.soThe implementation, also as libiec61131std.a
plc main.st -i "/usr/share/plc/include/*.st" -l iec61131std -o app --linker=cc

Some parts of the language call the library by themselves, so link it also when your own code names none of these functions:

  • ** calls EXPT
  • a comparison of STRING or WSTRING calls the string functions

Families

FamilyDeclared inContains
Arithmeticarithmetic_functions.stSQRT, LN, LOG, EXP, SIN, COS, TAN, ASIN, ACOS, ATAN, ATAN2, EXPT, the variadic ADD and MUL, and the constants PI_REAL, FRAC_PI_2_REAL, FRAC_PI_4_REAL, E_REAL, INF_REAL, and NAN_REAL, each also in an LREAL form such as PI_LREAL
Numericalnumerical_functions.stABS
Selectorsselectors.stMAX, MIN, LIMIT
Bit shiftsbit_shift_functions.stROL, ROR
Endiannessendianness_conversion_functions.stTO_BIG_ENDIAN, TO_LITTLE_ENDIAN, and back
Validationvalidation_functions.stIS_VALID, IS_VALID_BCD
Textstring_functions.stLEN, LEFT, RIGHT, MID, CONCAT, INSERT, DELETE, REPLACE, FIND, and the comparisons
Text conversionstring_conversion.stBetween STRING, WSTRING, CHAR, and WCHAR
Timerstimers.stTP, TON, TOF, each also in a _TIME and an _LTIME form
Counterscounters.stCTU, CTD, CTUD, each also with the suffix _INT, _DINT, _UDINT, _LINT, or _ULINT
Edgesflanks.stR_TRIG, F_TRIG
Bistablebistable_functionblocks.stSR, RS
Date and timedate_time_numeric_functions.stAdding and subtracting durations, dates, and times of day, and MUL_TIME and DIV_TIME
Date and timedate_time_conversion.stBetween the date and time types, and between the short and long families
Date and timedate_time_extra_functions.stCONCAT_DATE, CONCAT_TOD, the SPLIT_ family that takes such a value apart again, and DAY_OF_WEEK
Numeric conversionnum_conversion.st<TYPE>_TO_<TYPE> for every pair of numeric types
Bit conversionbit_conversion.stBetween the bit string types and BOOL, and between them and CHAR and WCHAR
Bit and numberbit_num_conversion.stBetween the bit string types and the numeric types
Truncationtrunc_int.stTRUNC_<TYPE>, which cuts the fraction of a real
Truncationreal_trunc_int.stREAL_TRUNC_<TYPE> and LREAL_TRUNC_<TYPE>, the same for one source type each
Generic conversionto_num.st, to_bit.st, to_string.st, to_date_time.stTO_<TYPE>, one generic function per target type
Text output and inputextra_functions.st<TYPE>_TO_STRING and <TYPE>_TO_WSTRING, the STRING_TO_<TYPE> family that reads a value back, TRUNC, and TIME(), which gives the time since midnight

The text conversion family covers eight of the twelve directions: each of the four text types converts to two of the other three. STRING to WCHAR, WSTRING to CHAR, CHAR to WSTRING, and WCHAR to STRING do not exist.

<TYPE>_TO_STRING covers most types, but not all of them. There is no INT_TO_STRING, SINT_TO_STRING, WORD_TO_STRING, or BOOL_TO_STRING, and the generic TO_STRING has no version for those types either, so a call compiles and then fails at the link step. Convert such a value to a wider type first, for example with INT_TO_DINT. <TYPE>_TO_WSTRING covers the same types except the unsigned integers.

Two forms of conversion

The library provides the same conversion twice. INT_TO_DINT(x) names both types, and TO_DINT(x) is generic and takes the source type from the argument. The generic form calls the named one, so both give the same result, and the named one saves a call.

Where a conversion can lose information, the name says so: TRUNC_DINT cuts the fraction of a real, and REAL_TO_DINT rounds it.