IEC61131-3 programming conventions

TwinCAT PLC libraries

Programming conventions for creating the IEC61131-3

The following programming conventions are used for naming objects, variables and instances of objects and variables in a standardised fashion. This will make the code and particularly the interfaces of function blocks and functions (e.g. in the PLC libraries) easily legible and comprehensible for us and for others. Code maintenance becomes easier.

It goes without saying that this document only refers to the creation of new PLC libraries. For compatibility reasons, existing libraries are, of course, not changed!!! For TwinCAT users and PLC programmers, this document should be seen as a guideline, since these programming conventions are not specified in the IEC61131-3 standard.

Naming of the PLC libraries

All libraries that are supplied with TwinCAT or can be installed from the Supplement CD start with the prefix Tc.

Example: TcUtilities.Lib

The libraries for the PC runtime system have the extension *.Lib, the Bus Controller libraries(BCs)have the extension *.Lb6 and additionally the letters BC at the end of the library name.

Example:

TcTempCtrl.Lib

TcTempCtrlBC.Lb6

Identifier

The identifier should be named with a uniform prefix, so that the object type can easily be recognised. The recommended conventions are listed below:

Definition of object names

Object | Prefix | Description | Example
FUNCTION_BLOCK | FB_ | Function Block | FB_GetData
STRUCT | ST_ | Structure | ST_BufferEntry
ENUM | E_ | Type of Enumeration | E_SignalStates
TYPE | T_ | Reference | T_Nibble
PROGRAM | P_ | Program | P_Axis
FUNCTION | F_ | Function | F_Convert

Generating instances of objects

Object | Prefix | Description | Instance | Call example
function block | fb | Instance name from function block | fbGetData | fbGetData();
struct | st | Instance name of the structure | stBufferEntry | stBufferEntry.nCounter := 5;
enum | e | Enum instance | eSignalState | eSignalState := E_STOP;
alias type | none | Instance name of a reference type | Nibble | Nibble := 1;

Generating instances of variables

Type | Prefix | Description | Example
SINT, USINT, …, DINT, UDINT, BYTE, WORD, DWORD, LWORD.... | n, i | numeric / integer | nCount, iError
BOOL | b | bit | bSwitch
REAL, LREAL | f | float | fValue
STRING | s | string | sName
TIME | t | time | tDelay
DATE | d | date | dMonday
DATE_AND_TIME | dt | date and time | dtNewYear
ARRAY[...] OF ... | arr | arrays | arrMessages
p | p | pointer | pData

Other prefixes that may also be used

Prefix | Description | Example
cb | count of bytes | cbLength
cw | count of words | cwRead

Upper / lower case

The object name prefix will always be upper case. Underscore (_) is used as a separator between prefix and object name. For variable and instance names, the prefix will always be lower case. The first letter of an identifier will always be upper case. If an identifier consists of several words, the first letter of a word will always be upper case. Separators (e.g. _ ) between the two words should not be used (see examples above).

Valid characters

The identifiers should only contain the following letters, digits and special characters:

0...9, A...Z, a...z,

Remarks

All identifiers should be specified in English.

Version check

Each library must contain a function in which the following information is stored:

  • Version number

  • TwinCAT version under which the library was generated

  • Date (American format)

  • Programmer

  • Comments

The version number must be readable during program run-time. The version number consists of three numeric values. Each value can be read individually (see example). In order to make the function name unambiguous, it will contain the name of the library. Example from the library TcBABasic.lib:

FUNCTION F_GetVersionTcBABasic : UINT
VAR_INPUT
       nVersionElement       :       INT;
END_VAR
VAR
END_VAR
(* Version history:
Date          | Version    | created under      | Author          | Remark
----------------------------------------------------------------------------------------
10/01/2001    | 1.0.0      | V2.7.0 (Build 402) | S. Mustermann   | first release
*)

CASE nVersionElement OF
1: (* major number *)
       F_GetVersionTcBABasic := 1;
2: (* minor number *)
       F_GetVersionTcBABasic := 0;
3: (* revision number *)
       F_GetVersionTcBABasic := 0;
ELSE
   F_GetVersionTcBABasic := 16#FFFF;
END_CASE