Programming style guidelines for the MotoLogix code examples.
The goal of these guidelines is to make the code more uniform and improve its readability.
Programming conventions
- All programming in Structured Text (
ST
). - Variable names starts with a prefix .
- Function parameters do not use a prefix.
- Constants are written in
UPPERCASE
, use the underscore (_
) as separator and do not use a prefix. - Focus on platform compatibility by favouring plain/universal code over platform specific functions.
- Wrap program code lines at max
80 characters
. - Tabs as
spaces
. - Tab width:
2 characters
. - No trailing whitespace.
Coding reference
One shot signals
For compatibility reasons we do not use platform specific functions such
as
R_TRIG
.Name/prefix | Description |
---|---|
aOneShots | name of the array for storing one shot signals |
bOsr | prefix for a rising edge |
bOsf | prefix for a falling edge |
// -----------------------------------------------------------------------------
// declaration
// -----------------------------------------------------------------------------
bMyInput: BOOL; // input for testing the one shot logic
aOneShots: ARRAY [0..9] OF BOOL; // bits for creating one shot signals
bOsrMyInput: BOOL; // rising edge for MyInput
bOsfMyInput: BOOL; // falling edge for MyInput
// -----------------------------------------------------------------------------
// logic
// -----------------------------------------------------------------------------
// rising edge
bOsrMyInput := bMyInput AND NOT aOneShots[0];
aOneShots[0] := bMyInput;
// falling edge
bOsfMyInput := NOT bMyInput AND NOT aOneShots[1];
aOneShots[1] := NOT bMyInput;
Naming conventions
This is based on
Beckhoff’s
TwinCAT3 programming conventions.
Variables
Below table lists the prefix for each data type.
Data type | Prefix |
---|---|
FUNCTION_BLOCK | fb |
STRUCT | st |
ARRAY | a |
ENUM | e |
INTERFACE | ip |
UNION | u |
BOOL | b |
BYTE | n |
WORD | n |
DWORD | n |
LWORD | n |
SINT | n |
USINT | n |
INT | n |
UINT | n |
DINT | n |
UDINT | n |
LINT | n |
ULINT | n |
REAL | f |
LREAL | f |
STRING | s |
WSTRING | s |
TIME | t |
LTIME | t |
TIME_OF_DAY | td |
DATE_AND_TIME | dt |
DATE | d |
POINTER | p |
POINTER TO INTERFACE | pip |
POINTER TO POINTER | pp |
REFERENCE TO | ref |