Fernando
Fernando

Reputation: 646

OpcNetApi DataChange Event does not fire

I'm using the OpcNetApi to connect to an OPC Server. I am able to connect and read data without any issues, however, when data changes I need to read again to be able to see the changes, I'm not being notified of any changes event though I have added a Handler to the DataChanged event of my Subscription.

Is there anything special I need to do to get this to work?

Opc.URL url = new Opc.URL("opcda://localhost/ArchestrA.FSGateway.3");
        OpcCom.Factory factory = new OpcCom.Factory();

        using (Opc.Da.Server server = new Opc.Da.Server(factory, url)) {
            server.Connect();
            Console.WriteLine("Server LocalID = {0}", server.Locale);
            Opc.Da.SubscriptionState state = new Opc.Da.SubscriptionState();
            using (Opc.Da.Subscription group = server.CreateSubscription(new Opc.Da.SubscriptionState {
                    Name = "GROUP1",
                    UpdateRate = 40,
                    Deadband = 0,
                    Active = true
                }) as Opc.Da.Subscription) {
                // Create the itesm
                Opc.Da.Item[] items = new Opc.Da.Item[2];
                items[0] = new Opc.Da.Item { ItemName = "value1", Active = true, ClientHandle = "6S1", ActiveSpecified = true };
                items[1] = new Opc.Da.Item { ItemName = "value2", Active = true, ClientHandle = "6S2", ActiveSpecified = true };
                Opc.Da.ItemResult[] results = group.AddItems(items);
                for (int i = 0; i < results.Length; i++) {
                    items[i].ServerHandle = results[i].ServerHandle;
                }
                group.DataChanged += new Opc.Da.DataChangedEventHandler(group_DataChanged);
                group.State.Active = true;
                Opc.IRequest request;
                group.Read(group.Items, 1234, new Opc.Da.ReadCompleteEventHandler(group_ReadComplete), out request);

                // Stop when 'q' is pressed
                ConsoleKeyInfo key = new ConsoleKeyInfo();
                while (key.Key != ConsoleKey.Q) {
                    key = Console.ReadKey();
                    if (key.Key == ConsoleKey.R) {
                        group.Read(group.Items, 1234, new Opc.Da.ReadCompleteEventHandler(group_ReadComplete), out request);
                    }
                }
                group.DataChanged -= group_DataChanged;
                group.RemoveItems(items);
            }
            server.Disconnect();
        }

        static void group_DataChanged(object subscriptionHandle, object requestHandle, Opc.Da.ItemValueResult[] values) {
            Console.WriteLine("Data Changed");
            foreach (Opc.Da.ItemValueResult readResult in values) {
                Console.WriteLine("\t{0}\tvalue:{1}", readResult.ItemName, readResult.Value);
            }
            Console.WriteLine();
        }

Thank you,

JFercan

Upvotes: 1

Views: 4407

Answers (1)

Camille G.
Camille G.

Reputation: 3256

Everything you wrote seems good but could you just show us the group_DataChanged method called when at least one of the data of the subscription has been updated.

Upvotes: 1

Related Questions