Entry type: FAQ, Entry ID: 59604194, Entry date: 04/07/2015
(2)
Rate

How do you write data to a file or read data from a file by means of a script on a panel with SIMATIC WinCC (TIA Portal)?

  • 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 this FAQ response we describe how you can write data from SIMATIC WinCC (TIA Portal) Runtime to a file or read data from the file. VBScript (Visual Basic Scripting) is used for implementing these functions. In order to save or read out information in a text file, it must be possible to access the file system of Microsoft Windows CE. The access is made via the "File System Object".
This FAQ response applies for SIMATIC WinCC (TIA Portal) in the Comfort, Advanced and Professional versions for the following operator panels: Comfort Panels and Panels of the 277 series and higher.

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.

Documentation of VBScript commands in WinCC (TIA Portal)
Information about the separate VBScript commands is available in the WinCC (TIA Portal) help system in the "Content" tab and in the WinCC Advanced manual (Entry ID: 109091876), under "Visualizing processes > Working with system functions and Runtime scripting".
 


Figl 01

Content of the entry

  • Example of saving data in a text file or csv file
  • Example of reading out data from a text file or csv file
  • Sample project

Instructions
The table below describes how to configure saving to and reading from files, in WinCC (TIA Portal).
The file formats below are used.

  • csv
  • txt

Note
Only internal tags are used in this example. Alternatively, you can also use tags with controller connection.
In this example we demonstrate writing to and reading from files on a memory card. If you use a different storage medium, this must be changed accordingly in the script.
 

No. Procedure
1 Create tags
  • Open the tag editor by means of "Project navigation > HMI_1 > HMI tags > Show all tags".
  • Add four internal tags with the following designations:
       
Tag

Data type

bExtension Bool
szMaterial WString
nPressure Int
nTemperature Int

 


Fig. 02
2 Add Objects
  • Add a new screen named "ImportExportData" under "Project navigation > Screens".
  • Insert two rectangles in the screen.
  • Add a symbolic IO field and three conventional IO fields.
  • Add a button and nine text fields.
  • Position all the objects and enter the following texts:
    • "Process values"
    • "Pressure"
    • "Temperature"
    • "Material"
    • "Text file"
    • "mbar"
    • "°C"
    • "Settings"
    • "Extension"
  • Create a text list named "Extension" of the "Value/Range" type
  • Add the following entries:
    • 0 => ".txt"
    • 1 => ".csv"
         

Fig. 03
3 Incorporate script, text list and tags
  • Create a script with the designation "Write_data". For this you go to the project navigation and click "Scripts > VBScripts > Add new VB functions".
  • Link the "Write_data" script to the "Save file" button under "Properties > Events > Click".
  • Link the "Extension" text list to the symbolic IO field "Extension".
  • Link the following tags to the corresponding objects:
     
    Tag/Script Object type Object for ...
    iPressure IO field Pressure
    iTemperature IO field Temperature
    szMaterial IO field Material
    bExtension Symbolic IO field Extension

  


Fig. 04
4 Create script - Part 1
  • Add the sample code to the "Write_data" script (see Fig. 05).

    Note
    More information about VB Script commands is available in the manual for WinCC (TIA Portal) and at the end of this FAQ response under "Further Information".
     

Description of the source code

  • <Lines 10 - 15> Declaration of local tags (only valid in the script).
  • <Line 18> The "mode" tag is initialized for opening the file (8 = Append).
  • <Lines 21-29> The "bExtension" tag is a global tag that can be used in the entire WinCC (TIA Portal) project. This tag is used to choose the file extension by means of the symbolic IO field.
    Note
    The "delimiter" tag serves as a separator and must be changed according to the country where applied!
  • <Line 32> The "On Error Resume Next" instruction is required in case a Runtime error occurs in the script. As soon as such an error occurs the next line containing the error routine is executed automatically.
    Note
    If this instruction is missing and an error occurs, the script is terminated.
  • <Line 35> The "CreateObject("FileCtl.File")" function creates an object that accesses the file system of Windows CE.
  • <Lines 38-42> The 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 can only access the file system of Windows CE. If this script is started on a PC with a different operating system, a Runtime error occurs in the script and the script is aborted.

   

Fig. 05

5 Create script - Part 2
  • <Line 45> Using the "Open" method of the "fo" object created the file specified is opened by means of the "path" (path name) and "mode" parameters.

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

  • <Lines 48-52> Error routine that is run if the corresponding file, or path name does not exist.
  • <Lines 55-57> In this section a check is made as to whether the file is new or already available. If the file is new, a header is added.
  • <Line 60> The "LinePrint" method of the "fo" object writes the specified text string to the opened file. Tag values or return values of methods can also be written to the file.
    The "Now" function writes a time stamp to the file.
  • <Line 63> The "Close" method closes the file.
  • <Line 66> The "Nothing" key instruction is required to separate the object tags that are assigned to the objects with the "Set" command.
  • <Line 68> Transfers a user-defined system message to the HMI message system.
     

Fig. 06
6 Start Runtime
  • Transfer the WinCC (TIA Portal) project to the operator panel and start Runtime.
    Note
    The script cannot be debugged between the configuration computer and the panel because of the different file systems.
  • Enter a pressure, a temperature and a material designation.
  • 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. 07
7 Open text file
If you save both files on the memory card, then you can view them on a PC by means of a card reader and open them with the appropriate program.
  • Open the file with the extension ".txt".
  • Then you can close the file.
     

Fig. 08
 
  • Open the file with the extension ".csv" with Excel, for example.
  • Close the Excel application.
     

Fig. 09
Table 01

Example of reading data from a .txt file or .csv file
The table below explains how to read data from a .txt file or .csv file and display it in WinCC (TIA Portal). IO fields are used for the display. For this reason, only the last entry is output.

Note
Since the last entry is sought in this example, the script runtime increases with the size of the file. Generally the configuration does not depend on the example described previously. However, components from it are used.
 

No. Procedure
1 Incorporate tags
  • Open the tag editor by means of "Project navigation > HMI_1 > HMI tags > Show all tags".
  • Add three internal tags with the following designations:
     
    Tag

    Data type

    szDate WString
    szString_1 WString
    iValue_1 Int
    iValue_2 Int


Note

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


Fig. 10
2 Add Objects
  • Switch to the "ImportExportData" screen.
  • Insert a rectangle in the screen.
  • If not already there, incorporate a symbolic IO field and four conventional IO fields.
  • Add a button and five text fields.
  • Position all the objects as shown in Fig. 11 and enter the following texts:
    • "Date"
    • "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 named "Extension" with the following entries:
  • 0 => ".txt"
  • 1 => ".csv"
     

Fig. 11
3 Incorporate script, text list and tags
  • Copy the "Write_data" script and change the name to "Read_data" (changes in script are dealt with in point 5)
  • Link the "Read_data" script to the "Read file" button under "Properties > Events > Click".
  • If not already done, link the "Extension" text list to the symbolic IO field "Extension".
  • Link the following tags to the corresponding objects:
     
    Tag/Script Object Object for...
    szDate IO field Date
    iValue_1 IO field Value_1
    iValue_2 IO field Value_2
    szString_1 IO field String_1

  


Fig. 12
4 Change script
  • Change the "Read_data" script as described below.

Description of the source code

  • <Lines 10-15> Change the names of the local tags (see Fig. 13).
  • <Line 18> Change the initialization of the "mode" tag from 8 (Append) to 1 (Input).
  • <Lines 21-29> Remove the "gap" tag which is no longer needed.
     

Fig. 13
5 Change script
  • <Lines 54-60> Remove the section, in which, writing to the file takes place.
  • <Lines 54-60> Add reading from the file (see Fig. 14).
     

Fig. 14
6 Start Runtime
  • Transfer the WinCC (TIA Portal) project to the operator panel and start Runtime.

    Note
    The script cannot be debugged between the configuration computer and the panel because of the different file systems.
     
  • Click the "Read file" button.
  • In the symbolic IO field you select ".csv". Click the button again.
  • Terminate the Runtime of the operator panel.
     

Fig. 15
Table 02

Download
The attachment includes the sample project described.

Plant Example_Project.zip (1183 KB)

Note
The source code used in the script can only be used in operator panels with Microsoft Windows CE, because it is not possible to access the file system of other operating systems with this source code.

Further Information
The FAQ responses listed below provide more 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 PCs: Entry ID 106501825("With WinCC (TIA Portal), how do you store PC Runtime data with a script in a file?")
  • Which VBS information and VBS programming aids are there in WinCC (TIA Portal)?
    Entry ID: 59885894
  • How do you open a Word, Excel or PDF document with a Comfort Panel?
    Entry ID: 51723931
  • How can you export and reimport WinCC Runtime data?
    Entry ID: 850338
  • How do you use a script to determine whether a storage drive with removable media (USB stick, SD/MMC card) is ready for writing on an HMI device?
    Entry ID: 89855157