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

How on a Windows-based panel 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 Microsoft Windows CE. 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 saving data in a text file or csv file
The following table explains how to save data from WinCC flexible in a text file or csv file.
 
No. Procedure
1 Create a new project

Create a new project by clicking "Project > New..." and select "Multi Panels > MP 370 12" Touch" as 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. You can also use tags with controller connection.

3 Add objects
  • Add two rectangles.
  • 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 Incorporating 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
    For more information on the various VBScript commands please refer to the WinCC flexible Help.


Description of the source code

The tags declared in <Line 10> are valid only within the script. The "bExtension" tag in <Line 12> 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 24> causes the object that accesses the file system of Windows to be transferred to the "f" 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 "FileCtl.File", you cannot access the file system of Windows CE. If this script is started on a panel with a different operating system, a runtime error occurs in the script.
 


Fig. 06

6

Note
Using the "Open" method of the "f" object created, the file specified is opened by means of the "path" (path name) parameter. Then the error routine already mentioned is executed if the relevant file does not exist.

  • file.Open Path name, Mode, [access], [lock], [reclength]


If the file or path name does not exist, the error routine already described is executed.

The "LinePrint" method of the "f" object writes the specified text string to the opened file. Here, tag values can also be transferred and saved.


Note

If you also want to save a time stamp for this text string, you can use the Now function.
   
Example
f.LinePrint( Now & "; material; " & szMaterial & ";")


Then the file is closed with the "Close" method. 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. 07
7 Start Runtime
  • Transfer the WinCC flexible project to the operator panel and start Runtime.

    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 Runtime of the operator panel.
     

Fig. 08
8 Open text file
If you save both text files on a memory card, then you can view them on a PC via a card reader and open them with the appropriate program.
  • Open the text file with the extension ".txt".
  • Then you can close the file.
     

Fig. 09
  
  • Open the file with the extension ".csv" with Microsoft Excel, for example.
  • 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 select "Multi Panels > MP 370 12" Touch" as operator panel.
2 Connect 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 Incorporating 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 sample code described to the "Read_data" script.

    Note

    For more information on the various VBScript commands please refer to the WinCC flexible Help.


Description of the source code

All the tags that are declared in the script are also valid only 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 24> causes the object that accesses the file system of Windows to be transferred to the object tag "f". 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 "FileCtl.File", you cannot access the file system of Windows CE. If this script is started on a panel with a different operating system, a runtime error occurs in the script.
 


Fig. 14
6 Create script - Part 2
Using the "Open" method of the "f" object created, the file specified is opened by means of the "path" (path name) parameter. Then the error routine already mentioned is executed if the relevant file does not exist.
  • file.Open Path name, Mode, [access], [lock], [reclength]


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 "LineInputString" 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 loop is terminated as soon as the end of the text file is reached.

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 are removed with the "Replace" function. For this, the first three parameters must be specified. The first argument includes 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" "Steel" ""
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
  • Transfer the WinCC flexible project to the operator panel and start Runtime.

    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 Runtime of the operator panel.
     

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_panel.zip (18.1 KB)

This contains the sample project described above, which saves or reads data in a text file.

Registration required read_write_data_from_or_into_a_file_panel.zip (1.7 MB)

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

  • With WinCC flexible and PCs: Entry ID 26106418 ("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)?")
  • 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 MP 370 12" Touch

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

 

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.
Support to the statistics
With this function the IDs found are listed according to number (format .txt).

Generate list
Copy URL
Display page in new design
mySupport Cockpit