Mapping

The TwinCAT Target Browser is available for linking OPC UA nodes to PLC symbols, which will add the address information of a PLC symbol to an OPC UA node. The nodeset can then be made available on the TwinCAT OPC UA Server.

Objects

Objects cannot be linked to PLC symbols since they do not have a value that could be linked.

Variables and properties

The mapping of variables and properties can be done by using the TwinCAT Target Browser. Please note that the data type of the PLC variable must "match" the data type of the OPC UA Node. (Depending on the setting under Tools -> Options -> Advanced -> Disable mapping validation & Show hint if mapping may not be valid) you will either be prevented from doing so or notified if a mapping does not fit.

Mapping 1:

The following table gives an overview of common, simple data types as defined in the OPC UA Base Namespace http://opcfoundation.org/UA/. It also shows the corresponding IEC61131 data type. Please note that no OPC UA data type exists for individual TwinCAT data types.

OPC UA data type

PLC data type

––

__UXINT

––

__XINT

––

__XWORD

––

BIT

Boolean

BOOL

SByte

SINT

Byte

USINT

Int16

INT

UInt16

UINT

Int32

DINT

UInt32

UDINT

Int64

LINT

UInt64

ULINT

Byte

BYTE

UInt16

WORD

UInt32

DWORD

UInt64

LWORD

Float

REAL

Double

LREAL

Int64

TIME

Int64

LTIME

DateTime

DATE

Int64

LDATE

UInt32

TOD / TIME_OF_DAY

Int64

LTOD / LTIME_OF_DAY

DateTime

DT / DATE_AND_TIME

Int64

LDT / LDATE_AND_TIME

String

STRING

Structures

Structured types can also be linked to a PLC structure via the TwinCAT Target Browser. However, there are a few special features to be considered here. On the one hand, the data structure in the PLC must "fit" the OPC UA data structure and, on the other hand, the data structure in the PLC still requires the OPC UA type information as a PLC attribute. The following button can be used to conveniently call these attributes for structure types and instances of a structure type:
Mapping 2:

The following OPC UA structure is given as an example.

<UADataType NodeId="ns=1;i=3015" BrowseName="1:ModelDataType">
  <DisplayName>ModelDataType</DisplayName>
  <References>
    <Reference ReferenceType="HasEncoding">ns=1;i=5048</Reference>
    <Reference ReferenceType="HasEncoding">ns=1;i=5050</Reference>
    <Reference ReferenceType="HasEncoding">ns=1;i=5049</Reference>
    <Reference ReferenceType="HasSubtype" IsForward="false">i=22</Reference>
  </References>
  <Definition Name="1:ModelDataType">
    <Field DataType="Byte" Name="SwitchingObjectIdentification"/>
    <Field DataType="Byte" Name="SwitchingObjectNumber"/>
    <Field DataType="Date" Name="CurrentDate"/>
    <Field DataType="Int32" ValueRank="1" ArrayDimensions="1" Name="SwitchingTimes"/>
  </Definition>
</UADataType>

The structure in the PLC must then have the following structure:

PROGRAM MAIN
VAR
  MyModel : ST_ModelDataType;
END_VAR

{attribute 'OPC.UA.DA' := '1'}
{attribute 'OPC.UA.DA.StructuredType' := '1'}
{attribute 'pack_mode' := '1'}
{attribute 'OPC.UA.AdditionalStructuredType.NamespaceName' := 'nameSpaceNameNodeset'}
{attribute 'OPC.UA.AdditionalStructuredType.Id' := 'i=3015'}
TYPE ST_ModelDataType :
STRUCT
  SwitchingObjectIdentification : BYTE;
  SwitchingObjectNumber : BYTE;
  CurrentDate : DT;
  SwitchingTimes : ARRAY[0..2] OF DINT;
END_STRUCT
END_TYPE

The additional attributes OPC.UA.AdditionalStructuredType.NamespaceName and OPC.UA.AdditionalStructuredType.id are necessary so that the TwinCAT OPC UA Server can assign the type information. They represent the namespace and the NodeID of the data type of the StructuredType.

The TwinCAT Target Browser can then be used to map MyModelInstance with the PLC variable MAIN.MyModelDataType.

Mapping 3:

Mapping 4:

Methods

Methods can be linked to a PLC function block via the TwinCAT Target Browser. However, there are a few special features to be considered here. On the one hand, the function block in the PLC must "fit" the OPC UA method and on the other hand, the function block in the PLC requires the structure of a so-called job method. This is explained in Job methods of the TwinCAT OPC UA Server documentation.

As the OPC UA method is extensively defined, only the key data of the OPC UA method is given here:

Four InputParameters: Boolean, Int16, String, Enum (INT16)

Four OutputParameters: Boolean, Int16, String, Enum (INT16)

The PLC method is as follows:

//Funktionsbaustein Deklaration:

{attribute 'OPC.UA.DA.JobMethod' := 'Method2_WithSimpleArguments'}
FUNCTION_BLOCK PUBLIC FB_Method2_WithSimpleArguments
VAR
// Timer to simulate job execution
timer : TON := (PT:=T#4S);

// Variables to determine if a job has been started (Start() method has been called) or finished
jobRunning : BOOL;
jobFinished : BOOL;

// State machine variable
state : UDINT;

//Values of method specific input parameters:
bArgIn : BOOL;
nArgIn : INT;
sArgIn : STRING;
eArgIn : E_Enum_1;

//Values of method specific output parameters:
bArgOut : BOOL;
nArgOut : INT;
sArgOut : STRING;
eArgOut : E_Enum_1;

END_VAR

//Funktionsbaustein Implementierung
CASE state OF
0:
IF jobRunning THEN
state := state + 1;
END_IF

1: //Logic of method in here:
timer(IN:=TRUE);
IF timer.Q THEN

//Set Inputs To Outputs
bArgOut := bArgIn;
nArgOut := nArgIn;
sArgOut := sArgIn;
eArgOut := eArgIn;

timer(IN:=FALSE);
jobRunning := FALSE;
jobFinished := TRUE;
state := 0;
END_IF

END_CASE

As required by every job method, the function block has three PLC methods:

//Start Deklaration:

{attribute 'TcRpcEnable' := '1'}
METHOD PUBLIC Start : HRESULT
VAR_INPUT
SimpleArgIn1 : BOOL;
SimpleArgIn2 : INT;
SimpleArgIn3 : STRING;
SimpleArgIn4 : E_Enum_1;
END_VAR
VAR_OUTPUT
hdl : UDINT; // handle, can be used for concurrent calls
END_VAR

//Start Implementierung:
IF jobRunning THEN
Start := Tc3_Module.E_FAIL;
ELSE
//Give inputs to functionblock body
bArgIn := SimpleArgIn1;
nArgIn := SimpleArgIn2;
sArgIn := SimpleArgIn3;
eArgIn := SimpleArgIn4;

hdl := 16#BE6780FF; //static because concurrent calls are disabled by checking jobRunning
jobRunning := TRUE;
Start := OpcUaStatusCodes.Good; //Set return value of method
END_IF

//CheckState Deklaration:
{attribute 'TcRpcEnable' := '1'}
METHOD PUBLIC CheckState : HRESULT
VAR_INPUT
hdl : UDINT; // handle, can be used for concurrent calls
END_VAR
VAR_OUTPUT
bBusy : BOOL; // Do not change. Used by server to find out if the job is finished

SimpleArgOut1 : BOOL;
SimpleArgOut2 : INT;
SimpleArgOut3 : STRING;
SimpleArgOut4 : E_Enum_1;
END_VAR

//CheckState Implementierung:
IF hdl <> 16#BE6780FF THEN
CheckState := OpcUaStatusCodes.BadInternalError; //Set return value of method
ELSIF NOT jobFinished THEN
bBusy := TRUE; // job is not finished yet
CheckState := OpcUaStatusCodes.Good; // done, no error occured during execution (OPC UA StatusCode "GOOD")
ELSE
//Give values to outputs
SimpleArgOut1 := bArgOut;
SimpleArgOut2 := nArgOut;
SimpleArgOut3 := sArgOut;
SimpleArgOut4 := eArgOut;

bBusy := FALSE; // job is finished
CheckState := OpcUaStatusCodes.Good; // done, no error occured during execution (OPC UA StatusCode "GOOD")
END_IF

//Abort Deklaration:
{attribute 'TcRpcEnable' := '1'}
METHOD PUBLIC Abort : HRESULT
VAR_INPUT
hdl : UDINT; // handle, can be used for concurrent calls
END_VAR

Once the method has been created in the PLC, it can be mapped in the same way as a variable. Mapping a method looks like this:

Mapping 5:

The parameters can be checked via the TwinCAT Symbol Mapping using the following buttons and adjusted if necessary. OPC UA and PLC must be compatible!

Mapping 6: