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

From AdvancedHMI
Jump to: navigation, search
(C# Example:)
 
(5 intermediate revisions by the same user not shown)
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.
 +
 
 +
First you must create 3 com drivers and 3 dataSubscriber2 components that are bound to the 3 com drivers
 +
 
 +
 
 +
the example below uses 3 EthernetIPforSLCMicroCom components with poll rate overrides of:
 +
 
 +
Slow: 1000; (CommDewax1Slow)
 +
 
 +
Medium: 500; (CommDewax1Medium)
 +
 
 +
Fast: 100; (CommDewax1Fast)
 +
 
 +
 
 +
Be sure to set the "DataChanged" Event handler to the custom methods below
  
 
     using AdvancedHMIDrivers;
 
     using AdvancedHMIDrivers;
Line 44: Line 58:
 
     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
 +
    {
 +
        public partial class MainForm : Form
 
         {
 
         {
             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()
 
             {
 
             {
                 #region Initalize Comms & Properties
+
                InitializeComponent();
                 // Always have 3 Comm Drivers (Slow, Medium, and Fast)
+
                 #region DataSubscribers (Continuation)
                 // It doesn't slow down your application because this all runs on a background thread
+
                 #region Add PLCItems (Slow, Medium, Fast)
                 // and the comms drivers poll from the same datalink layer
+
                 #region Add PLCSlowItems
                 EthernetIPforSLCMicroCom CommSlowDewaxer1 = new EthernetIPforSLCMicroCom();
+
                 PLCDewaxer1SlowItems.Add(new PLCAddressItem("F8:1"));
                 EthernetIPforSLCMicroCom CommMediumDewaxer1 = new EthernetIPforSLCMicroCom();
+
                PLCDewaxer1SlowItems.Add(new PLCAddressItem("F8:2"));
                 EthernetIPforSLCMicroCom CommFastDewaxer1 = new EthernetIPforSLCMicroCom();
+
                 PLCDewaxer1SlowItems.Add(new PLCAddressItem("F8:3"));
                 //
+
                 PLCDewaxer1SlowItems.Add(new PLCAddressItem("F8:4"));
                 // The following variables set the properties of the comms above in the project
+
                 PLCDewaxer1SlowItems.Add(new PLCAddressItem("F8:5"));
                 string CommDewaxerIPAddress = "192.168.10.1";
+
                 #endregion
                 int CommDewaxerSlow = 1000; // polling every second
+
                #region Add PLCMediumItems
                 int CommDewaxerMedium = 500; // polling every half second
+
                 PLCDewaxer1MediumItems.Add(new PLCAddressItem("C5:1.ACC"));
                 int CommDewaxerFast = 200; // polling every 200 milliseconds
+
                 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
                //
 
                #region DataSubscribers
 
                bool initalizeSub = false;
 
                // Dewaxer 1 Slow Subscriber
 
                DataSubscriber2 DataSlowDewaxer1 = new DataSubscriber2();
 
                DataSubscriber2 DataMediumDewaxer1 = new DataSubscriber2();
 
                DataSubscriber2 DataFastDewaxer1 = new DataSubscriber2();
 
                //
 
                // NOTE: There are two different examples 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
 
                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);
 
                //
 
                // Example 2
 
                // This is the other way. Create an array of a set number and then add in the form
 
                PLCAddressItem[] DewaxerSlow = new PLCAddressItem[1]; // 2 item array (0 index)
 
 
                 #endregion
 
                 #endregion
                 //
+
                 // bind the PLCAddressItems to the Subscribers
                 public MainForm()
+
                 for (var i = 0; i < PLCDewaxer1SlowItems.Count(); i ++)
 +
                {
 +
                    DataDewax1Slow.PLCAddressValueItems.Add(PLCDewaxer1SlowItems[i]);
 +
                }
 +
                for (var i = 0; i < PLCDewaxer1MediumItems.Count(); i++)
 
                 {
 
                 {
                     InitializeComponent();
+
                     DataDewax1Medium.PLCAddressValueItems.Add(PLCDewaxer1MediumItems[i]);
                    //
 
                    #region Set Comm & Subscriber Properties & Items
 
                    if (initalizeSub == false)
 
                    {
 
                        #region Set IP Addresses, Poll Rates, And Subscription Comms
 
                        // Dewaxer Comms
 
                        CommSlowDewaxer1.IPAddress = CommDewaxerIPAddress;
 
                        CommSlowDewaxer1.PollRateOverride = CommDewaxerSlow;
 
                        //
 
                        CommMediumDewaxer1.IPAddress = CommDewaxerIPAddress;
 
                        CommMediumDewaxer1.PollRateOverride = CommDewaxerMedium;
 
                        //
 
                        CommFastDewaxer1.IPAddress = CommDewaxerIPAddress;
 
                        CommFastDewaxer1.PollRateOverride = CommDewaxerFast;
 
                        //
 
                        // Bind Dewaxer Data Subscribers to Comms
 
                        DataSlowDewaxer1.ComComponent = CommSlowDewaxer1;
 
                        DataMediumDewaxer1.ComComponent = CommMediumDewaxer1;
 
                        DataFastDewaxer1.ComComponent = CommFastDewaxer1;
 
                        #endregion
 
                        //
 
                        #region DataSubscribers (Continuation)
 
                        // Check to see if subscriptions have been initalized                       
 
                        // Example 1
 
                        DataSlowDewaxer1.PLCAddressValueItems.Add(Dewaxer1Temperature);
 
                        DataSlowDewaxer1.PLCAddressValueItems.Add(Dewaxer2Temperature);
 
                        //
 
                        // Example 2
 
                        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;
 
                        //
 
                        for (var i = 0; i < DewaxerSlow.Count(); i++)
 
                        {
 
                            DataSlowDewaxer1.PLCAddressValueItems.Add(DewaxerSlow[i]);
 
                        }                 
 
                        // Subscriptions have been initalized
 
                        initalizeSub = true;
 
                        #endregion
 
                    }
 
                    #endregion
 
 
                 }
 
                 }
                 //
+
                 for (var i = 0; i < PLCDewaxer1FastItems.Count(); i++)
                 #region DataSubscribers (Set Data)
+
                 {
                 #region Slow Subscriptions
+
                    DataDewax1Fast.PLCAddressValueItems.Add(PLCDewaxer1FastItems[i]);
                private void DataSlowDewaxer1_DataChanged(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
+
                }         
 +
                 #endregion
 +
            }
 +
            private void DataDewax1Slow_DataChanged(object sender, PlcComEventArgs e)
 +
            {
 +
                if (e.ErrorId == 0 && e.Values != null && e.Values.Count() > 0)
 
                 {
 
                 {
                     if (e.ErrorId == 0)
+
                     try
 
                     {
 
                     {
                         if (e.PlcAddress == "N7")
+
                         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")
 
                         {
 
                         {
                             // map the value of this
+
                             label3.Text = e.Values[0];
                             // lbl_example.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
+
            }
                #region Medium Subscriptions
+
            private void DataDewax1Medium_DataChanged(object sender, PlcComEventArgs e)
                private void DataMediumDewaxer1_DataChanged(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
+
            {
 +
                if (e.ErrorId == 0 && e.Values != null && e.Values.Count() > 0)
 
                 {
 
                 {
                     if (e.ErrorId == 0)
+
                     try
 
                     {
 
                     {
                         if (e.PlcAddress == "N7")
+
                         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")
 
                         {
 
                         {
                             // map the value of this
+
                             label8.Text = e.Values[0];
                            // lbl_example.Text = e.Values[0];
 
 
                         }
 
                         }
 +
                    }
 +
                    catch (Exception ex)
 +
                    {
 +
                        errorMsg2.Text = "Source: " + ex.Source + " Message: " + ex.Message;
 
                     }
 
                     }
 
                 }
 
                 }
                #endregion
+
            }
                #region Fast Subscriptions
+
            private void DataDewax1Fast_DataChanged(object sender, PlcComEventArgs e)
                private void DataFastDewaxer1_DataChanged(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
+
            {
 +
                if (e.ErrorId == 0 && e.Values != null && e.Values.Count() > 0)
 
                 {
 
                 {
                     if (e.ErrorId == 0)
+
                     try
 
                     {
 
                     {
                         if (e.PlcAddress == "N7")
+
                         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")
 
                         {
 
                         {
                             // map the value of this
+
                             label11.Text = e.Values[0];
                            // lbl_example.Text = e.Values[0];
 
 
                         }
 
                         }
 +
                    }
 +
                    catch (Exception ex)
 +
                    {
 +
                        errorMsg3.Text = "Source: " + ex.Source + " Message: " + ex.Message;
 
                     }
 
                     }
 
                 }
 
                 }
                #endregion
+
             }      
                #endregion
 
             }
 
 
         }
 
         }
 +
    }

Latest revision as of 09:06, 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.

First you must create 3 com drivers and 3 dataSubscriber2 components that are bound to the 3 com drivers


the example below uses 3 EthernetIPforSLCMicroCom components with poll rate overrides of:

Slow: 1000; (CommDewax1Slow)

Medium: 500; (CommDewax1Medium)

Fast: 100; (CommDewax1Fast)


Be sure to set the "DataChanged" Event handler to the custom methods below

   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;
                   }
               }
           }        
       }
   }