Beckhoff TwinCAT basic tutorial: How to read and write CSV files with TwinCAT

This program is only a slight modification of the official sample program. In the original official sample, multiple modes of reading and writing are demonstrated (CSV files can be considered to be used when more data is required than TXT files, such as recording a small database collection).


However, the writing is quite confusing, and there is even no HMI interface. Readers need to learn how to quickly find the input and output in the program, such as TextModeWrite (writing CSV files in text mode). At least they need to locate the variables of the triggered BOOL variable, the file address and folder to be written, and the data to be written (where the data to be written is an array of string type, separated by commas). You can directly open the official program and run it. You can also directly set and reset bWrite in the variable table at the top to test the program's operation. However, it is not conducive to analysis. After two months, you may forget how to make this thing run.



After my modifications, I still used the original routine. The main program enters a state to do one thing. Since the bWrite trigger is a local variable in P_TextModeWrite, I can create a new Trigger variable in MAIN, and then modify the variable in the first line of the subroutine (the address is the same). This is more conducive to debugging. Of course, after writing it, I still return to the 0 state and inform what I have done.




The same is true for reading. It is important to note that although several of BFF's official examples are written very concisely (compared to reading and writing TXT files, he does not even generate class instances for basic FileOpen and FileClose, directly displaying the FBD in the form of ST, and input and output variables are all within a single PRG), in reality beginners cannot learn this, and it is also difficult to modify and port. Therefore, it is sufficient to know it, but it is difficult to master.

Like my work? Consider a donation! 👇