Changing application state by user program

Changing application state by user program

In this tutorial are described the guidelines and procedures for change state of your PLC using the user program.


Components

Software: MasterTool IEC XE 3.34
PLC: XP325


Tutorial Sections

1. ARCHITECTURE

2. DEVELOPMENT



1. ARCHITECTURE

In this tutorial architecture we use the Nexto Xpress Series PLC powered by a 24VDC source, where the XP325 Ethernet port was connected to the computer`s Ethernet port, using the NX92xx cable.

   


2. DEVELOPMENT

The application state (RUN/STOP) can be controlled using functions provided by the CmpApp library. Since the user program is not executed in STOP, will be used a resource called System Event to allow the code to be executed regardless the application state.
The first step is to configure a System Event that will be used to execute the code:

 

1. Access the “Task Configuration” window.
2. Go to the “System Events” tab.
3. Click on the “Add Event Handler” button, a new page will appear to configure the event.
4. Select the “BeforeReadInputs” event, which will run before the IEC Tasks execution. The field “Description” shows details about the chosen event.
5. In “Function to call”, enter the desired function name. This is where the code will be implemented.
6. The “Scope” field defines how the Function will be placed on the project. No need to change this. Keep the default.
7. The “Implementation language” field selects the IEC language for the function that will be created.
8. Then, press OK.

A new Function POU will be created according to the selected language. In the example above, the function name will be ApplicationControl, in ST.
  

Next, it is necessary to add libraries CmpIecTask and Systypes at Library Manager.

imagem

imagem

imagem



Finally, the logic to control the application state can be implemented. As shown on the screenshot below, the code is composed by three parts:

- A GVL ApplicationCommands

- A code on StartPrg

- The Function ApplicationControl




The GVL contains the variables to control the application state (RUN/STOP). The StartPrg contains the code to obtain the application pointer, which is necessary to use the functions from CmpApp. Finally, the function ApplicationControl contains the main code, which executes the functions from CmpApp.

Below there is the complete code of each part:

// ApplicationCommands GVL

VAR_GLOBAL

     bStop: BOOL := FALSE;

     bRun: BOOL := FALSE;

     pApp: POINTER TO APPLICATION := NULL;

     Result: RTS_IEC_RESULT;

END_VAR

(*The code inserted by the user into this POU is executed once after startup. Before this fist cycle it is not executed again. In case of redundant system, this one is executed in both CPUs during the startup.*)

PROGRAM StartPrg

VAR

     Result: RTS_IEC_RESULT;

END_VAR

---------------------------------------------------------------------------------------------------------------

ApplicationCommands.pApp := CmpApp.AppGetCurrent(ADR(Result));

FUNCTION ApplicationControl : DWORD

VAR_IN_OUT

        EventPrm: CmpIecTask.EVTPARAM_CmpIecTask;

END_VAR

VAR

END_VAR

---------------------------------------------------------------------------------------------------------------

IF ApplicationCommands.pApp <> NULL THEN

        IF ApplicationCommands.bStop THEN

                ApplicationCommands.bStop := FALSE;

                ApplicationCommands.Result := CmpApp.AppStopApplication(ApplicationCommands.pApp);

         ELSIF ApplicationCommands.bRun THEN

        ApplicationCommands.bRun := FALSE;

        ApplicationCommands.Result := CmpApp.AppStartApplication(ApplicationCommands.pApp);

        END_IF

END_IF

IMPORTANT: Right after an application download, the CPU will be in STOP, and this code will only be effective after putting it into RUN manually. Additionally, if the application state will be controlled remotely, it is necessary to use a communication protocol that runs even when the CPU is in STOP, such as OPC DA or OPC UA.

Do you have any questions? Then talk to us!

SEND YOUR QUESTION

Was this publication relevant to you? Evaluate the material so we can continue to improve.

Click to save rating
Liked? Then share



Subscribe to our newsletter and learn all about automation!

Receive news about the automation market, our solutions and the latest actions involving Altus directly in your e-mail.