Sample19: Synchronous File Access

This article describes how to implement a TwinCAT 3 C++ module which accesses files on the hard disk within the startup of a module, thus within the real-time environment.

Download

You can obtain the source code for this sample here: https://github.com/Beckhoff/TC1300_Samples/tree/main/S19-FileIOSync

1. Using a Visual Studio with TwinCAT installed, open the project via Open Project ....
2. Configure signing for this project by switching on TwinCAT signing with a right-click on Project->Properties->Tc Sign and configure your certificate and password if necessary.
Further information on signing C++ projects can be found in the chapter TwinCAT.
3. Select your target system.
4. Build the sample (e.g. Build->Build Solution).
5. Activate the configuration by clicking Sample19: Synchronous File Access 1:.
The sample is ready for operation.

All source code that is not automatically generated by the wizard is marked with the comment start flag "//sample code" and the comment end flag "//sample code end".
This allows you to search for these strings in the files to get an idea of the details.

Description

This sample describes file access via the TwinCAT interface ITCFileAccess. The access is synchronous and can be used for reading a configuration during startup of a module, for example.

The sample contains a C++ module TcFileTestDrv with an instance of this module TcFileTestDrv_Obj1.
In this sample, the file access takes place during the transition PREOP to SAFEOP, i.e. in the SetObjStatePS() method.

Helper methods encapsulate file handling.
First, general file information and a directory list is printed to the log device of TwinCAT 3. Then, a file %TC_TARGETPATH%DefaultConfig.xml (normally C:\TwinCAT\3.1\Target\DefaultConfig.xml) is copied to %TC_TARGETPATH%DefaultConfig.xml.bak.

To access the log entries, see the Error List tab of the TwinCAT 3 output window.
You can set the amount of information by changing the TraceLevelMax variable in the TcFileTestDrv_obj1 instance in the Parameters (Init) tab.