Function blocks

The following samples show the handling of the functionblocks of the Tc2_XmlDataSrv-Library. The PLC projekt, which contains the samples, can be downloaded here: TC3_XmlSrv_Samples.zip

All samples work with the structure ST_MYSTRUCT, which in turn includes ST_INNTERSTRUCT. Both Structures are shown in the following:

Structures

TYPE ST_MYSTRUCT:
STRUCT
   fReal     : REAL;
   bBool     : ARRAY [0..2] OF BOOL;
   stInner   : ST_INNTERSTRUCT;
END_STRUCT
END_TYPE 

TYPE ST_INNTERSTRUCT:
STRUCT
    nInteger  : INT;
    sString   : STRING;
END_STRUCT
END_TYPE

Sample 1: Write operation with FB_XmlSrvWrite

In the first step the structure ST_MyStruct is to be written into an XML file. The mode is set to XMLSRV_ADMISSING, so that the XML file is generated automatically and the structure is created within it. Folders are not created automatically! For larger structures this approach is also recommend if the file is only to be read later. In this way the XML file does not have to be created manually, and errors are avoided.

(* Sample1 creates an XML-file under the path C:\Test.xml and writes value1 to it. 
   FUNCTIONBLOCK: FB_XmlSrvWrite *)

PROGRAM Sample1
VAR
   value1         : ST_MyStruct;
   fbXmlSrvWrite  : FB_XmlSrvWrite;
   bExecute       : BOOL;
   sFilePath      : T_MaxString := 'C:\Test.xml'; (* CE: '\Hard Disk\Test.xml' *)
   sXPath         : T_MaxString := '/dataentry/MAIN.value1';
END_VAR

fbXmlSrvWrite(
 nMode       := XMLSRV_ADDMISSING, 
 pSymAddr    := ADR(value1),
 cbSymSize   := SIZEOF(value1), 
 sFilePath   := sFilePath,
 sXPath      := sXPath,
 bExecute    := bExecute
);
bExecute := TRUE;

Sample 2: Write operation with FB_XmlSrvWriteByName

Sample 2 leads to the same result as sample 1, but the block FB_XmlSrvWriteByName is used. Sample 1 offers higher performance.

(* Sample2 creates an XML-file under the path C:\Test.xml and writes value1 to it.
   FUNCTIONBLOCK: FB_XmlSrvWriteByName *)

PROGRAM Sample2
VAR
    value1         : ST_MyStruct;
    fbXmlSrvWrite  : FB_XmlSrvWriteByName;
    bExecute       : BOOL;
    sSymName       : T_MaxString := 'Sample2.value1';
    sFilePath      : T_MaxString := 'C:\Test.xml'; (* CE: '\Hard Disk\Test.xml' *)
   sXPath         : T_MaxString := '/dataentry/MAIN.value1';
END_VAR

fbXmlSrvWrite(
 nMode      := XMLSRV_ADDMISSING, 
 sSymName   := sSymName,
 sFilePath  := sFilePath,
 sXPath     := sXPath,
 bExecute   := bExecute
);
bExecute:= TRUE;

XML file

In both examples the XML file 'Test.XML' is created under C:\. In order to ensure that the XML file has the same content in both variants, the same sXPath is used, i.e. '/dataentry/MAIN.value', although value1 is not stored in Main, but directly in the respective programs. sSymName (Sample 2) specifies the location of the variables in TwinCAT: 'Sample2.value1'!

<dataentry>
  <MAIN.value1>
    <fReal>0</fReal>
    <bBool index="0">false</bBool>
    <bBool index="1">false</bBool>
    <bBool index="2">false</bBool>
    <stInner>
      <nInteger>0</nInteger>
      <sString></sString>
    </stInner>
  </MAIN.value1>
</dataentry> 

Sample 3: Read operation with FB_XmlSrvRead

The following section describes the process of reading the structure of the XML file created in sample 1 and/or sample 2. Sample 3 uses the block FB_XmlSrvRead for this purpose.

(* Sample3 reads an XML-file (C:\Test.xml)
   FUNCTIONBLOCK: FB_XmlSrvRead *)

PROGRAM Sample3

VAR
    value1        : ST_MyStruct;
    fbXmlSrvRead  : FB_XmlSrvRead;
    bExecute      : BOOL;
    sFilePath     : T_MaxString := 'C:\Test.xml'; (* CE: '\Hard Disk\Test.xml' *)
    sXPath        : T_MaxString := '/dataentry/MAIN.value1';
END_VAR

fbXmlSrvRead(
 pSymAddr   := ADR(value1),
 cbSymSize  := SIZEOF(value1), 
 sFilePath  := sFilePath,
 sXPath     := sXPath,
 bExecute   := bExecute
);
bExecute:= TRUE;

Sample 4: Read operation with FB_XmlSrvReadByName

Sample 4 shows the read operation using the block FB_XmlSrvReadByName.

(* Sample4 reads an XML-file (C:\Test.xml)
   FUNCTIONBLOCK: FB_XmlSrvReadByName *)

PROGRAM Sample4
VAR
   value1         : ST_MyStruct;
   fbXmlSrvRead   : FB_XmlSrvReadByName;
   bExecute       : BOOL;
   sSymName       : T_MaxString := 'Sample4.value1';
   sFilePath      : T_MaxString := 'C:\Test.xml'; (* CE: '\Hard Disk\Test.xml' *)
   sXPath         : T_MaxString := '/dataentry/MAIN.value1';
END_VAR

fbXmlSrvRead(
 sSymName    := sSymName,
 sFilePath   := sFilePath,
 sXPath      := sXPath,
 bExecute    := bExecute
);
bExecute:= TRUE;

Like for sample 2, note that sSymName and sXPath differ: sXPath indicates the path within the XML file. This was specified in sample 1 and/or sample 2. sSymName indicates the symbol name of the TwinCAT variable and is therefore 'Sample4.value1.

Requirements

Development environment

Target system type

PLC libraries to be linked

TwinCAT v3.1 Build 4011

PC or CX (x86, x64, ARM)

Tc2_XmlDataSrv