×
Siemens Industry Online Support
Siemens AG
Entry type: FAQ Entry ID: 26106418, Entry date: 04/07/2015
(7)
Rate

On a PC with WinCC flexible, how can you use a script to save and read out data in a text file?

  • 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
In order to save or read out data in a text file, it must be possible to access the file system of Windows. Access is made via an object that is assigned to an object tag.

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.

Content of the entry

  • Example of how to save data in a text file or csv file
  • Example of how to read out data from a text file or csv file
  • Download containing both source codes and the sample project

Documentation of VB script commands in WinCC flexible
Information about the separate VB scripts is available in the WinCC flexible Help system in the "Contents" tab under "WinCC flexible Information System > Working with WinCC flexible > References". 
 


Fig. 01

Example of how to save data in a text file or csv file
In the table below we describe how to save data from WinCC flexible in a text file or csv file.
   

No. Procedure
1

Create a new project

  • You create a new project by clicking "Project > New..." and select "PC > WinCC flexible Runtime" as the operator panel.
     

Fig. 02
2

Create tags

  • Open the Tag Editor via "Project > Operator panel_1 > Communication > Tags".
  • Add four internal tags with the following designations:
     

    No.

    Tag

    Data type

    1 bExtension Bool
    2 szMaterial String
    3 nPressure Int
    4 nTemperature Int
    Table 01 - Tags
     

Fig. 03

Note
Only internal tags are used in this example. Of course, you can also use tags with controller connection.
3 Add objects
  • Add two rectangles - see Fig. 03.
  • Incorporate a symbolic IO field and three conventional IO fields.
  • Extend the WinCC flexible screen by adding one button and nine text fields.
  • Position all the objects as shown in Fig. 04 and enter the texts below:
    "Process values"
    "Pressure:"
    "Temperature:"
    "Material:"
    "Text file:"
    "mbar"
    "°C"
    "Settings"
    "Extension:"
  • Create an "Extension" text list with the following items:
    0 => ".txt"
    1 => ".csv"
       

Fig. 04
4 Incorporate script, text list and tags
  • Create a script with the designation "Write_data". For this, in the project tree you click "Scripts > Add Script".
  • Link the following tags and the newly created script to the relevant objects:
     
    Tag/Script Object Object for...
    nPressure IO field Pressure
    nTemperature IO field Temperature
    szString_1 IO field Material
    bExtension Symbolic IO field Extension
    Write_data Button Save file
    Table 02 - Links to various objects
     
  • Link the "Extension" text list with the symbolic IO field.
     

Fig. 05
5 Create script - Part 1
  • Add the sample code described to the "Write_data" script.

    Note:
    More information about the various VB script commands is available in the WinCC flexible Help system.
       

Description of the source code
The tags declared in <Line 10> are valid only within the script. The "bExtension" tag in <Line 13> is a global tag that can be used in the whole WinCC flexible project. With this tag, the path including file name and extension can be selected via the symbolic IO field.

The "On Error Resume Next" statement is required in case runtime errors occur in the script. As soon as such an error occurs the next line containing the error routine is executed automatically.

The statement in <Line 23> causes the object that accesses the file system of Windows to be transferred to the "fso" object tag. The following source code includes the error routine for a runtime error. If a runtime error occurs, an error message is output in the message window and the script is then terminated.

Note
If you use "Scripting.FileSystemObject", you cannot access the file system of Windows CE. If this script is started on a panel with Windows CE, a runtime error occurs in the script.

If it does not already exist, a text file is created in <Line 32>. 
   


Fig. 06
6 Create script - Part 2
Using the "GetFile" with the "fso" object created, the file specific is returned via the parameter "path" (file specification). Then the error routine already mentioned is executed if the relevant file does not exist.
  • GetFile(file specification)

In <Line 44> the "OpenAsTextStream" method is used to open the file for reading or writing. Both parameters of this method are optional, whereby the first parameter specifies the input/output mode and the second the format in which the file is to be opened.

  • OpenAsTextStream([IO mode[,Format]])

If the file cannot be opened, the error routine already described is executed to terminate the script safely.

The "WriteLine" statement is for writing texts and values from tags to the relevant file. Then the file is closed with the "Close" method.

  • WriteLine([line sequence])

The "Nothing" key instruction is required to separate the object tags that are assigned to the objects with the "Set" command.

The last instruction transfers a user-defined system message to the HMI message system. 
   


Fig. 07
7 Start Runtime
  • Start the PC Runtime of WinCC flexible.

    Note
    Start WinCC flexible Runtime with Script Debugger to debug the script.
     
  • Preset various values and enter a material designation of your choice.
  • Then click the "Save data" button.
  • In the symbolic IO field you select ".csv" and click the button again.
  • Terminate the WinCC flexible Runtime.
     

Fig. 08
8 Open text file
  • Open the text file with the extension ".txt".
  • Then you can close the file.
       

Fig. 09
  • Open the file with the extension ".csv" with Excel, for example.
  • Then you close the Excel application.
       

Fig. 10

Example of reading out data from a text file or csv file.
The following table explains how to read data from a text file or csv file and display it in WinCC flexible.

This example is based on the previous example. The configuration from the previous example is not absolutely necessary in this example. However, components from it are used.

No. Procedure
1 Create a new project - optional
If you can use the previous project, then you can go to Step 2.
  • Create a new project by clicking "Project > New..." and then selecting "PC > WinCC flexible Runtime" as operator panel.
2 Link tags
  • Open the Tag Editor via "Project > Operator panel_1 > Communication > Tags".
  • Add three internal tags with the following designations:
     

    No.

    Tag

    Data type

    1 szString_1 String
    2 nValue_1 Int
    3 nValue_2 Int
    Table 03 - Tags

    Note
    If you cannot use the previous project, then also add the tag "bExtension" of the "Bool" type.
       

Fig. 11
3 Add objects
  • Add one rectangle - see Fig. 12.
  • If not already there, incorporate a symbolic IO field and three conventional IO fields.
  • Extend the WinCC flexible screen by adding one button and four text fields.
  • Position all the objects as shown in Fig. 12 and enter the texts below: "Value_1:"
    "Value_2:"
    "String_1:"
    "Text file:"
    "Settings" (if not already there)
    "Extension:" (if not already there)
  • If not already there, create a text list entitled "Extension" with the following items:
    0 => ".txt"
    1 => ".csv"
       

Fig. 12
4 Incorporate script, text list and tags
  • Create a script with the designation "Read_data". For this, in the project tree you click "Scripts > Add Script".
  • Link the following tags and the newly created script to the relevant objects:
     
    Tag/Script Object Object for...
    nValue_1 IO field Value_1
    nValue_2 IO field Value_2
    szString_1 IO field String_1
    bExtension*) Symbolic IO field Extension
    Read_data Button Read data
    Table 04 - Links to various objects
    *)The links are only necessary if there is no configuration for saving in a file.
     
  • If not already done, link the text list "Extension" with the symbolic IO field (see also Fig. 05).
       

Fig. 13
5 Create script - Part 1
  • Add the described sample code to the "Read_data" script.

    Note

    More information about the various VB script commands is available in the WinCC flexible Help system.

Description of the source cod
All the tags that are declared in the script are also only valid within the script. Via the symbolic IO field, the path including file name and extension can be selected through the "bExtension" tag in the script <Line 17>.

The "On Error Resume Next" instruction is required in case runtime errors occur in the script. As soon as such an error occurs the next line that contains the error routine is executed automatically.

The instruction in <Line 27> causes the object that accesses the file system of Windows to be transferred to the object tag "fso". The following source code includes the error routine for a runtime error. If a runtime error occurs, an error message is output in the message window and the script is then terminated.

Note
If you use "Scripting.FileSystemObject", you cannot access the file system of Windows CE. If this script is started on a panel with Windows CE, a runtime error occurs in the script. 
   


Fig. 14
6 Create script - Part 2
Using the "OpenTextFile" method the file that is defined in the script tag "Path" is opened. All the other parameters of this method are optional. The first parameter specifies the IO mode. The second parameter defines whether a new file is to be created or not if there is no file available. The last statement specifies the format in which the file is to be opened.
  • OpenTextFile(file name[,IO mode[,Create[,Format]]])

If the file cannot be opened, the error routine already described is executed to terminate the script safely.

With the "Do-While-Loop" instruction the "Readline" method is used by the object tag "f" to read out the text file line by line while the loop is being run through. The "Split" function separates the text line (expression) read from the file at the delimiter that can be specified optionally as second argument. After splitting, the separate contents are transferred to a one-dimensional data field. The last two arguments are not used in this example.

  • Split(expression[,delimiter[,number[,compare]]])

Since empty strings might also be included when saving (see Fig. 09, for example), these have to be removed with the "Replace" function. For this, the first three parameters must be specified. The first argument must include the character string (expression) in which the search (SearchCS) is to be made for the character that is to be replaced by the argument "ReplaceWith".

  • Replace(expression, find, replace with [,start[,number[,compare]]])
     

Fig. 15
7 Create script - Part 3
In order to retain the data structure, all the data is transferred into a two-dimensional array "HiField". Since not all the data from this array is required, only the relevant data from this array is transferred to the global WinCC flexible tags.

Example - Structure and content of HiField (a, b): 
   

Array

b
0 1 2
a 0 "material" "Stahl" ""
1 "pressure" "12" "mbar"
2 "temperature" "56" "°C"
Table 05 - Sample content of the two dimensional array
   

The open file is closed in <Line 62>. The key instruction "Nothing" is required to separate the object tags that are assigned to the objects with the "Set" command.

The last instruction transfers a user-defined system message to the HMI message system.
 


Fig. 16
8 Start Runtime
  • Start the PC Runtime of WinCC flexible.

    Note
    Start WinCC flexible Runtime with Script Debugger to debug the script.
     
  • Then click the "Read data" button.
  • In the symbolic IO field you select ".csv" and click the button again.
  • Terminate the WinCC flexible Runtime.
     

Fig. 17

Download of the source codes and sample project:
The download includes the two sample source codes from the sample project as a PDF document.
Registration required wincc_flexible_quellcodes_pc.zip (18.1 KB)

The download includes the sample project described above which saves or reads data in a text file.
Registration required read_write_data_from_or_into_a_file_pc.zip (1.8 MB)

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 (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)?")
  • With WinCC (TIA Portal) and PCs: Entry ID 106501825("How do you store data with a script in a file with WinCC (TIA Portal) PC Runtime?")

Runnability and test environment
The following table lists the components that have been used to create this entry and verify the functions described.
 

Components Product and version designation
PC operating system Microsoft Windows XP SP2
Standard Tools -
Engineering Tool -
HMI Software WinCC flexible 2005 SP1 HF7
HMI System WinCC flexible PC Runtime

Additional Keywords
Save, Load, Write, Read, CSV file, TXT file