Author Topic: OpcDaCom + Subscription Problem  (Read 978 times)

Yendor

  • Newbie
  • *
  • Posts: 1
    • View Profile
OpcDaCom + Subscription Problem
« on: August 17, 2017, 02:34:17 AM »
Hi,
I got a problem when I try to subscribe to OPC tag data using the OpcDaCom driver.

at the moment my test program is headless i.e. no form, running version 3.99x of the aHMI package

This is the simple test code in c#:

Code: [Select]
using AdvancedHMIDrivers;
using MfgControl.AdvancedHMI.Drivers.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace opcTest
{
    class Tester
    {

        const string serverProgID = "RSLinx Remote OPC Server";
        const string topic = "PLC001";
        const string itemA = "Tag001.child001";
        const string itemB = "Tag001.child002";

        private OpcDaCom opcClient;

        public void Work()
        {

            opcClient = new OpcDaCom()
            {
                OPCServer = serverProgID,
                OPCTopic = topic
            };

            string[] opcValue = opcClient.Read(itemA, 1);
            Console.WriteLine("Tag: {0}, Value: {1} ", itemA, opcValue[0]);

            int itemAID = opcClient.Subscribe(itemA, 1, 500, opcClient_DataChange);
            Console.WriteLine("hit <return> to close...");
            Console.ReadLine();
        }

        private void opcClient_DataChange(object sender, PlcComEventArgs e)
        {
            try
            {
                Console.WriteLine(e.Values[0]);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        static void Main(string[] args)
        {
            Tester tst = new Tester();
            tst.Work();
        }
    }
}

the error I'm getting is
Code: [Select]
System.NullReferenceException occurred
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=AdvancedHMIDrivers
  StackTrace:
   at AdvancedHMIDrivers.OpcDaCom.DataChangedCallBack(Object clientHandle, Object requestHandle, ItemValueResult[] values) in D:\AdvancedHMIv399x\AdvancedHMIDrivers\OpcDaCom.vb:line 561
   at OpcCom.Da.Subscription.Callback.OnDataChange(Int32 dwTransid, Int32 hGroup, Int32 hrMasterquality, Int32 hrMastererror, Int32 dwCount, Int32[] phClientItems, Object[] pvValues, Int16[] pwQualities, FILETIME[] pftTimeStamps, Int32[] pErrors)

when debugging I can see 'm_SynchronizingObject' in Sub 'DataChangedCallBack' is Nothing i.e. 'Object reference not set to an instance of an object.'

So this mush be user error and I'm not setting up the OpcDaCom object correctly, can anyone see what I'm doing wrong?

Thanks.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5260
    • View Profile
    • AdvancedHMI
Re: OpcDaCom + Subscription Problem
« Reply #1 on: August 17, 2017, 04:58:59 AM »
OpcDaCom is designed to be used with a UI. The SynchronizingObject would be set to a form so the data returned can be returned back onto the UI thread. If you are writing an application using an OPC server without a UI, there really is no need to use AdvancedHMI at all. Just directly interface with the OPC Server.