Author Topic: Problem with subscribing via code for AllenBradleySLCMicro driver  (Read 1009 times)

nsaamanen

  • Newbie
  • *
  • Posts: 2
    • View Profile
Using v3.99a

I'm implementing a headless application that is listening to a PLC and sending back its values to a SQL database, but the subscription was throwing a NullReferenceException when I was calling the subscribe.

Here's the relevant code (C#):

Code: [Select]
        private AdvancedHMIDrivers.EthernetIPforSLCMicroCom myMicroLogix = new EthernetIPforSLCMicroCom(); 
        List<int> subscriptionReturn = new List<int>();

<snipped irrelevant code>

               myMicroLogix.IPAddress = _config.IPAddress;
                myMicroLogix.Port = int.Parse(_config.Port);  //I do want to throw an exception if there is a config problem     
                       

                //Record object will hold the items to return
                foreach (var item in _config.RegisterDictionary)
                {
                    //Subscribe to the given register with the register address (stored in value)
                   subscriptionReturn.Add(myMicroLogix.Subscribe(item.Value, 1, 2000, ReadPLC_Subscribe));

                    //Test reading the register
                    var value = myMicroLogix.Read(item.Value);

                }

<snipped irrelevant code>

        /// <summary>
        /// Subscription callback for after the PLC subscription fires.  We want to parse and send it back as a message to the actor for further processing
        /// </summary>
        /// <param name="stateInfo"></param>
        /// <param name="plcArgs"></param>
        private void ReadPLC_Subscribe(object stateInfo, PlcComEventArgs plcArgs)
        {           
            RegisterCallbackData registerCallbackData = new RegisterCallbackData(plcArgs);
            _self.Tell(registerCallbackData);
        }


Upon the subscribe, I would get a null reference error, and continually get the error.  The Read statement works fine.  Digging around I found the following post (http://advancedhmi.com/forum/index.php?topic=404.0), and updated the following code starting at line 482 in AllenBradleySLCMicro.vb to see if that would change things.

Code: [Select]
                            Dim z() As Object = {Me, x}
                            If SynchronizingObject Is Nothing Then
                                If SubscriptionList(i).dlgCallBack IsNot Nothing Then
                                    SubscriptionList(i).dlgCallBack.Invoke(Me, x)
                                End If
                            Else If DirectCast(SynchronizingObject, Windows.Forms.Control).IsHandleCreated
                                SynchronizingObject.BeginInvoke(SubscriptionList(i).dlgCallBack, z)                           
                            End If


This change made my subscription work and return data fine - not sure if this is a change you would like to put back into your code moving forward or not (not sure if this would break anything else).

One other related question - it seems that subscribe is polling the PLC, I was expecting more of a pub/sub model with the subscription where the event would only fire upon data change, but it looks like that is not the case.  Is this a correct assumption?  I can add in logic to ensure that I only pass along changes if we are polling vs. pub/sub, but wanted to see if that is the correct assumption first.

Thanks for the product, it's certainly saved me a lot of time!

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Problem with subscribing via code for AllenBradleySLCMicro driver
« Reply #1 on: November 02, 2015, 04:17:27 PM »
One other related question - it seems that subscribe is polling the PLC, I was expecting more of a pub/sub model with the subscription where the event would only fire upon data change, but it looks like that is not the case.  Is this a correct assumption?  I can add in logic to ensure that I only pass along changes if we are polling vs. pub/sub, but wanted to see if that is the correct assumption first.
Thanks for the feedback. I implemented that code change for the next release.

You are correct in that the driver does poll the PLC and fire the DataReturned every time a response is returned. This is by design. Since the SLC/Micro PLCs does not support true subscribing (at least not within any published documentation), the driver must continuously poll the PLC. There is a DataSubscriber component that adds a another layer on top of the driver and gives both a DataReturned and a DataChanged event.

nsaamanen

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Problem with subscribing via code for AllenBradleySLCMicro driver
« Reply #2 on: November 11, 2015, 04:20:17 PM »
OK - that's what I figured.  Thanks for the help and the great product!