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

From AdvancedHMI
Jump to: navigation, search
(C# Example:)
Line 45: Line 45:
 
     using System.Windows.Forms;
 
     using System.Windows.Forms;
 
     //
 
     //
        namespace AdvancedHMICS
+
    namespace AdvancedHMICS
 +
    {
 +
        public partial class MainForm : Form
 
         {
 
         {
             public partial class MainForm : Form
+
            #region Initalize Comms & Properties
 +
            // Always have 3 Comm Drivers (Slow, Medium, and Fast)
 +
            // It doesn't slow down your application because this all runs on a background thread
 +
            // and the comms drivers poll from the same datalink layer
 +
            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
 +
            //
 +
            #region DataSubscribers
 +
            bool initalizeSub = false;
 +
            // Dewaxer 1 Subscribers
 +
            DataSubscriber2 DataSlowDewaxer1 = new DataSubscriber2();
 +
            DataSubscriber2 DataMediumDewaxer1 = new DataSubscriber2();
 +
            DataSubscriber2 DataFastDewaxer1 = new DataSubscriber2();   
 +
            #endregion
 +
            //
 +
             public MainForm()
 
             {
 
             {
                 #region Initalize Comms & Properties
+
                 InitializeComponent();
                // Always have 3 Comm Drivers (Slow, Medium, and Fast)
 
                // It doesn't slow down your application because this all runs on a background thread
 
                // and the comms drivers poll from the same datalink layer
 
                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
+
                 #region Set Comm & Subscriber Properties & Items
                string CommDewaxerIPAddress = "192.168.10.1";
+
                 if (initalizeSub == false)
                int CommDewaxerSlow = 1000; // polling every second
 
                int CommDewaxerMedium = 500; // polling every half second
 
                int CommDewaxerFast = 200; // polling every 200 milliseconds
 
                #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
 
                //
 
                public MainForm()
 
 
                 {
 
                 {
                     InitializeComponent();
+
                     #region Set IP Addresses, Poll Rates, And Subscription Comms
 +
                    // Dewaxer Comms
 +
                    CommSlowDewaxer1.IPAddress = CommDewaxerIPAddress;
 +
                    CommSlowDewaxer1.PollRateOverride = CommSlow;
 +
                    //
 +
                    CommMediumDewaxer1.IPAddress = CommDewaxerIPAddress;
 +
                    CommMediumDewaxer1.PollRateOverride = CommMedium;
 
                     //
 
                     //
                     #region Set Comm & Subscriber Properties & Items
+
                    CommFastDewaxer1.IPAddress = CommDewaxerIPAddress;
                     if (initalizeSub == false)
+
                    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++)
 
                     {
 
                     {
                        #region Set IP Addresses, Poll Rates, And Subscription Comms
+
                         DewaxerSlow[i] = new PLCAddressItem();
                        // 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
 
 
                     }
 
                     }
 +
                    //
 +
                    // 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
 
                     #endregion
 
                 }
 
                 }
                 //
+
                 #endregion
                #region DataSubscribers (Set Data)
+
            }
                #region Slow Subscriptions
+
            #region DataSubscribers (Set Data)
                private void DataSlowDewaxer1_DataChanged(object sender, MfgControl.AdvancedHMI.Drivers.Common.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)
+
                     if (e.PlcAddress == "N7")
 
                     {
 
                     {
                         if (e.PlcAddress == "N7")
+
                         // map the value of this
                        {
+
                        // lbl_example.Text = e.Values[0];                  
                            // map the value of this
 
                            // lbl_example.Text = e.Values[0];
 
                        }
 
 
                     }
 
                     }
 
                 }
 
                 }
                #endregion
+
            }
                #region Medium Subscriptions
+
            #endregion
                private void DataMediumDewaxer1_DataChanged(object sender, MfgControl.AdvancedHMI.Drivers.Common.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)
+
                     if (e.PlcAddress == "N7")
 
                     {
 
                     {
                         if (e.PlcAddress == "N7")
+
                         // map the value of this
                        {
+
                        // lbl_example.Text = e.Values[0];
                            // map the value of this
 
                            // lbl_example.Text = e.Values[0];
 
                        }
 
 
                     }
 
                     }
 
                 }
 
                 }
                #endregion
+
            }
                #region Fast Subscriptions
+
            #endregion
                private void DataFastDewaxer1_DataChanged(object sender, MfgControl.AdvancedHMI.Drivers.Common.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)
+
                     if (e.PlcAddress == "N7")
 
                     {
 
                     {
                         if (e.PlcAddress == "N7")
+
                         // map the value of this
                        {
+
                        // lbl_example.Text = e.Values[0];
                            // map the value of this
 
                            // lbl_example.Text = e.Values[0];
 
                        }
 
 
                     }
 
                     }
 
                 }
 
                 }
                #endregion
 
                #endregion
 
 
             }
 
             }
 +
            #endregion
 +
            #endregion
 
         }
 
         }
 +
    }

Revision as of 14:16, 15 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 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.

   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;
   //
   namespace AdvancedHMICS
   {
       public partial class MainForm : Form
       {
           #region Initalize Comms & Properties
           // Always have 3 Comm Drivers (Slow, Medium, and Fast)
           // It doesn't slow down your application because this all runs on a background thread
           // and the comms drivers poll from the same datalink layer 
           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
           //
           #region DataSubscribers
           bool initalizeSub = false;
           // Dewaxer 1 Subscribers
           DataSubscriber2 DataSlowDewaxer1 = new DataSubscriber2();
           DataSubscriber2 DataMediumDewaxer1 = new DataSubscriber2();
           DataSubscriber2 DataFastDewaxer1 = new DataSubscriber2();     
           #endregion
           //
           public MainForm()
           {
               InitializeComponent();
               //
               #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 = 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
               }
               #endregion
           }
           #region DataSubscribers (Set Data)
           #region Slow Subscriptions
           private void DataSlowDewaxer1_DataChanged(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
           {
               if (e.ErrorId == 0)
               {
                   if (e.PlcAddress == "N7")
                   {
                       // map the value of this
                       // lbl_example.Text = e.Values[0];                    
                   }
               }
           }
           #endregion
           #region Medium Subscriptions
           private void DataMediumDewaxer1_DataChanged(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
           {
               if (e.ErrorId == 0)
               {
                   if (e.PlcAddress == "N7")
                   {
                       // map the value of this
                       // lbl_example.Text = e.Values[0];
                   }
               }
           }
           #endregion
           #region Fast Subscriptions
           private void DataFastDewaxer1_DataChanged(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
           {
               if (e.ErrorId == 0)
               {
                   if (e.PlcAddress == "N7")
                   {
                       // map the value of this
                       // lbl_example.Text = e.Values[0];
                   }
               }
           }
           #endregion
           #endregion
       }
   }