Search code examples
plctwincatstructured-text

How to save value of integer variable in TwinCat3?


I have a program in TwinCat where 5 integer variables are being updated every 10 seconds to represent the state of 5 pumps. I want to save these values that are being generated into either a textfile or CSV file that I can later extract from the PLC. Code below:

IF toninPumpPulse.Q THEN
    rinPump1RPM := (inPump1Count/6)*6; //Pulse sample for 10 seconds. 6 pulses = 1 round.
    inPump1Count := 0; //Reset counter for next 10 second sample
    rinPump2RPM := (inPump2Count/6)*6; //Pulse sample for 10 seconds. 6 pulses = 1 round.
    inPump2Count := 0; //Reset counter for next 10 second sample
    rinPump3RPM := (inPump3Count/6)*6; //Pulse sample for 10 seconds. 6 pulses = 1 round.
    inPump3Count := 0; //Reset counter for next 10 second sample
    rinPump4RPM := (inPump4Count/6)*6; //Pulse sample for 10 seconds. 6 pulses = 1 round.
    inPump4Count := 0; //Reset counter for next 10 second sample
    rinPump5RPM := (inPump5Count/6)*6; //Pulse sample for 10 seconds. 6 pulses = 1 round.
    inPump5Count := 0; //Reset counter for next 10 second sample

I am looking to have a new CSV file be created and then populated with the variable values. I am pretty inexperienced in TwinCat and reading the Beckhoff website is not exactly helping as well.


Solution

  • You want to use a combination of several function blocks:

    • FB_FileOpen
    • FB_FilePuts
    • FB_FileClose

    All documentation you need + example code is already available on the Beckhoff infosys for this purpose:

    https://infosys.beckhoff.com/english.php?content=../content/1033/tcplclibutilities/html/tcplclibutilities_csv_sample.htm&id=

    Also see this on how to write files in general: TwinCAT 3: Write to File