Difference between revisions of "Subscribing to PLC data via code"

From AdvancedHMI
Jump to: navigation, search
(C# Example:)
(C# Example:)
Line 30: Line 30:
  
  
The Following is an example of how to programmatically set the comm drivers and create subscriptions to PLC addresses using the "DataSubscriber2" control. The "DataSubscriber2" runs on a background thread which allows for asynchronous capability when gathering tag information.
+
The Following is an example of how to programmatically set PLC Items to a dataSubscriber2 component
  
 
     using AdvancedHMIDrivers;
 
     using AdvancedHMIDrivers;
Line 44: Line 44:
 
     using System.Threading.Tasks;
 
     using System.Threading.Tasks;
 
     using System.Windows.Forms;
 
     using System.Windows.Forms;
 +
    using MfgControl.AdvancedHMI.Drivers.Common;
 
     //
 
     //
 
     namespace AdvancedHMICS
 
     namespace AdvancedHMICS
Line 49: Line 50:
 
         public partial class MainForm : Form
 
         public partial class MainForm : Form
 
         {
 
         {
             #region Initalize Comms & Properties
+
             #region PLCItemsLists
             // Always have 3 Comm Drivers (Slow, Medium, and Fast)
+
             List<PLCAddressItem> PLCDewaxer1SlowItems = new List<PLCAddressItem>();
            // It doesn't slow down your application because this all runs on a background thread
+
             List<PLCAddressItem> PLCDewaxer1MediumItems = new List<PLCAddressItem>();
            // and the comms drivers poll from the same datalink layer
+
             List<PLCAddressItem> PLCDewaxer1FastItems = new List<PLCAddressItem>();
            EthernetIPforSLCMicroCom CommSlowDewaxer1 = new EthernetIPforSLCMicroCom();
 
             EthernetIPforSLCMicroCom CommMediumDewaxer1 = new EthernetIPforSLCMicroCom();
 
             EthernetIPforSLCMicroCom CommFastDewaxer1 = new EthernetIPforSLCMicroCom();
 
            //
 
            // The following variables set the properties of the comms above in the project
 
            string CommDewaxerIPAddress = "10.60.48.138";
 
            int CommSlow = 1000; // polling every second
 
            int CommMedium = 500; // polling every half second
 
            int CommFast = 200; // polling every 200 milliseconds
 
 
             #endregion
 
             #endregion
            //
 
            #region DataSubscribers
 
            bool initalizeSub = false;
 
            // Dewaxer 1 Subscribers
 
            DataSubscriber2 DataSlowDewaxer1 = new DataSubscriber2();
 
            DataSubscriber2 DataMediumDewaxer1 = new DataSubscriber2();
 
            DataSubscriber2 DataFastDewaxer1 = new DataSubscriber2();   
 
            #endregion
 
            //
 
 
             public MainForm()
 
             public MainForm()
 
             {
 
             {
 
                 InitializeComponent();
 
                 InitializeComponent();
                 //
+
                 #region DataSubscribers (Continuation)
                 #region Set Comm & Subscriber Properties & Items
+
                #region Add PLCItems (Slow, Medium, Fast)
                 if (initalizeSub == false)
+
                #region Add PLCSlowItems
 +
                PLCDewaxer1SlowItems.Add(new PLCAddressItem("F8:1"));
 +
                PLCDewaxer1SlowItems.Add(new PLCAddressItem("F8:2"));
 +
                PLCDewaxer1SlowItems.Add(new PLCAddressItem("F8:3"));
 +
                PLCDewaxer1SlowItems.Add(new PLCAddressItem("F8:4"));
 +
                PLCDewaxer1SlowItems.Add(new PLCAddressItem("F8:5"));
 +
                #endregion
 +
                #region Add PLCMediumItems
 +
                PLCDewaxer1MediumItems.Add(new PLCAddressItem("C5:1.ACC"));
 +
                PLCDewaxer1MediumItems.Add(new PLCAddressItem("C5:2.ACC"));
 +
                PLCDewaxer1MediumItems.Add(new PLCAddressItem("C5:3.ACC"));
 +
                #endregion
 +
                 #region Add PLCFastItems
 +
                 PLCDewaxer1FastItems.Add(new PLCAddressItem("N7:1"));
 +
                PLCDewaxer1FastItems.Add(new PLCAddressItem("N7:2"));
 +
                PLCDewaxer1FastItems.Add(new PLCAddressItem("N7:3"));
 +
                #endregion
 +
                #endregion
 +
                // bind the PLCAddressItems to the Subscribers
 +
                for (var i = 0; i < PLCDewaxer1SlowItems.Count(); i ++)
 +
                {
 +
                    DataDewax1Slow.PLCAddressValueItems.Add(PLCDewaxer1SlowItems[i]);
 +
                }
 +
 
 +
                for (var i = 0; i < PLCDewaxer1MediumItems.Count(); i++)
 
                 {
 
                 {
                     #region Set IP Addresses, Poll Rates, And Subscription Comms
+
                     DataDewax1Medium.PLCAddressValueItems.Add(PLCDewaxer1MediumItems[i]);
                    // Dewaxer Comms
 
                    CommSlowDewaxer1.IPAddress = CommDewaxerIPAddress;
 
                    CommSlowDewaxer1.PollRateOverride = CommSlow;
 
                    //
 
                    CommMediumDewaxer1.IPAddress = CommDewaxerIPAddress;
 
                    CommMediumDewaxer1.PollRateOverride = CommMedium;
 
                    //
 
                    CommFastDewaxer1.IPAddress = CommDewaxerIPAddress;
 
                    CommFastDewaxer1.PollRateOverride = CommFast;
 
                    //
 
                    // Bind Dewaxer Data Subscribers to Comms
 
                    DataSlowDewaxer1.ComComponent = CommSlowDewaxer1;
 
                    DataMediumDewaxer1.ComComponent = CommMediumDewaxer1;
 
                    DataFastDewaxer1.ComComponent = CommFastDewaxer1;
 
                    #endregion
 
                    //
 
                    #region DataSubscribers (Continuation)
 
                    // NOTE: This is two different ways of adding elements programmatically
 
                    //
 
                    // Example 1   
 
                    // This is one way of adding individual tags to the data subscriber during creation of the project
 
                    // and then adding these items to the subscriber in the form
 
                    // NOTE: You can create these items during runtime as well (like with a button press)
 
                    PLCAddressItem Dewaxer1Temperature = new PLCAddressItem("N7:10", 1, "D1Temp", "Dewax 1 Temperature", 1, 1);
 
                    PLCAddressItem Dewaxer2Temperature = new PLCAddressItem("N7:11", 1, "D2Temp", "Dewax 2 Temperature", 1, 1);
 
                    //
 
                    // Bind the PLC elements to the subscriber
 
                    DataSlowDewaxer1.PLCAddressValueItems.Add(Dewaxer1Temperature);
 
                    DataSlowDewaxer1.PLCAddressValueItems.Add(Dewaxer2Temperature);
 
                    //
 
                    // Example 2
 
                    // This is the other way. Create an array of a set number and then add in the form
 
                    PLCAddressItem[] DewaxerSlow = new PLCAddressItem[2]; // 2 item array (0 index)
 
                    //
 
                    // Initalize all of the PLC elements
 
                    for (int i = 0; i < DewaxerSlow.Count(); i++)
 
                    {
 
                        DewaxerSlow[i] = new PLCAddressItem();
 
                    }
 
                    //
 
                    // Set the PLC elements
 
                    DewaxerSlow[0].PLCAddress = "N7:0"; // Actual Address or tag name in the processor
 
                    DewaxerSlow[0].Description = "Example";
 
                    DewaxerSlow[0].Name = "Test"; //
 
                    DewaxerSlow[0].ScaleOffset = 2; // This will offset the PLC value by plus 2 (NOTE: may use this if a sensor value is reading constantly offset)
 
                    DewaxerSlow[0].ScaleFactor = .50; // This will divide the PLC value by half (NOTE: may use this if you have to convert to a percentage)
 
                    //
 
                    DewaxerSlow[1].PLCAddress = "N7:2";
 
                    DewaxerSlow[1].Description = "Example2";
 
                    DewaxerSlow[1].Name = "Test2";
 
                    DewaxerSlow[1].ScaleOffset = 2;
 
                    DewaxerSlow[1].ScaleFactor = .50;
 
                    //
 
                    // add all of the PLC elements to the subscriber
 
                    for (var i = 0; i < DewaxerSlow.Count(); i++)
 
                    {
 
                        DataSlowDewaxer1.PLCAddressValueItems.Add(DewaxerSlow[i]);
 
                    }
 
                    //
 
                    // Subscriptions have been initalized
 
                    initalizeSub = true;
 
                    #endregion
 
 
                 }
 
                 }
 +
 +
                for (var i = 0; i < PLCDewaxer1FastItems.Count(); i++)
 +
                {
 +
                    DataDewax1Fast.PLCAddressValueItems.Add(PLCDewaxer1FastItems[i]);
 +
                }         
 
                 #endregion
 
                 #endregion
 
             }
 
             }
            #region DataSubscribers (Set Data)
+
             private void DataDewax1Slow_DataChanged(object sender, PlcComEventArgs e)
            #region Slow Subscriptions
 
             private void DataSlowDewaxer1_DataChanged(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
 
 
             {
 
             {
                 if (e.ErrorId == 0)
+
                 if (e.ErrorId == 0 && e.Values != null && e.Values.Count() > 0)
 
                 {
 
                 {
                     if (e.PlcAddress == "N7")
+
                     try
 
                     {
 
                     {
                         // map the value of this
+
                         if (e.PlcAddress == "F8:1")
                         // lbl_example.Text = e.Values[0];                  
+
                        {
 +
                            label1.Text = e.Values[0];
 +
                        }
 +
                        if (e.PlcAddress == "F8:2")
 +
                         {
 +
                            label2.Text = e.Values[0];
 +
                        }
 +
                        if (e.PlcAddress == "F8:3")
 +
                        {
 +
                            label3.Text = e.Values[0];
 +
                        }
 +
                        if (e.PlcAddress == "F8:4")
 +
                        {
 +
                            label4.Text = e.Values[0];
 +
                        }
 +
                        if (e.PlcAddress == "F8:5")
 +
                        {
 +
                            label5.Text = e.Values[0];
 +
                        }
 
                     }
 
                     }
 +
                    catch (Exception ex)
 +
                    {
 +
                        errorMsg1.Text = "Source: " + ex.Source + " Message: " + ex.Message;
 +
                    }               
 
                 }
 
                 }
 
             }
 
             }
            #endregion
+
             private void DataDewax1Medium_DataChanged(object sender, PlcComEventArgs e)
            #region Medium Subscriptions
 
             private void DataMediumDewaxer1_DataChanged(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
 
 
             {
 
             {
                 if (e.ErrorId == 0)
+
                 if (e.ErrorId == 0 && e.Values != null && e.Values.Count() > 0)
 
                 {
 
                 {
                     if (e.PlcAddress == "N7")
+
                     try
 +
                    {
 +
                        if (e.PlcAddress == "C5:1.ACC")
 +
                        {
 +
                            label6.Text = e.Values[0];
 +
                        }
 +
                        if (e.PlcAddress == "C5:2.ACC")
 +
                        {
 +
                            label7.Text = e.Values[0];
 +
                        }
 +
                        if (e.PlcAddress == "C5:3.ACC")
 +
                        {
 +
                            label8.Text = e.Values[0];
 +
                        }
 +
                    }
 +
                    catch (Exception ex)
 
                     {
 
                     {
                         // map the value of this
+
                         errorMsg2.Text = "Source: " + ex.Source + " Message: " + ex.Message;
                        // lbl_example.Text = e.Values[0];
 
 
                     }
 
                     }
 
                 }
 
                 }
 
             }
 
             }
            #endregion
+
             private void DataDewax1Fast_DataChanged(object sender, PlcComEventArgs e)
            #region Fast Subscriptions
 
             private void DataFastDewaxer1_DataChanged(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
 
 
             {
 
             {
                 if (e.ErrorId == 0)
+
                 if (e.ErrorId == 0 && e.Values != null && e.Values.Count() > 0)
 
                 {
 
                 {
                     if (e.PlcAddress == "N7")
+
                     try
 +
                    {
 +
                        if (e.PlcAddress == "N7:1")
 +
                        {
 +
                            label9.Text = e.Values[0];
 +
                        }
 +
                        if (e.PlcAddress == "N7:2")
 +
                        {
 +
                            label10.Text = e.Values[0];
 +
                        }
 +
                        if (e.PlcAddress == "N7:3")
 +
                        {
 +
                            label11.Text = e.Values[0];
 +
                        }
 +
                    }
 +
                    catch (Exception ex)
 
                     {
 
                     {
                         // map the value of this
+
                         errorMsg3.Text = "Source: " + ex.Source + " Message: " + ex.Message;
                        // lbl_example.Text = e.Values[0];
 
 
                     }
 
                     }
 
                 }
 
                 }
             }
+
             }      
            #endregion
 
            #endregion
 
 
         }
 
         }
 
     }
 
     }

Revision as of 07:50, 17 May 2018

VB Example:

The AdvancedHMI drivers support a subscription that will automatically poll data and return the values. The DataSubscriber provides a non-code method of using this service. However, there are times when it is desired or more efficient to subscribe using code. This is an example on how to do this:


   Private SubscriptionID1 As Integer
   Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
       SubscriptionID1 = EthernetIPforCLXCom1.Subscribe("MyTag", 1, 500, AddressOf Subscription_DataReceived)
   End Sub
   Private Sub Subscription_DataReceived(sender As Object, e As Drivers.Common.PlcComEventArgs)
       If e.ErrorId=0 AndAlso e.Values IsNot Nothing AndAlso e.Values.Count>0 Then
           Label1.Text = e.Values(0)
       End If
   End Sub
   Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
       EthernetIPforCLXCom1.UnSubscribe(SubscriptionID1)
   End Sub


The rate at which data is polled is determined by the value of the PollRateOverride property.

Each driver is highly optimized for communications using subscriptions. All of the visual controls (e.g. Gauge) use the subscription mechanism to receive their data.


C# Example:

The Following is an example of how to programmatically set PLC Items to a dataSubscriber2 component

   using AdvancedHMIDrivers;
   using AdvancedHMIControls;
   using MfgControl.AdvancedHMI.Drivers;
   using System;
   using System.Collections.Generic;
   using System.ComponentModel;
   using System.Data;
   using System.Drawing;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;
   using System.Windows.Forms;
   using MfgControl.AdvancedHMI.Drivers.Common;
   //
   namespace AdvancedHMICS
   {
       public partial class MainForm : Form
       {
           #region PLCItemsLists
           List<PLCAddressItem> PLCDewaxer1SlowItems = new List<PLCAddressItem>();
           List<PLCAddressItem> PLCDewaxer1MediumItems = new List<PLCAddressItem>();
           List<PLCAddressItem> PLCDewaxer1FastItems = new List<PLCAddressItem>();
           #endregion
           public MainForm()
           {
               InitializeComponent();
               #region DataSubscribers (Continuation)
               #region Add PLCItems (Slow, Medium, Fast)
               #region Add PLCSlowItems
               PLCDewaxer1SlowItems.Add(new PLCAddressItem("F8:1"));
               PLCDewaxer1SlowItems.Add(new PLCAddressItem("F8:2"));
               PLCDewaxer1SlowItems.Add(new PLCAddressItem("F8:3"));
               PLCDewaxer1SlowItems.Add(new PLCAddressItem("F8:4"));
               PLCDewaxer1SlowItems.Add(new PLCAddressItem("F8:5"));
               #endregion
               #region Add PLCMediumItems 
               PLCDewaxer1MediumItems.Add(new PLCAddressItem("C5:1.ACC"));
               PLCDewaxer1MediumItems.Add(new PLCAddressItem("C5:2.ACC"));
               PLCDewaxer1MediumItems.Add(new PLCAddressItem("C5:3.ACC"));
               #endregion
               #region Add PLCFastItems
               PLCDewaxer1FastItems.Add(new PLCAddressItem("N7:1"));
               PLCDewaxer1FastItems.Add(new PLCAddressItem("N7:2"));
               PLCDewaxer1FastItems.Add(new PLCAddressItem("N7:3"));
               #endregion
               #endregion
               // bind the PLCAddressItems to the Subscribers
               for (var i = 0; i < PLCDewaxer1SlowItems.Count(); i ++)
               {
                   DataDewax1Slow.PLCAddressValueItems.Add(PLCDewaxer1SlowItems[i]);
               }
               for (var i = 0; i < PLCDewaxer1MediumItems.Count(); i++)
               {
                   DataDewax1Medium.PLCAddressValueItems.Add(PLCDewaxer1MediumItems[i]);
               }
               for (var i = 0; i < PLCDewaxer1FastItems.Count(); i++)
               {
                   DataDewax1Fast.PLCAddressValueItems.Add(PLCDewaxer1FastItems[i]);
               }           
               #endregion
           }
           private void DataDewax1Slow_DataChanged(object sender, PlcComEventArgs e)
           {
               if (e.ErrorId == 0 && e.Values != null && e.Values.Count() > 0)
               {
                   try
                   {
                       if (e.PlcAddress == "F8:1")
                       {
                           label1.Text = e.Values[0];
                       }
                       if (e.PlcAddress == "F8:2")
                       {
                           label2.Text = e.Values[0];
                       }
                       if (e.PlcAddress == "F8:3")
                       {
                           label3.Text = e.Values[0];
                       }
                       if (e.PlcAddress == "F8:4")
                       {
                           label4.Text = e.Values[0];
                       }
                       if (e.PlcAddress == "F8:5")
                       {
                           label5.Text = e.Values[0];
                       }
                   }
                   catch (Exception ex)
                   {
                       errorMsg1.Text = "Source: " + ex.Source + " Message: " + ex.Message;
                   }                 
               }
           }
           private void DataDewax1Medium_DataChanged(object sender, PlcComEventArgs e)
           {
               if (e.ErrorId == 0 && e.Values != null && e.Values.Count() > 0)
               {
                   try
                   {
                       if (e.PlcAddress == "C5:1.ACC")
                       {
                           label6.Text = e.Values[0];
                       }
                       if (e.PlcAddress == "C5:2.ACC")
                       {
                           label7.Text = e.Values[0];
                       }
                       if (e.PlcAddress == "C5:3.ACC")
                       {
                           label8.Text = e.Values[0];
                       }
                   }
                   catch (Exception ex)
                   {
                       errorMsg2.Text = "Source: " + ex.Source + " Message: " + ex.Message;
                   }
               }
           }
           private void DataDewax1Fast_DataChanged(object sender, PlcComEventArgs e)
           {
               if (e.ErrorId == 0 && e.Values != null && e.Values.Count() > 0)
               {
                   try
                   {
                       if (e.PlcAddress == "N7:1")
                       {
                           label9.Text = e.Values[0];
                       }
                       if (e.PlcAddress == "N7:2")
                       {
                           label10.Text = e.Values[0];
                       }
                       if (e.PlcAddress == "N7:3")
                       {
                           label11.Text = e.Values[0];
                       }
                   }
                   catch (Exception ex)
                   {
                       errorMsg3.Text = "Source: " + ex.Source + " Message: " + ex.Message;
                   }
               }
           }        
       }
   }