Error Codes
Topics:
Data type for error codes
Error codes should be specified and transferred as hrErrorCode
(type HRESULT). The type HRESULT has the special feature that errors are represented by negative values. Warnings or information can optionally be output by means of positive values.
As an alternative you can specify nErrorID
(type UDINT).
Declaration | Error range | No error | Message/info | Check functions |
---|---|---|---|---|
hrErrorCode : HRESULT; | <0 | >=0 | >0 | IF SUCCEEDED(hrErrorCode) THEN IF FAILED(hrErrorCode) THEN |
nErrorID : UDINT; | >0 | 0 |
| IF nErrorID = 0 THEN IF nErrorID <> 0 THEN |
Error information to functions and methods
Functions and methods do not require error information, provided that no error can occur. Sample: c := AddTwoValues(a, b);
Functions and methods can, in exceptional cases, output an error case using only Boolean, provided that there can be only one cause of the error.
In most cases functions and methods indicate an error by means of an error code (HRESULT) at the return value. Often a function block outputs the error of the last called method. See also the topic Error information to function blocks.
Error information to function blocks
Function blocks do not require error information, provided that no error can occur.
It is recommended to provide error information to function blocks in the following way.
For small function blocks, which have few error causes, the following outputs are sufficient:
VAR_OUTPUT
bError : BOOL; // TRUE if an error occurred.
hrErrorCode : HRESULT; // '< 0' = error; '> 0' = info; '0' = no error/info
END_VAR
For more complex function blocks, which have many different error causes with possibly different error sources, a further output is recommended:
VAR_OUTPUT
bError : BOOL; // TRUE if an error occurred.
hrErrorCode : HRESULT; // '< 0' = error; '> 0' = info; '0' = no error/info
ipErrorMessage : I_TcMessage := fbErrorMessage; // shows detailed information about occurred errors, warnings and more
END_VAR
VAR
{attribute 'conditionalshow'}
fbErrorMessage : FB_TcMessage;
END_VAR
bError
enables the simplest possible query as to whether an error case exists. Also in the Online View of the PLC Editor you can easily see an error, if it is pending over several task cycles.
hrErrorCode
specifies the error code and can be easily passed on as such. In the Online View of the PLC editor the hexadecimal value is displayed.
ipErrorMessage
(alternatively ipResultMessage
) shows detailed information about the occurred error. In the Online View of the PLC editor, among other things, a language-dependent plain text is displayed for error description. In addition, this message can be transmitted to the TwinCAT 3 EventLogger. Custom texts for custom error codes can be defined using custom event classes in event lists. For more information see the documentation of the PLC library Tc3_EventLogger and the related sample. At the output of the function block, the information is provided as an interface to limit access to relevant content. However, it is recommended to ensure internally that the interface pointer is always valid.
On the topic of error codes, please also refer to the following topic on programming conventions: