Sample19: Synchronous File Access
This article describes how to implement a TC3 C++ module which accesses files on the hard disk within the startup of a module, thus within the real-time environment.
Download
Get the source code for this sample.
- 1. Unpack the downloaded ZIP file.
- 2. Using a Visual Studio with TwinCAT installed, open the project via Open Project ....
- 3. 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.
For more information on signing C++ projects, click here. - 4. Select your target system.
- 5. Build the sample (e.g. Build->Build Solution).
- 6. Activate the configuration by clicking on .
- The sample is ready for operation.
The whole source code, which is not automatically generated by the wizard, is identified with the comment start flag "//sample code" and the comment end flag "//sample code end".
In this way you can search for these strings in the files, in order 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 window of TwinCAT 3. Then, a file %TC_TARGETPATH%DefaultConfig.xml (normally C:\TwinCAT\3.x\Target\DefaultConfig.xml) is copied to %TC_TARGETPATH%DefaultConfig.xml.bak.
For access to the log entries, see the Error List tab in the TwinCAT 3 output window.
The amount of information can be set by changing the variable TraceLevelMax in the instance TcFileTestDrv_obj1 in Parameter (Init) tab.