StatusCode

StatusCode 1:

Requirements

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

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 2:

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 3:
StatusCode 4:

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

StatusCode 5:

StatusCode 6:

The decimal value used here is defined by the OPC UA specification. The latest 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 7: