×
Siemens Industry Online Support
Siemens AG
Entry type: FAQ Entry ID: 106501825, Entry date: 05/17/2017
(17)
Rate

How do you store data with a script in a file with WinCC (TIA Portal) PC Runtime?

  • Entry
  • Associated product(s)
VB scripts permit you to store any data on the operator panel on a USB removable medium. This FAQ response explains how to program such scripts.

Description
The TIA Portal provides various system functions for permanently storing data archives and recipe data on removable media or network drives. If you also wish to store process tags and project data for reloading at a later date, then you should use Visual Basic scripts instead.
This FAQ response explains how to write such scripts for storing process data and how to use them in WinCC (TIA Portal) Advanced PC Runtime.

Notes

  • Only use scripts for saving data if the WinCC standard mechanisms (saving of data archives, etc.) are not sufficient for you application!
  • The mechanism presented depends on the operating system of your operator panel and on the configuration software used. Refer to the notes at the end of the entry for information about handling other configurations.
     
Instructions
In the example below, the process tags "Material", "Pressure" and "Temperature" are to be written to the file whose name is specified under "FileName" and later read out again as "MaterialFromFile", "PressureFromFile" and "TemperatureFromFile".
  1. In the TIA Portal you create a project with SIMATIC PC station on which WinCC RT Advanced is configured.
     
  2. In the WinCC RT tag table you create the process tags that you want to store and read again (here: "Material", "Pressure" and "Temperature", and "MaterialFromFile", "PressureFromFile" and "TemperatureFromFile"). These tags can in principle be of any type.
    In addition, you configure a "FileName" tag of the "WString" data type. This will later contain the path and name of your file. Make sure that there is enough memory available for the path.
       

    Fig. 01
     
  3. Configure the two scripts "Write_data_to_file" and "Read_data_from_file". (You can also copy these scripts as downloads from the FAQ response, see below.)
       

    Fig. 02
       

    Fig. 03
     
  4. Link the execution of the two scripts to appropriate events, like clicking a button.
     

    Fig. 04

Each time the "Write_data_to_file" script is called, a "data record" is now attached to the existing file in a new line. "Read_data_from_file" reads all the data records from the specified file and copies the values last written into the appropriate project tag.
The two scripts work as follows.

  1. Creation of a File System object, which means connection to the file system of the PC
  2. Creation or opening of the file which contains the values to be stored
  3. Writing or reading of the values
  4. Closing of the file, disconnection of the file system

To adapt the scripts to your requirements, in line 36
("Write_data_to_file")

ts.WriteLine(Material & ";" & Pressure & ";" & Temperature)

and in line 28 and in the following
("Read_data_from_file")

MaterialFromFile = field(0)
PressureFromFile = field(1)
TemperatureFromFile = field(2)

you replace the specified tag names with the tags which you want to store or read. You can specify as many tags as you wish. Make sure when writing and reading that you separate two tags accordingly with a delimiter in each case (here: ";", line 36 and line 26).

If, when you load the Start screen, you want to load the stored process tags from an existing csv file, then you must first define the associated path. You need an additional script for this.

  1. Create a new script in your project under "Scripts > VB scripts > Add new VB function".
  2. Change the name of this to "OpenDefaultFileName".
  3. Open this and add the code line "FileName = FilePath\FileName.FileType" (C:\Users\os\Documents\archive.csv, for example). The file path, file name and file type correspond to your csv file.
  4. Open the Start screen. Use "Add function" to link the script to the Start event ("Properties > Events > Loaded"). There you enter the script name "OpenDefaultFileName".
  5. Now also add the script "Read_data_from_file".

When the Start screen opens, the file path is defined and then the last line (last tag status) is read back to the process tags "MaterialFromFile", "PressureFromFile" and "TemperatureFromFile".

If, when quitting the Start screen, you want to load the current process tags ("Material", "Pressure" and "Temperature") into an existing csv file, then you must first define the associated path. You need an additional script for this.

  1. Create a new script in your project under "Scripts > VB scripts > Add new VB function".
  2. Change the name of this to "CloseDefaultFileName".
  3. Open this and add the code line "FileName = FilePath\FileName.FileType" (C:\Users\os\Documents\archive.csv, for example). The file path, file name and file type correspond to your csv file.
  4. Open the Start screen. Use "Add function" to link the script to the Start event ("Properties > Events > Closed"). There you enter the script name "CloseDefaultFileName".
  5. Now also add the script "Write_data_to_file".

When the Start screen is closed, the file path is defined and the last process tag status is written to the last line of the csv file.

Function of the sample project

The function is implemented in the sample project provided.
In the upper three input/output fields ("Current values:") you specify the process values for the tags to be stored ("Material", "Pressure", "Temperature"). Underneath you specify the directory path and the name of the file to be used.
Click the "Write to disk" button to call the "Write_data_to_file" script and write another data record to the file. Click the "Read from disk" button to call the "Read_data_from_file" script and read out all the data records from the file and display the data record last written in the "Values on file:" item.


Fig. 05

When the Start screen is loaded, the current data is read from an existing csv file "archive.csv" in the file path "C:\Users\os\Documents\". If the file is not available, the basic values of the process tags are displayed in the IO fields.
Close Runtime with Exit. When the Start screen is closed, the process tags are stored in the csv file "archive.csv" in the file path "C:\Users\os\Documents\".

Downloads
The following downloads include the archived TIA project and the scripts used in txt format.

 Registrierung notwendig  readwrite_to_file.zip (310,1 KB)

 Registrierung notwendig  scripts_readwrite_to_file.zip (1,9 KB)

Warning
Note that the version of Visual Basic used in the PC Runtime is different to the version used in Windows CE-based panels. The scripts presented here cannot be run on such panels without modification. Instead you should use the corresponding scripts from the FAQ responses listed below.

Further Information
The FAQ responses listed below provide additional information about storing text files with scripts.

  • With WinCC flexible and Windows-based panels: Entry ID 26107211("How on a Windows-based panel can you use a script to save and read out information in a text file?")
  • With WinCC flexible und PCs:: Entry ID 26107211("On a PC with WinCC flexible, how can you use a script to save and read out data in a text file?")
  • With WinCC (TIA Portal) and Windows-based panels: Entry ID 59604194("How can you write data to a file or read data from a file by means of a script on a Comfort Panel with SIMATIC WinCC (TIA Portal)?")

Software
WinCC Advanced (TIA Portal) V13 SP1, WinCC Advanced RT(TIA Portal) V13 SP1, MS Excel 2007 and 2010

Additional Keywords
File access, Archiving, Logging, Saving tags, Loading tags, Process tags, Persistent storage, Excel storage, Runtime start, RT start, Close Runtime, Close RT


Security information
In order to protect technical infrastructures, systems, machines and networks against cyber threats, it is necessary to implement – and continuously maintain – a holistic, state-of-the-art IT security concept. Siemens’ products and solutions constitute one element of such a concept. For more information about cyber security, please visit
https://www.siemens.com/cybersecurity#Ouraspiration.