Exemplo n.º 1
0
        public void WriteValues(string host, string serviceProgId, string groupKey, Dictionary <string, object> itemValuePairs)
        {
            OpcDaService server = GetOpcDaService(host, serviceProgId);

            if (server == null)
            {
                return;
            }

            if (server.OpcDaGroupS.ContainsKey(groupKey) == true)
            {
                OpcDaGroup       group    = server.OpcDaGroupS[groupKey];
                var              keyList  = itemValuePairs.Keys.ToList();
                List <OpcDaItem> itemList = new List <OpcDaItem>();
                keyList.ForEach(ids =>
                {
                    var daItem = group.Items
                                 .Where(a => a.ItemId == ids)
                                 .FirstOrDefault();
                    itemList.Add(daItem);
                });

                object[]  dd  = itemValuePairs.Values.ToArray();
                HRESULT[] res = group.Write(itemList, dd);

                LogHelper.Log("Write HRESULT " + res.ToString());
            }
        }
Exemplo n.º 2
0
        public static void SetTagValues(string opcServerUrl, BaseTagValueWrapper valuesWrapper)
        {
            lock (GetLockObject(opcServerUrl))
            {
                OpcDaServer server = GetOrCreateServer(opcServerUrl);
                OpcDaGroup  group  = server.Groups.FirstOrDefault(x => x.Name == "DecisionsWriteGroup");
                if (group == null)
                {
                    group = server.AddGroup("DecisionsWriteGroup");
                }

                BaseTagValue[] values = valuesWrapper.Values;
                if (values == null || values.Length == 0)
                {
                    return;
                }

                List <OpcDaItemDefinition> missing = new List <OpcDaItemDefinition>();
                foreach (BaseTagValue value in values)
                {
                    OpcDaItem item = group.Items.FirstOrDefault(x => x.ItemId == value.Path);
                    if (item == null)
                    {
                        missing.Add(new OpcDaItemDefinition {
                            ItemId = value.Path
                        });
                    }
                } // ensure that tags are in group

                if (missing.Count > 0)
                {
                    OpcDaItemResult[] addResults = group.AddItems(missing);
                    foreach (OpcDaItemResult result in addResults)
                    {
                        if (result.Error.Failed)
                        {
                            throw new Exception($"Set tag value failed: could not add tag to group");
                        }
                    }
                }

                List <OpcDaItem> items      = new List <OpcDaItem>();
                List <object>    itemValues = new List <object>();
                foreach (BaseTagValue value in values)
                {
                    OpcDaItem item = group.Items.First(x => x.ItemId == value.Path); //throw if missing
                    items.Add(item);
                    itemValues.Add(OPCAgentUtils.GetObjectValueFromTag(value));
                }
                HRESULT[] writeResults = group.Write(items, itemValues.ToArray());
                foreach (HRESULT writeResult in writeResults)
                {
                    if (writeResult.Failed)
                    {
                        throw new Exception($"Set tag value failed: Error while writing values: {writeResult.ToString()}");
                    }
                }
            }
        }
Exemplo n.º 3
0
 //Method writes data to "VAL_CALC" field of Capacity (Density) object and than - to OPC server
 internal void WriteMultiplyItems(OpcDaGroup dataGroup, object[] values)
 {
     OpcDaItem[] items = dataGroup.Items.ToArray();
     try
     {
         HRESULT[] resultsWrite = dataGroup.Write(items, values);
     }
     catch (Exception)
     {
     }
 }
        public void writeTag(string _tagName, string _tagValue, bool _sync = false)
        {
            // Create a group with items.
            if (writingGroup == null)
            {
                writingGroup = this.server.AddGroup("writingGroup");
            }

            writingGroup.IsActive = true;

            OpcDaItemDefinition def = new OpcDaItemDefinition
            {
                ItemId   = _tagName,
                IsActive = true
            };

            OpcDaItemDefinition[] definitions = { def };
            OpcDaItemResult[]     results     = writingGroup.AddItems(definitions);

            // Handle adding results.
            foreach (OpcDaItemResult result in results)
            {
            }

            OpcDaItem[] items  = { writingGroup.Items[0] };
            object[]    values = { _tagValue };

            if (_sync)
            {
                // Write values to the items synchronously.
                HRESULT[] hresults = writingGroup.Write(items, values);
            }/*
              * else
              * {
              * // Write values to the items asynchronously.
              * HRESULT[] results = await group.WriteAsync(items, values);
              * }*/
        }
        public void WriteValiesToAnItemOfAnOpcServerSynchronously()
        {
            Uri url = UrlBuilder.Build("Matrikon.OPC.Simulation.1");

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

                // Create a group with items.
                OpcDaGroup group = CreateGroupWithItems(server);

                // Write value to the item.
                OpcDaItem   item   = group.Items.FirstOrDefault(i => i.ItemId == "Bucket Brigade.Int4");
                OpcDaItem[] items  = { item };
                object[]    values = { 123 };

                HRESULT[] results = group.Write(items, values);

                // Handle write result.
                if (results[0].Failed)
                {
                    Console.WriteLine("Error writing value");
                }

                // Read and output value.
                OpcDaItemValue value = group.Read(items, OpcDaDataSource.Device)[0];
                Console.WriteLine("ItemId: {0}; Value: {1}; Quality: {2}; Timestamp: {3}",
                                  value.Item.ItemId, value.Value, value.Quality, value.Timestamp);

                // The output should be like the following:
                //   ItemId: Bucket Brigade.Int4; Value: 123; Quality: Good+Good+NotLimited; Timestamp: 04/18/2016 14:04:11 +03:00

                value.Value.Should().Be(123);
                value.Quality.Master.Should().Be(OpcDaQualityMaster.Good);
            }
        }