Rule:
If data are required in several Function blocks or Functions, data exchange is handled via the block interfaces (Input, Output and InOut interfaces). Direct access to Static variables from outside is prohibited.
//Data Exchange via InOut interface
VAR_IN_OUT
SafetyMode : MyStructuredDataType;
END_VAR
Rule:
Always use access modifiers to make explicit, which access level you want to set. Therefore the usage of VAR is forbidden inside methods, classes and functions. It is only allowed in function blocks.
FUNCTION MyFunction
VAR //Not OK
MyVar : BOOL;
END_VAR
END_FUNCTION
CLASS MyClass
VAR //Not OK
MyVar : BOOL;
END_VAR
VAR PROTECTED //OK
MyVar : BOOL;
END_VAR
END_CLASS
Recommendation:
Use PRIVATE, PROTECTED or INTERNAL access modifier for your class variables. Only use PUBLIC if it is necessary.
Recommendation:
If many parameters are transferred, it should be attempted to encapsulate these in a structured data type. Examples for such PLC data types are configuration data, actual values, set point values, outputs of the current state of the function block, etc. For frequently changing control or status tags, it may make sense to declare these directly as elementary Input or Output tags for simple access.
//Bad: One Input Variable for each parameter
VAR_INPUT
ActualValue : REAL;
Setpoint : REAL;
Mode : INT;
Reset : BOOL;
END_VAR
//Good: Use a STRUCT for this parameters
TYPE
PidInput : STRUCT
ActualValue : REAL;
Setpoint : REAL;
Mode : INT;
Reset : BOOL;
END_STRUCT;
END_TYPE
VAR_INPUT
MyPidInput : PidInput;
END_VAR
Recommendation:
For structured variables (e. g. of type ARRAY, STRING, etc.) and structured data types, the InOut interface type should generally be used. For identical optimization settings of the interconnected data and the called block, this saves copying within the CPU unlike, for example, for input tags. Instead of copying, the pointer reference to the data is used directly. Furthermore, using a reference saves storage space in the load memory.
//E.g. using Axis Data in InOut interface
VAR_IN_OUT
PosAxisInterface : StandardTelegram5
TargetPositions : Array [0..5] OF REAL;
END_VAR
TIP
Programming in this way produces side effects. Your data is side-effect free only when using the input and output interface.
Recommendation:
Write output variables only once per cycle. Per editing cycle, an output tag should only be assigned a new value once. This ensures that all outputs remain consistent.
Recommendation:
Access operations to the process image using absolute identifiers should be kept to a minimum in order to remain portable and maximize performance.
Rule:
Read-only global variables (e.g. Inputs) should be accessed as VAR_EXTERNAL CONSTANT