Back to top

KVB C# Scripting Examples

KVB C# Scripting Examples

 

The AKI panels have the capability of running a C# program in the background.  Each HMI screen in KVB has a tab called “Script”. The C# script will run in the background only for the screen that is currently active, except for the script in the Tag screen, which runs all the time.

Following are some examples of scripts and brief explanations of what they are used for.


This script will set an alarm (a tag value) based on the value of the tag “MODBUS_FAULT1”.  This script is entered in the Tag screen so that is runs in the background all of the time.  The script is triggered whenever the tag “MODBUS_FAULT1” changes value.

public partial class Tags

      {

            void MODBUS_FAULT1_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)

            {

            if(Globals.Tags.MODBUS_FAULT1.Value != 0)

                  {

                        Globals.Tags.Alarm.Value = 1;

                  }

            if(Globals.Tags.MODBUS_FAULT1.Value == 0)

                  {

                        Globals.Tags.Alarm.Value = 0;

                  }    

            }

           

    }


Handling the move type of a motion task is not all that simple, since the parameter “MT.CNTL” has different functions based on the value of each bit.  The overall binary value of the parameter determines the move type.  This script will set a value for the tag “MoveType” based on the values of the three bits of MT.CNTL.  The script is entered in the Tag screen so that it runs in the background all of the time.  The script is triggered whenever the tag “MT_LOAD” changes value.

void MT_LOAD_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)

{

if((Globals.Tags.MT_CNTL_0.Value==0) && (Globals.Tags.MT_CNTL_1.Value==0) && (Globals.Tags.MT_CNTL_2.Value==0))

      {

            Globals.Tags.MoveType.Value=0;

      }

      if((Globals.Tags.MT_CNTL_0.Value==1) && (Globals.Tags.MT_CNTL_1.Value==0) && (Globals.Tags.MT_CNTL_2.Value==0))

      {

            Globals.Tags.MoveType.Value=1;

      }

      if((Globals.Tags.MT_CNTL_0.Value==1) && (Globals.Tags.MT_CNTL_1.Value==1) && (Globals.Tags.MT_CNTL_2.Value==1))

      {

            Globals.Tags.MoveType.Value=2;

      }

}


This script will set values for the three bits of MT.CNTL based on the value of the tag “MoveType”.  The script is entered in the Tag screen so that it runs in the background all of the time.  The script is triggered whenever the tag “MoveType” changes value.

void MoveType_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)

{

      if(Globals.Tags.MoveType.Value==0)

      {

            Globals.Tags.MT_CNTL_0.Value=0;

            Globals.Tags.MT_CNTL_1.Value=0;

            Globals.Tags.MT_CNTL_2.Value=0;

      }

      if(Globals.Tags.MoveType.Value==1)

      {

            Globals.Tags.MT_CNTL_0.Value=1;

            Globals.Tags.MT_CNTL_1.Value=0;

            Globals.Tags.MT_CNTL_2.Value=0;

      }

      if(Globals.Tags.MoveType.Value==2)

      {

            Globals.Tags.MT_CNTL_0.Value=1;

            Globals.Tags.MT_CNTL_1.Value=1;

            Globals.Tags.MT_CNTL_2.Value=1;

      }

}


This is a script that uses the “System.Threading” commands to control how the script thread is processed.  It uses the “Thread.Sleep” command to pause the script for a number of milliseconds.

Add “using System.Threading;” to the namespace list.

The command “Thread.Sleep(50);” will pause the script for 50ms.

For example:

Use this to pause between Modbus commands to give the drive time to process the commands.

namespace Neo.ApplicationFramework.Generated

{

    using System.Windows.Forms;

    using System;

    using System.Drawing;

    using Neo.ApplicationFramework.Tools;

    using Neo.ApplicationFramework.Common.Graphics.Logic;

    using Neo.ApplicationFramework.Controls;

    using Neo.ApplicationFramework.Interfaces;

    using System.Threading;

   

    public partial class Run

    {

           void Run_Opened(System.Object sender, System.EventArgs e)

            {

                  Globals.Tags.MT_NUM.Value = 21;

                  Thread.Sleep(100);

                  Globals.Tags.MT_LOAD.Value = 1;

            }

    }

}


This is a script that makes the LED on the panel blink while pressing a jog button.

void Picture2_MouseDown(System.Object sender, System.Windows.Forms.MouseEventArgs e)

            {

            Globals.Tags.SystemTagPowerLedBlinkFrequency.Value = 7;

            }

            void Picture2_MouseUp(System.Object sender, System.Windows.Forms.MouseEventArgs e)

            {

            Globals.Tags.SystemTagPowerLedBlinkFrequency.Value = 0;

            }

            void Picture3_MouseDown(System.Object sender, System.Windows.Forms.MouseEventArgs e)

            {

            Globals.Tags.SystemTagPowerLedBlinkFrequency.Value = 7;

            }

            void Picture3_MouseUp(System.Object sender, System.Windows.Forms.MouseEventArgs e)

            {

            Globals.Tags.SystemTagPowerLedBlinkFrequency.Value = 0;

            }


This is a script that has a loop and counter.  The script will loop each time digital input 1 is turned on.  The counter keeps track of the number of loops.

void Button1_Click(System.Object sender, System.EventArgs e)

      {

            Int newstate = 0;

            Int oldstate = 0;

            int cnt = 0;

            do

            {                     

                  newstate = Globals.Tags.DIN1_STATE.Value;

                  if((newstate == 1) && (oldstate == 0))

                  {

                        cnt += 1;  

                  }

                  oldstate = newstate;

                  Globals.Tags.Count.Value = cnt;    

                  Delay(200);//This is where I want the delay

            }while (cnt < 20);

      }                

      void Button2_Click(System.Object sender, System.EventArgs e)

      {

            Globals.Tags.Count.Value = Globals.Tags.DIN1_STATE.Value;

      }