This repository present a simple example of using the modified tinyxml2 to parse XML file in TwinCAT3 C++ project.
This example will read an simple XML file, parse it, set a parameter with the parsed value. In each cycle, it adds the parameter value to the input value and writes it to the output.
Note: The Visual Studio Solution has been configured with Release on TwinCAT RT (x64). Other configurations should be adjusted accordingly.
The XML file is hardcoded to "%TC_RESOURCEPATH%XMLParser/Config.xml"
which expands to C:\TwinCAT\3.1\Target\Resource\XMLParser/Config.xml
by default.
Other supported environment variables may be found here.
Virtual environment variable | Registry value | Default value |
---|---|---|
%TC_INSTALLPATH% | InstallDir | C:\TwinCAT\3.x \ |
%TC_TARGETPATH% | TargetDir | C:\TwinCAT\3.x \Target\ |
%TC_BOOTPRJPATH% | BootDir | C:\TwinCAT\3.x \Boot\ |
%TC_RESOURCEPATH% | ResourceDir | C:\TwinCAT\3.x \Target\Resource\ |
Please add a XML file to aforementioned path. The example XML file is as follows:
<?xml version="1.0"?>
<DIFF><VALUE>2</VALUE>
</DIFF>
Note: It is possible to automatically deploy the XML file to the target in the PLC project, see this.
We will compile the tinyxml2
library as a static library and use it in the TwinCAT3 C++ project.
For this, we will follow the steps below:
- Clone the
tinyxml2
repository as a submodule. - Create a new TwinCAT3 C++ static library project
tinyxml2
. - Add
tinyxml2.h
andtinyxml2.cpp
files from cloned submodules to the project Header and Source Files Filter. - Since precompiled headers are used in the TwinCAT3 C++ project, we need to add the current project directory to the
Additional Include Directories
in thetinyxml2
project settings so that the precompiled header file can be found. - Build the project, make sure no
Tc Sign
is applied.
We present a simple example of reading an XML file using the tinyxml2
library in the TwinCAT3 versioned C++ project.
For this, we will follow the steps below:
- Create a new TwinCAT3 versioned C++ project
Example
and add a C++ Module. - Configure
TwinCAT signing
for the project. - Add the cloned path of submodule
tinyxml2
to theAdditional Include Directories
in theExample
project settings. - Add the
tinyxml2.lib
file to theAdditional Dependencies
in theExample
project settings. - Write codes using
ITcFileAccessPtr
interface with classTcFileAccess
to read the XML file and parse it using thetinyxml2
library.
The reading and parsing of the XML file are done in the function SetObjectStatePS
, i.e., when the object transfers from PREOP
to SAFEOP
state.
To be specific,
- We create an instance of
TcFileAccess
class; - The
TcFileAccess
instance was then set toPREOP
state; - We use the interface
ITcFileAccessPtr
pointing theTcFileAccess
instance to read the XML file; - We parse the result string using the
tinyxml2
library and set the parameter with the parsed value.
Finally, in SetObjStateSP
, we set the instance TcFileAccess
to INIT
state and dereference it (this is done automatically by setting the smart pointer to NULL
).