Style guide

  2 minutes  

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

  1. All programming in Structured Text (ST).
  2. Variable names starts with a prefix .
  3. Function parameters do not use a prefix.
  4. Constants are written in UPPERCASE, use the underscore (_) as separator and do not use a prefix.
  5. Focus on platform compatibility by favouring plain/universal code over platform specific functions.
  6. Wrap program code lines at max 80 characters.
  7. Tabs as spaces.
  8. Tab width: 2 characters.
  9. No trailing whitespace.

Coding reference

One shot signals

For compatibility reasons we do not use platform specific functions such as R_TRIG.
Name/prefixDescription
aOneShotsname of the array for storing one shot signals
bOsrprefix for a rising edge
bOsfprefix 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 typePrefix
FUNCTION_BLOCKfb
STRUCTst
ARRAYa
ENUMe
INTERFACEip
UNIONu
BOOLb
BYTEn
WORDn
DWORDn
LWORDn
SINTn
USINTn
INTn
UINTn
DINTn
UDINTn
LINTn
ULINTn
REALf
LREALf
STRINGs
WSTRINGs
TIMEt
LTIMEt
TIME_OF_DAYtd
DATE_AND_TIMEdt
DATEd
POINTERp
POINTER TO INTERFACEpip
POINTER TO POINTERpp
REFERENCE TOref

Pages built with Hugo - 23 Apr 2024 11:54 CEST