Getting Started
In the following you will get to know the basics in working with the TwinCAT XML Data Server.
Primitive data types
The table below shows the supported primitive data types
Data type | PLC example | Xml example |
---|---|---|
UDINT, DINT, UINT, INT, USINT, SINT, DWORD, WORD, BYTE |
|
|
LREAL, REAL |
|
|
STRING |
|
|
TIME, DATE, TOD,DT |
|
|
BOOL |
|
|
Example: The plc global variable Var1 ist typed as DINT.This ist the XML structure
<dataentry>
<Var1>10</Var1>
</dataentry>
The input parameters of the function block FB_XmlSrvRead must be set as follows:
fbRead.pSymAddr := ADR(value1);
fbRead.cbSymSize := SIZEOF(value1);
fbRead.sFilePath := 'C:\Test.xml';
fbRead.sXPath :='/dataentry/Var1';
The root element in the XML file and the name of the variable are freely selectable in the XML file. These values must be specified in the input parameters sXPath
, sFilePath
. Also, multiple variable definitions can be stored in a single xml file:
<dataentry>
<Var1>10</Var1>
<Var2>100</Var2>
<Var3>
<a>100</a>
<b>10</b>
</Var3>
</dataentry>
To access the definition vor Var3.a
set sXpath:='/dataentry/Var3.a'
Structures
Structures in the XML file have the same hierarchical structure as in the PLC. However, there is the possibility to skip individual sub elements in the XML file. The individual sub elements of the structure must have the same names as in the PLC, otherwise they are skipped. If sub elements in the XML file cannot be converted to the correct data type, they will also be skipped.
Example: The global variable Var2
is typed as ST_MYSTRUCT
:
TYPE ST_MYSTRUCT:
STRUCT
a: UINT;
b: DINT;
c: LREAL;
d: STRING;
END_STRUCT
END_TYPE
A possible xml file representation:
<variables>
<Var1>10</Var1>
<Var2> <!-- sXPath := '\variables\Var2' --!>
<a>100</a>
<b>-10</b>
<c>1.2</c>
<d>Hallo</d>
</Var2>
</variables>
In this case, all sub elements are completely and correctly defined, so the variable is fully initialized. In the following example, however, only the sub element c
is serialized:
<variables>
<Var1>10</Var1>
<Variable2> <!-- sXPath := '\variables\Variable2' --!>
<Info>Test entry</Info>
<a>-100</a>
<c>1.2</c>
</Variable2>
</variables>
Sub element a
cannot be converted because it is negative and a UINT is required. The sub-element b
is completely missing. The <Info>
tag is skipped because it is not defined in the PLC file.
Arrays
To specify the index of arrays, the "Index" attribute must be used for the individual array elements. Individual array elements can also be omitted. These are then simply skipped.
Example: the plc variable array1
is defined as: ARRAY[1..4] OF DINT
.
A possible xml file representation:
<dataentry>
<array1 index="1">10</array1>
<array1 index="2">10</array1>
<array1 index="3">10</array1>
<array1 index="4">10</array1>
</dataentry>
Requirements
Entwicklungsumgebung | Zielplattform | Einzubindende SPS Bibliotheken |
---|---|---|
TwinCAT v2.10.0 | PC or CX (x86, ARM) | TcXmlDataSrv.Lib |