Author Topic: Subscribing Data question  (Read 2482 times)

Jesse

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Subscribing Data question
« on: May 02, 2014, 05:31:35 PM »
I noticed on this topic - ( http://advancedhmi.com/forum/index.php?topic=238.0 ) thats subscribing data instead of reading data on a timer is a much better way to go because its mores responsive due to running on a background thread. But in the example of the subscription code Archie responded that you need to save of the subsciption IDs so you can unsubscribe to them when the form closes.

So my question...
If I have a tag in my controllogix called Furnace_Name  (the tag would be in the controller scope)

How would the code look to subscribe the String data in that tag and put it in a variable, then when the form closes unsubscribe that?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Subscribing Data question
« Reply #1 on: May 03, 2014, 08:45:45 AM »
The DataSubscriber component was designed to encapsulate the complexities of subscribing. But if you want to manual subscribe, this is a short example:

Code: [Select]
    '* Create a variable to hold the suscriptionID for later unsubscribing
    Private FurnaceNameID As Integer
    Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        '* Create the subscription to the tag
        FurnaceNameID = EthernetIPforCLXCom1.Subscribe("Furnace_Name", 1, 500, AddressOf SubscriptionDataReceived)
    End Sub

    Private Sub MainForm_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        '* Stop the updating of this tag and Release the resources
        EthernetIPforCLXCom1.UnSubscribe(FurnaceNameID)
    End Sub

    Private Sub SubscriptionDataReceived(sender As System.Object, e As MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs)
        '* Show the data that came back from the subscription
        Me.Text = e.Values(0)
    End Sub
« Last Edit: June 11, 2014, 07:12:24 AM by Archie »

lostcontrol

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Subscribing Data question
« Reply #2 on: June 11, 2014, 04:08:27 AM »
Would you recommend the component example code as provided?
I tried the example, but with not being connected to PLC yet & error is thrown. How should this be handled?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Subscribing Data question
« Reply #3 on: June 11, 2014, 07:17:04 AM »
If you look at the code in any of the controls (e.g. BasicLabel), you can see a full implementation of how to subscribe, check for errors, and retry until successful, and finally unsubscribe. You can see this full implementation is fairly complex, the DataSubscriber component was created to encapsulate all of this so the developer would not have to write the code.