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#):
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.
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!