Sample20a: FileIO-Cyclic Read / Write
This article is a more comprehensive sample than S20 and S19. It demonstrates cyclic read and/or write access to files from a TC3-C++ module.
Download
Here you can access 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.
Description
The sample describes how to access files for reading and/or writing using the CycleUpdate method, i.e. in a cyclic manner.
This sample contains the following projects and module instances.
- A static library (TcAsyncFileIo) offers the file access.
The code for the file access can be shared, therefore this code is located in a static library that is used by the driver projects. - A driver (TcAsyncBufferReadingDrv) provides two instances:
- ReadingModule: uses the static library to read the AsyncTest.txt file.
- WriteDetectModule: detects write operations and initiates read operations.
- A driver (TcAsyncBufferWritingDrv) provides one instance:
- WriteModule: uses the static library to write the AsyncTest.txt file.
When starting the sample, the writing module begins to write data to the file located in the boot project path (Windows: C:\TwinCAT\3.x\Boot\AsyncTest.txt; TwinCAT/BSD: /usr/local/etc/TwinCAT/3.x/Boot/AsyncTest.txt). The input variable bDisableWriting can be used to prevent writing.
The objects are connected to one another: once the writing is complete, the WritingModule triggers the DetectModule of TcAsyncBufferReadingDrv. As a result of this, the ReadingModule initiates a reading procedure.
Observe the nBytesWritten / nBytesRead output variables of the WritingModule / ReadingModule. Over and above that, protocol messages are generated at verbose level. As before, these can be configured with the help of the TraceLevelMax parameter of the module.
- A driver (TcAsyncFileFindDrv) provides one instance
- FileFindModule: list the files of a directory with the help of the static library.
Initiate the action with the help of the input variable bExecute. The FilePath parameter contains the directory whose files are to be listed (Windows: c:\TwinCAT\3.1\Boot\*; TwinCAT/BSD: /usr/local/etc/TwinCAT/3.x/Boot/*).
Observe the sequence tracking (verbose protocol level) with regard to the list of files found.
Understanding the sample
The project TcAsyncFileIO contains various classes in a static library. This library is used by driver projects for reading and writing.
Each class is intended for a file access operation such as Open / Read / Write / List / Close / .... Since execution takes place in a cyclic real-time context, each operation has a status, and the class encapsulates this state machine.
As an entry point for understanding the file access, begin with the classes TcFsmFileReader and TcFsmFileWriter.
If too many history tracking messages occur, which hamper understanding of the sample, you can disable modules!