Creating a text file

<< Click to Display Table of Contents >>

Navigation:  »No topics above this level«

Creating a text file

mapp Services V5.16

Requirement

An end customer wants to expand the machine functionality, including writing information to a text file.

The program change should be carried out using component mapp CodeBox. Structured Text should be used as the programming language. It should be possible to change the content of the text file via the HMI application at runtime.

Machine manufacturer: Preparation

Configuration

To start a Structured Text program, the MpCodeBoxFlexGeneral configuration and the MpCodeBoxFlexInstance configuration are added.

General settings for the Structured Text programs are made in the MpCodeBoxFlexGeneral configuration. This included defining access rights for process variables as well as functions and function blocks as well defining the libraries that should be used for the Structured Text programs. Library FileIO is required for opening and editing a text file. For this reason, the library is specified under "Supported libraries". In addition, library "asstring" is used to determine the length of the text to be written. In order to specify the library in the configuration, it must already be located in the software configuration of the target system. The access rights to process variables and functions are set to "Allow all".

usecase2_mpcodeboxflexgeneral

One MpCodeBoxFlexInstance configuration is used per MpCodeBoxFlex instance. This configuration is used to define the storage medium on which the Structured Text programs are located. Storage medium "HD" is specified, which was previously defined in the configuration of the target system.

usecase2_mpcodeboxflexinstance

Creating the Structured Text program

The Structured Text program is then created. Any text editor can be used for this. The function blocks for opening, closing and writing a text file are declared in the program.

The function blocks are configured and called accordingly under "PROGRAM_MAIN". The content of the text file to be created depends on variable "MyOutput", which was created in Automation Studio task "Report".

#include 'FileIO.fun'

#include 'FileIO.var'

#include 'asstring.fun'

#include 'asstring.var'

VAR { AT 'Report' }

   MyOutput : STRING[100];

END_VAR

VAR

   fileOpen : FileOpen;

   fileClose : FileClose;

   fileWrite : FileWrite;

END_VAR

PROGRAM _MAIN

   fileOpen(enable := TRUE, pDevice := ADR('HD'), pFile := ADR('Export.txt'), mode := fiREAD_WRITE);

      IF (fileOpen.status = 0) THEN

           fileWrite(enable := TRUE, ident := fileOpen.ident, offset := 0, pSrc := ADR(MyOutput), len := strlen(ADR(MyOutput)));

           fileClose(enable := TRUE, ident := fileOpen.ident);

      END_IF

END_PROGRAM

Structured Text program: FileHandling

The Structured Text program is saved in the previously specified storage medium under "FileHandling".

Configuring the function block

Function block MpCodeBoxFlexControl is added and configured. The mapp Link of the MpCodeBoxFlexInstance configuration is used as the mapp Link. The function block is enabled using input "Enable". The name of Structured Text program "FileHandling" is specified on input parameter "Name". In order to execute the Structured Text program, process variable "StartProcess" is specified on input parameter "Start". This process variable is used to start the process.

After downloading the changes to the target system, the program can be tested. Command "StartProcess = TRUE" is used to start the Structured Text program. For information about whether the text file was processed successfully, see the info structure of the function block.

Creating a mapp View HMI application

A mapp View HMI application is added in the next step because the content of the text file should be changed at runtime via the HMI application.

logo_getting_started

For a step-by-step explanation of how to create a mapp View HMI application, see the "Getting started" tutorial Creating a mapp View HMI application.

The content of the text file is determined by process variable "MyOutput". The variable can be edited using widget TextInput. Widget "Button" can be used to start the Structured Text program via the HMI application. When clicking on the button, process variable "StartProcess" can be set to TRUE so that the Structured Text program is executed and the text file is created. The event binding can look like this:

<EventBinding>

          <Source xsi:type="widgets.brease.Button.Event" contentRefId="MyContent" widgetRefId="CreateTextFile" event="Click" />

          <EventHandler>

              <Action>

                  <Target xsi:type="opcUa.NodeAction" refId="::Report:StartProcess" >

                      <Method xsi:type="opcUa.NodeAction.SetValueBool" value="true" />

                  </Target>

              </Action>

          </EventHandler>

      </EventBinding>

It is important to note that process variables "MyOutput" and "StartProcess" must be enabled for the OPC UA communication:

usecase2_opcua

The finished HMI application can look like this:

usecase2_visucontent

End customer: Use

The end customer now has the possibility to influence the text file content via the HMI application. The desired content is entered in the text input window, and the text file is generated with button "Create text file".

usecase2_visu

The desired text file is located in the defined storage location and can be opened using a text editor.