StatusCode

StatusCode 1:

Requirements

This functionality is only available for Data Access devices based on TwinCAT 3 and the import of TMC symbol files.

StatusCode 2:

New implementation in setup version 5.x

The functionality for the TwinCAT OPC UA Server in setup version 5.x is described below. This is followed by a description of the functionality for the TwinCAT OPC UA Server in setup version 4.x..

The TwinCAT OPC UA Server enables the adaptation of the OPC UA Status Code for a specific PLC symbol. Carry out the following steps to be able to change the StatusCode of a symbol at runtime.

Creating a structure

To ensure data consistency, the concept is based on a structure (see Structured Types). Each symbol for which the StatusCode is to be changed must be packaged in a structure.

Create a new structure and add the following pragma before the structure definition. The structure contains the symbol itself and a variable of data type DINT, which represents the StatusCode in decimal form.

{attribute 'OPC.UA.DA.StructuredType' := '1'}
{attribute 'OPC.UA.DA.StatusAndValue' := '1'}
TYPE ST_StatusCodeSimple :
STRUCT
  value : REAL;
  status : DINT := -2147155968; // equals 'BadCommunicationError'
END_STRUCT
END_TYPE

You can also use complex symbols, for example:

{attribute 'OPC.UA.DA.StructuredType' := '1'}
{attribute 'OPC.UA.DA.StatusAndValue' := '1'}
TYPE ST_StatusCodeComplex :
STRUCT
  {attribute 'OPC.UA.DA.StructuredType' := '1'}
  value : ST_Complex_1;
  status : DINT := -2146762752; // equals 'BadServiceUnsupported'
END_STRUCT
END_TYPE

Now create an instance of this structure, e.g. in the MAIN program, and add the pragma to enable the instance via OPC UA.

PROGRAM MAIN
VAR
  {attribute 'OPC.UA.DA' := '1'}
  stStatusCodeSimple : ST_StatusCodeSimple;
  {attribute 'OPC.UA.DA' := '1'}
  stStatusCodeComplex : ST_StatusCodeComplex;
END_VAR

The defined structure instances are now provided with the data type of the member variables defined as "value" in the TwinCAT OPC UA Server.

StatusCode 3:

Changing the StatusCode at runtime

To change the StatusCode at runtime, simply edit the value of the member variable "status". If you set this to "-2147155968", as in the sample above, the StatusCode changes to "BadCommunicationError".

StatusCode 4:
StatusCode 5:

However, if you set the value to "0", the StatusCode changes to "Good".

StatusCode 6:

StatusCode 7:

The decimal value used here is defined by the OPC UA specification. The current version of the StatusCode mapping can be viewed here. The table below lists some common StatusCodes and their numerical representation.

StatusCode

Hex

Decimal

Good

0x00000000

0

Uncertain

0x40000000

1073741824

Bad

0x80000000

-2147483648

BadUnexpectedError

0x80010000

-2147418112

BadInternalError

0x80020000

-2147352576

BadCommunicationError

0x80050000

-2147155968

BadTimeout

0x800A0000

-2146828288

BadServiceNotSupported

0x800B0000

-2146762752

When calculating the decimal representation of a StatusCode on the basis of its hexadecimal representation, please note that your computer is set to DWORD, for example the Windows computer ("Programmer" view).

StatusCode 8:

Use in Setup version 4.x

In the "older" implementation in Setup version 4.x, the variable is not displayed as a single variable on the OPC UA side, but as a structure in the same way as on the PLC side. There are also differences in the configuration of the attributes.

{attribute 'OPC.UA.DA.StructuredType' := '1'}
{attribute 'OPC.UA.DA.STATUS' := 'Quality'}
TYPE ST_StatusCodeSimple_V4 :
STRUCT
  Value : REAL;
  Quality : DINT := -2147155968; // equals 'BadCommunicationError'
END_STRUCT
END_TYPE

The StatusCode is only made available at the structure variable via OPC UA. The Value and Quality values contain the value and the value of the status code.

StatusCode 9: