Mimic Manual · Chapter 7

Driving Symbols with Scripts

A symbol watches variable values and changes by itself. All a script has to do is update the variables. If you connect PLC values directly as tags (TAG.name), you do not even need this script. (Common Rules for All Symbols — Tag connection and quality display)

Preparing variables (Data module)

xscript
// [Tank and pump state]
double TankLevel   = 0      // Tank level % (0~100)
bool   PumpCmd     = false  // Pump run command (toggled on each click)
bool   PumpRun     = false  // Pump running state (input IO value, display only)
bool   PumpAlarm   = false  // Pump alarm
bool   ValveOpen   = false  // Valve open
double FeedFlow    = 0      // Feed flow rate (L/min)
bool   FlowOn      = false  // Show flow in the pipe

Declare variables for states (DataNameStateA/B) as bool, and variables for values (ValueDataName) and flow speed (FlowSpeedDataName) as numbers (int, long, double). PumpCmd is a command variable changed by clicks, so it is not connected to a symbol.

Connecting to symbols

Symbol propertyValue
Tank · ValueDataNameData::TankLevel
Pump · DataNameStateAData::PumpRun
Pump · DataNameStateBData::PumpAlarm
Pipe · DataNameStateAData::FlowOn
Pipe · FlowSpeedDataNameData::FeedFlow

Caution Variables of another module are written as module::variable. To use a Data module variable on a screen (ViewRun), always add the module name, as in Data::TankLevel. A module variable can be reached by its bare name only inside its own module.

Note The value connected to FlowSpeedDataName becomes the flow speed multiplier as is (1 = base speed, limited to 0.01–100). If your flow rate is in the tens, keep a separate ratio variable — the flow rate divided by a reference flow rate — and connect that. (Connecting with Pipes)

Updating values (a periodic function, e.g. in the Mon module)

xscript
FUNCTION UpdateMimic()
{
   // ==========================================================================
   // Module : 'Mon'
   // Caption : Update the values shown on the Mimic screen
   // Return : Always true
   // ==========================================================================
 
   // Update the states connected to symbols from the actual inputs ( the click function does not change these variables )
   Data::PumpRun   = IO["DI_PUMP_RUN"].ReadBit();
   Data::PumpAlarm = IO["DI_PUMP_ALM"].ReadBit();
   Data::TankLevel = COM["LEVEL"].GetLevel();
 
   // Show flow in the pipe only when the pump runs and the valve is open
   Data::FlowOn = ( Data::PumpRun == true ) && ( Data::ValveOpen == true );
 
   return true;
}

The IO names and communication object functions such as COM["LEVEL"].GetLevel() are examples. Replace them with the IO names and driver functions of your project. Symbols read their connected variables every 100 ms and redraw themselves, so the script does not need to refresh the screen.

Operating by click

Type a function name into a symbol's OnClickEvent and the function runs when the symbol is clicked.

xscript
FUNCTION OnPumpClick(string ctrlName, int tagIndex)
{
   // Toggle the pump run command on every click and send it to the output
   // ( PumpRun is display only, updated from the input by the periodic function, so it is not changed here )
   if( Data::PumpCmd == true )
   {
      Data::PumpCmd = false;
   }
   else
   {
      Data::PumpCmd = true;
   }
   IO["DO_PUMP_RUN"].WriteBit( Data::PumpCmd );
   return true;
}

Caution If the click function changes PumpRun, which the periodic function overwrites from the input, the next cycle sets it back to the input value. Let the click change a command variable (PumpCmd) or an output IO, and use the state variable connected to the symbol only to show the actual state. When the pump actually starts, PumpRun becomes true at the next cycle and the symbol turns to the running color.

  • Put the event function in the script of the screen that contains the symbol. For a symbol on the ViewRun screen, it must be a FUNCTION in the ViewRun module (ViewRun.xms). In another module it is not found, and only a log entry is written.
  • If the function declares parameters, the clicked symbol's Name and TagIndex are passed in that order. You can also declare it as (string sender, int tag, array params), like other screen control events (params is empty for a Mimic click), or without parameters, as in FUNCTION OnPumpClick(), when you do not need them.
  • The function returns true. If it returns false or fails to run, OnClick RunScriptEventFunction Error is written to the log.
  • The sample (MimicUISample) calls a function of another module from the screen function, as in Event::OnPumpClick();, to keep the operation logic in one module. The sample has no real equipment and its Mon module simulates the values, so its click function changes Data::PumpRun directly.

Changing symbol properties from a script

Symbol properties can be read and written directly with CONTROL["screen::control"]. For example, to hide equipment under maintenance:

xscript
FUNCTION HideMaintenanceValve()
{
   // Hide the valve symbol under maintenance from the screen
   CONTROL["ViewRun::Valve1"].Visible = false;
   return true;
}

A symbol with ValueDataName connected is overwritten by the connected value at the next 100 ms cycle, so change the connected variable rather than the property to change a value. See XScript — GUI for all GUI functions.

Subscribe