Features, discussions, tips, tricks, questions, problems and feedback

Script to Read an Agent and then Change to a different Graphic Form

Hello,

i am Currently busy with a Script that reads an Digital Agent (1 or 0) that represents when an machine is running or not. I managed to read the raw value of the digital when the page opens (i have a messagebox that pops up saying the machine has stopped when the value is 0 and when it is 1 then the page loads normally) however when i change the value to 1 and then 0 again i want the message box to pop up again and at this moment it does not.

Here is my code i have currently trying to see if it registers that the value has changed but it did not display any of the message boxes i added so i have no idea if im doing something wrong. Any help or guidance would be appreciated.

   private void DataElement_ValueChanged(object sender, EventArgs e)
   {
       // Add some debugging code to verify that the function is being called
       MessageBox.Show("DataElement_ValueChanged function called");

       // Check if the sender is the correct DataElement object
       DataElement de = sender as DataElement;
       if (de != null)
       {
           MessageBox.Show("DataElement value changed to: " + de.Value);
       }

       // Call the CheckMachineStop function
       CheckMachineStop();       
   }

   private void CheckMachineStop()
   {
       // Read the data element value using ReadDataElement method
       MethodReturnInfo mri = MyConnection.ReadDataElement("RS27_Tags.Digital.MACHINERUN.rawValue");

       // Get the DataElementCollection object from the MethodReturnInfo object
       DataElementCollection collectionReturned = mri.ReturnObject as DataElementCollection;

       // Check if the collection contains the data element
       if (collectionReturned.Contains("RS27_Tags.Digital.MACHINERUN.rawValue"))
       {
           // Get the DataElement object from the collection
           DataElement de = collectionReturned["RS27_Tags.Digital.MACHINERUN.rawValue"];

           // Check if the element's value is 0
           if (Convert.ToInt32(de.Value) == 0)
           {
               // Shows A message to the Operator that the machine has stopped
              MessageBox.Show("Machine is Stopped");

           }
       }
   }

i managed to get it working via a Hidden Text box, so when ever the value changes it triggers my function. However now instead of it Popping up a text box saying that the machine has stopped i have made a Page Dedicated to be a stop page which holds information on why the machine has stopped based on a couple of factors however i want to let my Navigation change to an Certain Graphic form Called “Stop Page” how will i implement it into my current code?

 private void InitializeComponent()

{
// …
this.label.TextChanged += new System.EventHandler(this.label_TextChanged);
this.timer = new System.Windows.Forms.Timer();
this.timer.Interval = 30000; // 30 seconds
this.timer.Tick += new System.EventHandler(this.timer_Tick);

     // ...
 }

 private void label_TextChanged(object sender, System.EventArgs e)

{
CheckMachineStop();
timer.Start();
}
private void timer_Tick(object sender, System.EventArgs e)
{
CheckMachineStop();
}

 private void CheckMachineStop()
 {
     // Read the data element value using ReadDataElement method
     MethodReturnInfo mri = MyConnection.ReadDataElement("RS27_Tags.Digital.MACHINERUN.rawValue");

     // Get the DataElementCollection object from the MethodReturnInfo object
     DataElementCollection collectionReturned = mri.ReturnObject as DataElementCollection;

     // Check if the collection contains the data element
     if (collectionReturned.Contains("RS27_Tags.Digital.MACHINERUN.rawValue"))
     {
         // Get the DataElement object from the collection
         DataElement de = collectionReturned["RS27_Tags.Digital.MACHINERUN.rawValue"];

         // Check if the element's value is 0
         if (Convert.ToInt32(de.Value) == 0)
         {
             // Check if a message box is not already open
             if (!isMessageBoxOpen)
             {
                 isMessageBoxOpen = true;
                 MessageBox.Show("Machine is Stopped");
                 isMessageBoxOpen = false;
             }
         }
         else
         {
             timer.Stop();
         }
     }
 }
1 Like

Hi Erik,

Are you using graphic form script or a script spider? I think using a script spider for this will be much easier.

With script spider you can link the digital to one of its new inputs i.e. var 1. The script spider will trigger every time the digital changes.

Based on the digital value passed to var 1 you can launch a popup graphic form.

You can use script below to open a popup form:

You can also do this without script as in the article below. A lot easier than scripting and here you will be looking at digital off (0) not on (1), thus using linking an IF spider to before the LogicTriggerSpider to check when its 0 to trigger the Application spider:

1 Like

It could be more efficient to use the Spider Engine instead of scripting for this:

  1. No polling is required (to read the data element intermittently)
  2. It will only trigger on element change

Step 1:

On the monitoring form (the form that will monitor the value), open the Spider Engine:

image

Step 2:

  1. Create an “IF” Spider, “Logic Trigger” Spider and “Application Spider” and set the “Operator” to “=” and the “Constraint” to “1”
  2. Drag the output of the relevant tag / Adroit slot / data element (the tag that indicates a machine failure for example, i.e. Adroit.tag.value" to the input of the IF Spider
  3. Drag the output logical result of the IF Spider to the input of the Logic Trigger Spider
  4. Create an Application Spider which is configured to Open the specific graphic form
  5. Select this application spider in the Logic Trigger Spider

When the value of the relevant data element changes to 1, the application spider will trigger, which in turn will open the relevant graphic form

Example graphic forms:

Download here:
Forms.zip (15.5 KB)

CheckConditionOpenForm (Configured with some additional behaviors)

…and InformationForm which opens when the Digital.value = 1

image

1 Like