titanium-as / TitaniumAS.Opc.Client

Open source .NET client library for OPC DA
MIT License
193 stars 94 forks source link

Getting values by subscription after adding Items to group #4

Closed dergolem007 closed 7 years ago

dergolem007 commented 7 years ago

HI,

I have a group, getting values by supscription. All working fine. But after a while, i add some items to the group. Then only the some Items will be updated not all with changed values.

Denis

sorry for english

alexey-kachalov commented 7 years ago

Hi Denis. We suppose items can be not active. Check OpcDaItem.IsActive properties of you items at first. You also can check OpcDaGroup.IsActive.

dergolem007 commented 7 years ago

Hi, OpcDaGroup.IsActive is true and all OpcDaItem.IsActive are true

trkeast commented 7 years ago

Hello, Denis!

I tried to reproduce situation, but the problem is not apparent. Can you see how this code works on on your configuration?

public class OpcTest { private const string ServerUri = "Matrikon.OPC.Simulation.1"; //ProgId for matrikon simulation server private const string SubscribeItemId = "Random.Int4"; //itemId for subscribed opc item private const string GroupName = "TestGroup"; //group name for make subscription private const int UpdateRateInMilliseconds = 1000; //subscription update rate for group

    private static void Main(string[] args)
    {
        var groupState = new OpcDaGroupState
        {
            Culture = CultureInfo.CurrentCulture, //set LCID for group - some OPC servers may be sensitive for this
            IsActive = true, //only active group can be subscribed
            PercentDeadband = 0.0f, //percent deadband
            TimeBias = TimeSpan.Zero,
            UpdateRate = TimeSpan.FromMilliseconds(UpdateRateInMilliseconds),
            UserData = "some userdata" //user data
        };
        var itemDefinitions = new[]
        {
            new OpcDaItemDefinition
            {
                IsActive = true, //only active item can be subscribed
                ItemId = SubscribeItemId //itemid for opc server item
            }
        };

        Uri url = UrlBuilder.Build(ServerUri); //build url

        using (var server = new OpcDaServer(url))
        {
            // Connect to the server first.
            server.Connect();

            OpcDaGroup @group = server.AddGroup(GroupName, groupState); //add group on OPC server
            OpcDaItemResult[] results = @group.AddItems(itemDefinitions); //add items on OPC server
            foreach (OpcDaItemResult result in results) //print errors
            {
                if (result.Error.Failed)
                {
                    Console.WriteLine("Error on create item: '{0}'", result.Error);
                }
            }

            // Read all items of the group synchronously.
            OpcDaItemValue[] values = group.Read(group.Items, OpcDaDataSource.Device);
            foreach (OpcDaItemValue value in values)
            {
                Console.WriteLine("[Sync Read] ItemId: {0}; Value: {1}; Quality: {2}; Timestamp: {3}",
                    value.Item.ItemId,
                    value.Value, value.Quality, value.Timestamp);
            }

            // Read all items of the group asynchronously.
            Task<OpcDaItemValue[]> task = group.ReadAsync(group.Items);
            task.ContinueWith(result =>
            {
                OpcDaItemValue[] itemValues = result.Result;
                foreach (OpcDaItemValue value in itemValues)
                {
                    Console.WriteLine("[Async Read] ItemId: {0}; Value: {1}; Quality: {2}; Timestamp: {3}",
                        value.Item.ItemId,
                        value.Value, value.Quality, value.Timestamp);
                }
            });

            group.ValuesChanged += GroupOnValuesChanged; //subscribe on event

            Console.WriteLine("Press enter to terminate...");
            Console.ReadLine();

            group.ValuesChanged -= GroupOnValuesChanged;
            server.Disconnect();
        }
    }

    private static void GroupOnValuesChanged(object sender, OpcDaItemValuesChangedEventArgs args)
    {
        // Output values.
        foreach (OpcDaItemValue value in args.Values)
        {
            Console.WriteLine("[Subscribe] ItemId: {0}; Value: {1}; Quality: {2}; Timestamp: {3}", value.Item.ItemId,
                value.Value, value.Quality, value.Timestamp);
        }
    }

}
dergolem007 commented 7 years ago

Hi, yes that works.