Product:    ISaGRAF 4.1
Date: 2-July-2001
File: Start / Stop the Virtual Machine from an external trigger action.htm
Subject: How to implement a hook to Start / Stop the Virtual Machine from an external trigger action
Keywords: Hook - Virtual Machine


This FAQ explains one way to implement a START/ STOP of a resource (Virtual Machine) from an external triggered events.

This modification is done on the target side. Nothing needs to be changed on the workbench side.


This example is done on WinNT.

The Virtual Machine will be stopped when a text file called "stop", will be created in the "cmds" directory of the target.

The Virtual Machine will be started when a text file called "start", will be created into the cmds directory of the target.


Basically the target is put in a "ERROR" mode. (This mode is also used when there is a division by zero in the application).

In this mode, IO drivers are still called but the application is no more executed.

So, this example will also show one way to inhibit the IO drivers in this mode.


The function used to detect the creation of the stop file will be written in a target hook.

(See the FAQ called "Target Hook Integration" to see the way to proceed in order to integrate a hook in the Virtual Machine.

Add the following code into SmpHooks.c

File: SmpHooks.c
Author: GGR - ICS Triplex ISaGRAF
Creation date: 27-Jun-2001 --- 4.10 Released ---
Attached documents:

Description: Functions for managing BODY messages

Modifications: (who / date / description)

#include <dsys0def.h>
#include <dker0def.h>
#include <dker0uhk.h>
#include <sys/stat.h>

/* function used to inhibit the IO drivers*/
__declspec(dllimport) void FlagStopDriverSet(uchar FlagStopRequest);

function : kerHookBegCycExec
description : Hook called at each beginning cycle to check the existence of the start or stop texte file.

warning :
void kerHookBegCycExec(void)

struct _stat StatFile;

if (_stat("START", &StatFile) == 0) /* File START exist */
printf("Start file exist\n");

if (_stat("STOP", &StatFile) == 0) /* File STOP exist */
printf("Stop file exist\n");

FlagStopDriverSet(1); /* Set the flag in the IO driver to inhibit them */
FlagStopDriverSet(0); /* Remove the flag in the IO driver */



Now into each IO driver if you don't want it to be executed when you are in this mode, add the following code:

Note that in the ISaVM project, you will need to add in the "Additional library path" the path of the driver library,

And the name of the library (CjIOSmpl.lib in this case).



/* This function is called from a hook at each beginning of a cycle ISA_KERHOOK_BEGCYCEXEC*/

static uchar _cuSTOPFLAG;

__declspec(dllexport) void FlagStopDriverSet(uchar FlagStopRequest)
_cuSTOPFLAG = (uchar)FlagStopRequest;


Then, you can test _cuSTOPFLAG in the read or write function of the driver


if (_cuSTOPFLAG == 0) then read or update the IO points.


Note that when there will be a division by zero into the application, the IO drivers containing this modification will be no more called.

Note also that the detection of the trigged events is dependant of the application cycle time.(+/- cycle time). 


Copyright © 1999-2009 ICS Triplex ISaGRAF Inc. All rights reserved.