Function blocks

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

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: Basic printing procedure with FB_XmlSrvWrite

In the first step the structure ST_MyStruct shall be written to an XML file. The mode is set to XMLSRV_ADMISSING, so that the XML file will be created automatically (if it does not already exist) as well as the structure in it. Folders will not be created automatically! This method is recommended even then, if you do not intend to write but just to read the file. So, the XML file does not have to be created manually, which saves time and avoids mistakes.

(* 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: Basic printing procedure with FB_XmlSrvWriteByName

Sample 2 has the same result as sample 1, but makes use of FB_XmlSrvWriteByNamen. However Sample 1 is more performant.

(* 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

No matter which function block is used, the XML file 'Test.xml' will be created under C:\. For that you get the same XML-content both times, sXPath has to be the same ('/dataentry/MAIN.value1'). It does not matter that value1 is not directly created in the main, but in the specific program. This path in TwinCAT is set by sSymName (Sample 2): '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: Basic reading procedure with FB_XmlSrvRead

In the following the structure created in Sample 1 or Sample 2 shall be read.

(* 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: Basic reading procedure with FB_XmlSrvReadByName

Sample 4 shows the reading procedure under usage of 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;

Here again (as in sample 2) you must differentiate between sSymName and sXPath: sXPath sets the path within the XML file - determined in sample 1 and 2. sSymName on the other hand sets the symbol name of the variable in TwinCAT, which is 'Sample4.value1'.

Requirements

Entwicklungsumgebung

Zielplattform

Einzubindende SPS Bibliotheken

TwinCAT v2.10.0

PC or CX (x86, ARM)

TcXmlDataSrv.Lib