show the entry list
WinCC (TIA Portal) -- Creating HMI configurations -- Working with system functions and Runtime scripting
How do you store information with a script in a file with WinCC (TIA Portal) PC Runtime?
With WinCC Runtime Professional, how do you read out and process the computer name of the Thin Client currently connected via the Terminal Server?
How do you configure a screen change via the controller in WinCC (TIA Portal)?
How do you access an SQL database in WinCC Runtime Advanced using a script?
Which VBS information and VBS programming aids are there in WinCC (TIA Portal)?
How can you start specific items in the Control Panel directly from the Runtime of a Comfort Panel?
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)?
Tips and tricks for creating scripts in WinCC (TIA Portal)
How do you set the time display format (12 or 24 hours, for example) of the WinCC Online Trend Control in SIMATIC WinCC Professional?
How can you export and reimport WinCC Runtime data?
How can you use scripts to dynamize objects in WinCC Comfort and WinCC Advanced?
Comfort Panels -- Creating HMI configurations -- Tips and tricks for configuring
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?
In WinCC (TIA Portal), how do you configure a connection between WinCC Runtime Advanced or HMI Panel and an S7 controller over Industrial Ethernet (PROFINET)?
Why are configured graphics not displayed on the Comfort Panel?
How do you prevent the full screen mode of the screen keyboard when keying in on the SIMATIC HMI TP700 Comfort, TP900 Comfort or TP1200 Comfort Portrait?
What are the options for configuring a check box or radio box for SIMATIC WinCC (TIA Portal)?
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)?
How can you use scripts to dynamize objects in WinCC Comfort and WinCC Advanced?
Multi Panel -- Creating HMI configurations -- Tips and tricks for configuring
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)?
How do you create a time switch with WinCC flexible?
How can you use scripts to dynamize objects in WinCC Comfort and WinCC Advanced?
With which versions can you select the MP377 15" Touch daylight readable as operator panel?
How do you configure a connection between WinCC flexible Runtime (PC/Panel) and a SIMATIC S7-300/S7400 via PROFIBUS/MPI?
Creating underscore with the MP270
How can you start specific items in the Control Panel directly from the Runtime on an operator panel?
Mobile Panels -- Creating HMI configurations -- Tips and tricks for configuring
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?
In WinCC (TIA Portal), how do you configure a connection between WinCC Runtime Advanced or HMI Panel and an S7 controller over Industrial Ethernet (PROFINET)?
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)?
How do you create a time switch with WinCC flexible?
How can you use scripts to dynamize objects in WinCC Comfort and WinCC Advanced?
How do you configure a connection between WinCC flexible Runtime (PC/Panel) and a SIMATIC S7-300/S7400 via PROFIBUS/MPI?
How can you start specific items in the Control Panel directly from the Runtime on an operator panel?
How do you implement the "Clean Screen" function for cleaning the touch screen on Mobile Panels and OP 177B?
"Clean Screen" function blocked with Mobile Panel 170
Evaluation of the handwheel position on the Mobile Panel 170
What should you pay attention to when configuring a project ID on the Mobile Panel 177 / 277 with battery pack?
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)?
Part number:

Description
In this FAQ response we describe how you can write data from 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. Access is made here by means of the "FileSystemObject".

Documentation on VBScript commands
Information about the separate VBScript commands is available in the WinCC (TIA Portal) help system in the "Content" tab under " Visualizing processes > Working with system functions and Runtime scripting".


Bild 01

Content of the entry

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

 
Bild 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 texts below (see Fig. 04).
     
    • "Process values"
    • "Pressure"
    • "Temperature"
    • "Material"
    • "Text file"
    • "mbar"
    • "°C"
    • "Settings"
    • "Extension"
       
  • Create a text list named "Extension" and of the "Value/Range" type.
  • Add the following entries:
     
    • 0 =>  ".txt"
    • 1 =>  ".csv"

Bild 03
3 Incorporating 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 tags below 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


Bild 04

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

    Note
    More information about the VBScript commands is available in the Help system of WinCC (TIA Portal).

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.


Bild 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 text string specified 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 statement 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.


Bild 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, temperature and Material.
  • Confirm with the "Save data" button.
  • In the symbolic IO field you select ".csv" and click the button again.
  • Terminate the Runtime of the operator panel.


Bild 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 ".txt" extension.
  • Then you can close the file.


Bild 08

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


Bild 09

Table 01

Example of reading information from a .txt file or .csv file
The table below explains how to read information 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 Connect 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.


Bild 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 texts below:
     
    • "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 entries below:
     
    • 0 =>  ".txt"
    • 1 =>  ".csv"


Bild 11

3 Incorporating 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 tags below 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


Bild 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 that is no longer needed.


Bild 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).


Bild 14

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

    Note
    Start the simulation with Script Debugger to debug the script.
     
  • Click the "Read data" button.
  • In the symbolic IO field you select ".csv". Click the button again.
  • Terminate the Runtime of the operator panel.


Bild 15

Table 02

Download
The attachment includes the sample project described.

Attachment 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.

 Entry ID:59604194   Date:2012-04-13 
I regard this article....as helpfulas not helpful                                 
mySupport
My Documentation Manager 
Newsletter 
CAx-Download-Manager 
Support Request
To this entry
Print
Create PDF 
Send to a friend
QuickLinks
Compatibility tool 
Topics
Help
Online Help
Guided Tour