Sample20: FileIO-Write

This article describes the implementation of TwinCAT 3 C++ modules, which write (process) values to a file.

The writing of the file is triggered by a deterministic cycle - the execution of File IO is decoupled (asynchronous), i.e.: the deterministic cycle continues to run and is not hindered by writing to the file.

Download

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

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 Sample20: FileIO-Write 1:.
The sample is ready for operation.

Description

The sample includes an instance of TcAsyncWritingModule, which writes data to the AsyncTest.txt file in BOOTPRJPATH directory (Windows: C:\TwinCAT\3.1\Boot; TwinCAT/BSD: /usr/local/etc/TwinCAT/3.1/Boot).

TcAsyncBufferWritingModule has two buffers (m_Buffer1, m_Buffer2), which are alternately filled with current data. The member variable m_pBufferFill points to the buffer that is currently to be filled. Once a buffer is filled, the member variable m_pBufferWrite is set such that it points to the full buffer.

These data are written to a file with the aid of TcFsmFileWriter.

Note that the file has no human-readable content, such as ASCII characters; in this sample, but binary data are written to the file.