Subscribing to PLC data via code
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; } } } } }