Tips and Tricks: The instance path attribute

In this tips and tricks post I will demonstrate the usage of the attribute: ‘instance-path’. The instance path attribute allows you to get the instance path of the containing POU in a string variable. Something I find particularly useful for error-logs and debug messages.

How to use it

1. We need a string variable which will receive the instance path, in this example we call it ‘Path’.

2. We give the variable ‘Path’ the pragma ‘{attribute ‘instance-path’}’, we also give it the pragma ‘{attribute ‘noinit’}’ to let the compiler know that this variable should not be implicitly assigned.

3. The containing POU gets the pragma ‘{attribute ‘reflection’}’ to let the program know it should search for instance-path variables within this POU.

Example:


{attribute 'reflection'}
FUNCTION_BLOCK POU
VAR
   {attribute 'instance-path'}
   {attribute 'noinit'}
   Path : STRING;
   Error : STRING;
END_VAR
Error := CONCAT('Error occurred at ', Path);

Running this in the PLC gives us a error message with details about the place where the error occurred:

Conclusion

This short tips and tricks post showed the usage the instance path attribute. A attribute particularly useful for more detailed logs and error messages. More information about the instance path attribute can be found here.

 

Gerhard Barteling

Gerhard is a mechatronic engineer with a predilection for software engineering.