Article translated with the assistance of AI tools. Images and parts of the content will still be reviewed.
This tutorial provides guidelines and procedures to change the status of your PLC through the user program.
Components
Software: MasterTool IEC XE 3.34
PLC: XP325
Tutorial Sections
1. ARCHITECTURE
2. DEVELOPMENT
1. ARCHITECTURE
In the architecture of this tutorial, a Nexto Xpress Series PLC powered by a 24VDC power supply was used, where the Ethernet port of the XP325 was connected to the Ethernet port of the computer using the NX92xx cable.
2. DEVELOPMENT
This tutorial will explain the steps to change the PLC status while the application is running, through the user program. The PLC status (RUN/STOP) can be controlled using functions provided by the CmpApp library. Since the user program does not run in STOP mode, a feature called System Event will be used to allow the code to run regardless of 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 the “Add Event Handler” button; a new page will appear to configure the event.
4. Select the “BeforeReadInputs” event, which will execute before the IEC Tasks run. The “Description” field shows details about the selected event.
5. In “Function to call,” enter the name of the desired function. This is where the code will be implemented.
6. The “Scope” field defines how the Function will be placed in the project. There is no need to change this. Keep the default.
7. The “Implementation language” field selects the IEC language for the function to 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 the CmpIecTask and Systypes libraries in the Library Manager:
Finally, the logic to control the application status can be implemented. As shown in the screenshot below, the code consists of three parts:
- The GVL ApplicationCommands
- A code in StartPrg
- The ApplicationControl Function
The GVL contains variables to control the application status (RUN/STOP). StartPrg contains the code to get the application pointer, necessary to use the CmpApp functions. Finally, the ApplicationControl function contains the main code, which executes the CmpApp functions.
Below is the complete code for 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 in this POU is executed once after initialization. Before this first cycle, it is not executed again. In the case of a redundant system, this is executed on both CPUs during initialization.*)
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 downloading an application, the CPU will be in STOP mode, and this code will only take effect after manually setting it to RUN. Additionally, if the application status is controlled remotely, it is necessary to use a communication protocol that works even when the CPU is in STOP, such as OPC DA or OPC UA.
Comments
0 comments
Please sign in to leave a comment.