public void Test_AddGroup()
        {
            var group = _server.AddGroup("a1");

            group.Should().NotBeNull();
            _server.Groups.Should().Contain(group).And.HaveCount(1);
            group.Name.Should().Be("a1");
            group.IsActive.Should().Be(false);
            group.Culture.Should().Be(_server.Culture);
            group.PercentDeadband.Should().Be(0.0f);
            group.UpdateRate.Should().Be(TimeSpan.FromSeconds(1));
        }
Beispiel #2
0
        //private static string server = String.Format("opcda:/{0}", DatabaseInteractor.GetServerName());

        public static object ReadTag(string serverName, string opcTag = "")
        {
            Uri    url = UrlBuilder.Build(serverName);
            object readString;

            using (var client = new OpcDaServer(url))
            {
                client.Connect();
                var g1 = client.AddGroup("g1");
                g1.IsActive = true;
                var item = new[]
                {
                    new OpcDaItemDefinition
                    {
                        ItemId   = opcTag,
                        IsActive = true
                    }
                };
                g1.AddItems(item);
                try
                {
                    readString = g1.Read(g1.Items)[0].Value;
                    return(readString);
                }
                catch (Exception e)
                {
                    log.Error(String.Format("Error reading Tag {0}", opcTag));
                    log.Error(e);
                    return(null);
                }
            }
        }
        private static OpcDaGroup CreateGroupWithItems(OpcDaServer server)
        {
            // Create a group with items.
            OpcDaGroup group = server.AddGroup("MyGroup");

            group.IsActive = true;

            var definition1 = new OpcDaItemDefinition
            {
                ItemId   = "Bucket Brigade.Int4",
                IsActive = true
            };
            var definition2 = new OpcDaItemDefinition
            {
                ItemId   = "Random.Int2",
                IsActive = true
            };

            OpcDaItemDefinition[] definitions = { definition1, definition2 };
            OpcDaItemResult[]     results     = group.AddItems(definitions);

            // Handle adding results.
            foreach (OpcDaItemResult result in results)
            {
                if (result.Error.Failed)
                {
                    Console.WriteLine("Error adding items: {0}", result.Error);
                }
            }

            return(group);
        }
Beispiel #4
0
        public static OpcDaGroup Subscribe(String groupname, OpcDaServer server, String[] tags)
        {
            var group = server.AddGroup(groupname);

            group.IsActive       = true;
            group.UpdateRate     = TimeSpan.FromMilliseconds(cfg.Period);
            group.ValuesChanged += Group_ValuesChanged;

            List <OpcDaItemDefinition> defs = new List <OpcDaItemDefinition>();

            foreach (String tag in tags)
            {
                var tagdef = new OpcDaItemDefinition {
                    ItemId = tag, IsActive = true
                };
                defs.Add(tagdef);
            }

            var results = group.AddItems(defs.ToArray());
            int idx     = 0;

            foreach (OpcDaItemResult res in results)
            {
                if (res.Error.Failed)
                {
                    Console.WriteLine("Error adding item {0}: {1}", defs[idx].ItemId, res.Error);
                }
                idx++;
            }

            return(group);
        }
Beispiel #5
0
        internal static void WriteTag(string serverName, string opcTag, object valueToWrite)
        {
            Uri url = UrlBuilder.Build(serverName);

            using (var client = new OpcDaServer(url))
            {
                client.Connect();
                var g1   = client.AddGroup("g1");
                var item = new[]
                {
                    new OpcDaItemDefinition
                    {
                        ItemId   = opcTag,
                        IsActive = true
                    }
                };
                g1.AddItems(item);
                object[] writeObject = { valueToWrite };
                try
                {
                    g1.Write(g1.Items, writeObject);
                }
                catch (Exception e)
                {
                    log.Error(String.Format("Error writing Tag {opcTag}", opcTag));
                    log.Error(e);
                }
            }
        }
Beispiel #6
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()}");
                    }
                }
            }
        }
        public void Test_MemoryLeaks()
        {
            using (var server = new OpcDaServer(UrlBuilder.Build("Matrikon.OPC.Simulation.1")))
            {
                for (var gi = 0; gi < 500; gi++)
                {
                    var group    = server.AddGroup("g" + gi);
                    var itemDefs = Enumerable.Repeat(
                        new[]
                    {
                        new OpcDaItemDefinition
                        {
                            ItemId = "Random.Int1"
                        },
                        new OpcDaItemDefinition
                        {
                            ItemId            = "Random.Int2",
                            RequestedDataType = TypeConverter.FromVarEnum(VarEnum.VT_R4)
                        }
                    }, 1000).SelectMany(t => t).ToArray();

                    var cts = new CancellationTokenSource();

                    group.ValidateItems(itemDefs);
                    group.AddItems(itemDefs);
                    group.SetActiveItems(group.Items);
                    group.SetDataTypes(group.Items, TypeConverter.FromVarEnum(VarEnum.VT_I4));
                    group.SyncItems();

                    group.IsActive = true;
                    group.SyncState();

                    // sync read
                    group.Read(group.Items);
                    group.ReadMaxAge(group.Items, TimeSpan.Zero);

                    // async read
                    Task.WaitAll(group.ReadAsync(group.Items, cts.Token), group.ReadMaxAgeAsync(group.Items,
                                                                                                TimeSpan.Zero, cts.Token),
                                 group.RefreshAsync(OpcDaDataSource.Cache, cts.Token)
                                 );

                    group.RemoveItems(group.Items.Take(group.Items.Count / 2).ToArray());

                    // sync read
                    group.Read(group.Items);
                    group.ReadMaxAge(group.Items, TimeSpan.Zero);

                    // async read
                    Task.WaitAll(group.ReadAsync(group.Items, cts.Token), group.ReadMaxAgeAsync(group.Items,
                                                                                                TimeSpan.Zero, cts.Token),
                                 group.RefreshAsync(OpcDaDataSource.Cache, cts.Token)
                                 );
                }
            }
            GC.Collect();
        }
Beispiel #8
0
        static void Main()
        {
            Bootstrap.Initialize();

            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 = server.AddGroup("MyGroup");
                group.IsActive = true;

                var definition1 = new OpcDaItemDefinition
                {
                    ItemId   = "Random.Boolean",
                    IsActive = true
                };
                var definition2 = new OpcDaItemDefinition
                {
                    ItemId   = "Random.Int1",
                    IsActive = true
                };
                var definition3 = new OpcDaItemDefinition
                {
                    ItemId   = "Random.Int2",
                    IsActive = true
                };
                OpcDaItemDefinition[] definitions = { definition1, definition2, definition3 };
                OpcDaItemResult[]     results     = group.AddItems(definitions);

                // Handle adding results.
                foreach (OpcDaItemResult result in results)
                {
                    if (result.Error.Failed)
                    {
                        Console.WriteLine("Error adding items: {0}", result.Error);
                    }
                }

                while (true)
                {
                    OpcDaItemValue[] values = group.Read(group.Items, OpcDaDataSource.Device);
                    Console.WriteLine("Random.Boolean: {0} \n Random.Int1: {1} \n Random.Int2: {2}", values[0].Value, values[1].Value, values[2].Value);
                    Thread.Sleep(1000);
                    Console.Clear();
                }


                /*int count = server.Groups.Count;
                 * Console.WriteLine(count);*/
            }
        }
Beispiel #9
0
        public static void AddOrUpdateEventGroup(string opcServerUrl, OpcEventGroup eventGroup)
        {
            lock (GetLockObject(opcServerUrl))
            {
                OpcDaServer server = GetServer(opcServerUrl);
                if (server == null)
                {
                    return;
                }

                if (eventGroup?.Paths == null || eventGroup.Paths.Length == 0)
                {
                    RemoveEventGroup(opcServerUrl, eventGroup.EventId);
                    return;
                }

                // If server isn't connected or there is no ping thread running, then we aren't listening, and can't do anything here:
                PingThread pingThread;
                if (!ServerIsConnected(opcServerUrl) || !pingThreads.TryGetValue(opcServerUrl, out pingThread) || !pingThread.IsRunning)
                {
                    return;
                }

                string     groupName = "DecisionsGroup_" + eventGroup.EventId;
                OpcDaGroup group     = server.Groups.FirstOrDefault(x => x.Name == groupName);

                if (group == null)
                {
                    group          = server.AddGroup(groupName);
                    group.IsActive = true;
                    AddItemIdsToGroup(group, eventGroup.Paths);
                    OpcDaItemValue[] values = group.Read(group.Items, OpcDaDataSource.Device);
                    Callback(opcServerUrl, eventGroup.EventId, ConvertToBaseTagValues(values)); // Send all current values in this group before subscribing to further changes
                    group.ValuesChanged += (object sender, OpcDaItemValuesChangedEventArgs e) =>
                    {
                        Callback(opcServerUrl, eventGroup.EventId, ConvertToBaseTagValues(e.Values));
                    };
                }
                else
                {
                    string[]    addedPaths   = eventGroup.Paths.Except(group.Items.Select(x => x.ItemId)).ToArray();
                    OpcDaItem[] removedItems = group.Items.Where(x => !eventGroup.Paths.Contains(x.ItemId)).ToArray();

                    AddItemIdsToGroup(group, addedPaths);
                    if (removedItems.Length > 0)
                    {
                        group.RemoveItems(removedItems); // could check return value for errors here
                    }
                }

                group.PercentDeadband = eventGroup.Deadband;
                group.UpdateRate      = TimeSpan.FromMilliseconds(eventGroup.UpdateRate > 0 ? eventGroup.UpdateRate : 100); // ValuesChanged won't be triggered if zero
            }
        }
Beispiel #10
0
 public static OpcInitialData GetInitialData(string opcServerUrl, bool valuesOnly)
 {
     lock (GetLockObject(opcServerUrl))
     {
         OpcDaServer server  = GetOrCreateServer(opcServerUrl);
         var         browser = new OpcDaBrowserAuto(server);
         OpcDaGroup  group   = server.AddGroup("DecisionsDataTypeGroup");
         OpcNode[]   nodes   = GetNodesRecursive(browser, group, null);
         //now that group has all tags, read all initial values at once:
         OpcDaItemValue[] values = group.Read(group.Items, OpcDaDataSource.Device);
         server.RemoveGroup(group);
         return(new OpcInitialData {
             Nodes = valuesOnly ? null : nodes, Values = ConvertToBaseTagValues(values)
         });
     }
 }
        public void Test_PendingRequests()
        {
            using (var server = new OpcDaServer(UrlBuilder.Build("Matrikon.OPC.Simulation.1")))
            {
                server.Connect();
                var group    = server.AddGroup("g");
                var itemDefs = Enumerable.Repeat(
                    new[]
                {
                    new OpcDaItemDefinition
                    {
                        ItemId = "Random.Int1"
                    },
                    new OpcDaItemDefinition
                    {
                        ItemId            = "Random.Int2",
                        RequestedDataType = TypeConverter.FromVarEnum(VarEnum.VT_R4)
                    }
                }, 1000).SelectMany(t => t).ToArray();
                group.AddItems(itemDefs);
                group.SetActiveItems(group.Items);
                group.IsActive     = true;
                group.UpdateRate   = TimeSpan.FromMilliseconds(10);
                group.IsSubscribed = true;
                CancellationTokenSource cts = new CancellationTokenSource();

                List <Task> tasks = new List <Task>();
                for (var i = 0; i < 100; i++)
                {
                    tasks.Add(group.ReadAsync(group.Items, cts.Token));
                    tasks.Add(group.ReadMaxAgeAsync(group.Items, TimeSpan.Zero, cts.Token));
                    tasks.Add(group.RefreshAsync(OpcDaDataSource.Device, cts.Token));
                    tasks.Add(group.RefreshMaxAgeAsync(TimeSpan.Zero, cts.Token));
                    tasks.Add(group.WriteAsync(group.Items, Enumerable.Repeat((object)1, group.Items.Count).ToArray(), cts.Token));
                    tasks.Add(group.WriteVQTAsync(group.Items, Enumerable.Repeat(new OpcDaVQT(), group.Items.Count).ToArray(), cts.Token));
                }

                Task.WaitAll(tasks.ToArray());
            }
            GC.Collect();
        }
Beispiel #12
0
        public void read_group(string opcserver, string group_name, List <string> items)
        {
            Uri url = UrlBuilder.Build(opcserver);

            using (var server = new OpcDaServer(url))
            {
                // Connect to the server first.
                server.Connect();
                // Create a group with items.
                OpcDaGroup group = server.AddGroup(group_name);
                IList <OpcDaItemDefinition> definitions = new List <OpcDaItemDefinition>();
                int i = 0;
                foreach (string id in items)
                {
                    LogHelper.Log("id: " + id);
                    var definition = new OpcDaItemDefinition
                    {
                        ItemId   = id,
                        IsActive = true
                    };
                    definitions.Insert(i++, definition);
                }
                group.IsActive = true;
                OpcDaItemResult[] results    = group.AddItems(definitions);
                OpcDaItemValue[]  itemValues = group.Read(group.Items, OpcDaDataSource.Device);
                // Handle adding results.
                JsonObject data = new JsonObject();
                foreach (OpcDaItemValue item in itemValues)
                {
                    if (item.Item != null && item.Value != null)
                    {
                        data.Add(item.Item.ItemId, item.Value);
                    }
                }
                server.Disconnect();
            }
        }
Beispiel #13
0
        protected OpcDaGroup Subscribe(String groupname, OpcDaServer server, String[] tags)
        {
            var group = server.AddGroup(groupname);

            group.IsActive       = true;
            group.UpdateRate     = TimeSpan.FromMilliseconds(cfg.Period);
            group.ValuesChanged += Group_ValuesChanged;

            List <OpcDaItemDefinition> defs = new List <OpcDaItemDefinition>();

            foreach (String tag in tags)
            {
                var tagdef = new OpcDaItemDefinition {
                    ItemId = tag, IsActive = true
                };
                defs.Add(tagdef);
            }

            var results = group.AddItems(defs.ToArray());
            int idx     = 0;

            foreach (OpcDaItemResult res in results)
            {
                if (res.Error.Failed)
                {
                    if (!res.Error.ToString().Contains("The item ID is not defined in the server address space"))
                    {
                        string err = String.Format("Error adding item {0}: {1}", defs[idx].ItemId, res.Error);
                        Log.Write(err, LogType.WARNING);
                    }
                }
                idx++;
            }

            return(group);
        }
Beispiel #14
0
        public ClientDaOPC()
        {
            Uri url = UrlBuilder.Build("Kepware.KEPServerEX.V6", "10.177.3.61");

            server = new OpcDaServer(url);
            server.Connect();
            Console.WriteLine("OPC is connect");

            var tagsIBAD = new OpcDaItemDefinition[] {
                //ibad
                //error
                //int
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.Al2O3_CH1_TMP",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.DC_01",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.DC_02",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.Homo_Epl_TMP",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.HomoEpl_Heat_1",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.HomoEpl_Heat_2",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.HomoEpl_Heat_3",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.HomoEpl_Heat_4",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.HomoEpl_Heat_5",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.LMO_Heat_1",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.LMO_Heat_2",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.LMO_Heat_3",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.LMO_Heat_4",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.LMO_Heat_5",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.LMO_TMP",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.IBAD_TMP_A",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.IBAD_TMP_B",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.IBAD_TMP_C",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.IBAD_TMP_D",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.NECK_2_3_TMP",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.NECK_3_4_TMP",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.NECK_5_6_TMP",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.RF_01",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.RF_02",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.RHEED_TMP",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.Unwind_TMP",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.Wind_TMP",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.Y2O3_TMP",
                    IsActive = true
                },
                //bool
                //status
                //float
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.System.Length",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.System.Linear_Speed",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.System.Set_Length",
                    IsActive = true
                },



                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_PLC_01.ALARM.Al2O3_CH1_TMP",
                    IsActive = true
                },
            };
            var tagsMOIKA = new OpcDaItemDefinition[] {
                new OpcDaItemDefinition()
                {
                    ItemId   = "BackhoffADS.MOIKA_PLC01.CHAMBER_ALL_PROCESS_STEP",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                //bool
                {
                    ItemId   = "BackhoffADS.MOIKA_PLC01.CHAMBER_ALL_PROCESS_RUN",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "BackhoffADS.MOIKA_PLC01.CHAMBER_ALL_PROCESS_COMPLETE",
                    IsActive = true
                },
            };
            var tagsSCADA = new OpcDaItemDefinition[] {
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_SCADA.ALARM.EGUN_01",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_SCADA.ALARM.EGUN_01",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_SCADA.System.Start_Procces",
                    IsActive = true
                },
            };
            var tagsTerm = new OpcDaItemDefinition[] {
                //string
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_TERM.Unwind_Load_cell.CurrentTape_01",
                    IsActive = true
                },
                //float
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.IBAD_TERM.Unwind_Load_cell.Position_01",
                    IsActive = true
                }
            };

            groupIBAD           = server.AddGroup("IBAD");
            groupIBAD.IsActive  = true;
            groupMOIKA          = server.AddGroup("MOIKA");
            groupMOIKA.IsActive = true;
            groupSCADA          = server.AddGroup("SCADA");
            groupSCADA.IsActive = true;
            groupTERM           = server.AddGroup("TERM");
            groupTERM.IsActive  = true;
            OpcDaItemDefinition[] definitions1 = tagsIBAD;
            OpcDaItemDefinition[] definitions2 = tagsMOIKA;
            OpcDaItemDefinition[] definitions3 = tagsSCADA;
            OpcDaItemDefinition[] definitions4 = tagsTerm;
            OpcDaItemResult[]     results      = groupIBAD.AddItems(definitions1);
            OpcDaItemResult[]     results2     = groupMOIKA.AddItems(definitions2);
            OpcDaItemResult[]     results3     = groupSCADA.AddItems(definitions3);
            OpcDaItemResult[]     results4     = groupTERM.AddItems(definitions4);
            Console.WriteLine("Group IBAD is Add");

            var time = new Timer(1000);

            time.AutoReset = true;
            time.Enabled   = true;
            time.Elapsed  += Time_Elapsed;
        }
        private static OpcDaGroup CreateGroupWithItems(OpcDaServer server)
        {
            // Create a group with items.
            OpcDaGroup group = server.AddGroup("MyGroup");
            group.IsActive = true;

            var definition1 = new OpcDaItemDefinition
            {
                ItemId = "Bucket Brigade.Int4",
                IsActive = true
            };
            var definition2 = new OpcDaItemDefinition
            {
                ItemId = "Random.Int2",
                IsActive = true
            };
            OpcDaItemDefinition[] definitions = {definition1, definition2};
            OpcDaItemResult[] results = group.AddItems(definitions);

            // Handle adding results.
            foreach (OpcDaItemResult result in results)
            {
                if (result.Error.Failed)
                    Console.WriteLine("Error adding items: {0}", result.Error);
            }

            return group;
        }
Beispiel #16
0
        public ClientDaOPC()
        {
            Uri url = UrlBuilder.Build("Kepware.KEPServerEX.V6", "10.177.3.61");

            server = new OpcDaServer(url);
            server.Connect();
            Console.WriteLine(DateTime.Now + " :OPC is connect");

            groupLeap130          = server.AddGroup("LEAP130");
            groupLeap130.IsActive = true;
            var tagsLeap130 = new OpcDaItemDefinition[]
            {
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.PLD_LEP130.OpMode",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.PLD_LEP130.OpModeCode",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.PLD_LEP130.Egy",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.PLD_LEP130.Hv",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.PLD_LEP130.CounterNewFill",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.PLD_LEP130.SideA",
                    IsActive = true
                },
            };

            groupLeap130.AddItems(tagsLeap130);
            Console.WriteLine(DateTime.Now + " Group LEAP130 is Add");

            groupLeap300          = server.AddGroup("LEAP300");
            groupLeap300.IsActive = true;
            var tagsLeap300 = new OpcDaItemDefinition[]
            {
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.PLD_LEP300.OpMode",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.PLD_LEP300.OpModeCode",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.PLD_LEP300.Egy",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.PLD_LEP300.Hv",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.PLD_LEP300.CounterNewFill",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "ModbusTCP.PLD_LEP300.SideA",
                    IsActive = true
                },
            };

            groupLeap300.AddItems(tagsLeap300);
            Console.WriteLine(DateTime.Now + " Group LEAP300 is Add");

            groupPldA          = server.AddGroup("PldA");
            groupPldA.IsActive = true;
            var tagsPldA = new OpcDaItemDefinition[]
            {
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_A_PLC_01.Alarm.AlarmReel",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_A_PLC_01.Alarm.AlarmVacuum",
                    IsActive = true
                },

                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_A_PLC_01.Speed_m_h",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_A_PLC_01.pos",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_A_PLC_01.Main_Length_m",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_A_PLC_01.Name",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_A_PLC_01.RunTimes",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_A_PLC_01.RunTimeSet",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_A_PLC_01.LengthSetting",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_A_PLC_01.PROCES_START",
                    IsActive = true
                },
            };

            groupPldA.AddItems(tagsPldA);
            Console.WriteLine(DateTime.Now + " Group PldA is Add");

            groupPldB          = server.AddGroup("PldB");
            groupPldB.IsActive = true;
            var tagsPldB = new OpcDaItemDefinition[]
            {
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_B_PLC_01.Alarm.AlarmReel",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_B_PLC_01.Alarm.AlarmVacuum",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_B_PLC_01.Speed_m_h",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_B_PLC_01.pos",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_B_PLC_01.Main_Length_m",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_B_PLC_01.Name",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_B_PLC_01.RunTimes",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_B_PLC_01.RunTimeSet",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_B_PLC_01.LengthSetting",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.PLD_Set_B_PLC_01.PROCES_START",
                    IsActive = true
                },
            };

            groupPldB.AddItems(tagsPldB);
            Console.WriteLine(DateTime.Now + " Group PldB is Add");

            groupSilver          = server.AddGroup("Silver");
            groupSilver.IsActive = true;
            var tagsSilver = new OpcDaItemDefinition[]
            {
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.SPAD_SILVER_PLC_01.Alarm.AlarmReel",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.SPAD_SILVER_PLC_01.Alarm.AlarmVacuum",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.SPAD_SILVER_PLC_01.HMI_Length_Monitor", //*1000
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.SPAD_SILVER_PLC_01.HMI_Lenth_Setting", //*1000
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.SPAD_SILVER_PLC_01.HMI_Tape_Speed_Monitor",//*10
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.SPAD_SILVER_PLC_03.Pos",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.SPAD_SILVER_PLC_03.Name",
                    IsActive = true
                },
                new OpcDaItemDefinition()
                {
                    ItemId   = "FinsTCP.SPAD_SILVER_PLC_03.PROCES_START",
                    IsActive = true
                },
            };

            groupSilver.AddItems(tagsSilver);
            Console.WriteLine(DateTime.Now + " Group Silver is Add");

            var time = new Timer(1000);

            time.AutoReset = true;
            time.Enabled   = true;
            time.Elapsed  += Time_Elapsed;;
        }
        public void Test_Culture()
        {
            var group   = _server.AddGroup("g1");
            var culture = group.Culture;

            group.Culture = CultureInfo.InvariantCulture;
            // matrikon doesn't support LCID
        }
        public void Test_PendingRequests()
        {
            using (var server = new OpcDaServer(UrlBuilder.Build("Matrikon.OPC.Simulation.1")))
            {
                server.Connect();
                var group = server.AddGroup("g");
                var itemDefs = Enumerable.Repeat(
                    new[]
                        {
                            new OpcDaItemDefinition
                            {
                                ItemId = "Random.Int1"
                            },
                            new OpcDaItemDefinition
                            {
                                ItemId = "Random.Int2",
                                RequestedDataType = TypeConverter.FromVarEnum(VarEnum.VT_R4)
                            }
                        }, 1000).SelectMany(t => t).ToArray();
                group.AddItems(itemDefs);
                group.SetActiveItems(group.Items);
                group.IsActive = true;
                group.UpdateRate = TimeSpan.FromMilliseconds(10);
                group.IsSubscribed = true;
                CancellationTokenSource cts = new CancellationTokenSource();
                
                List<Task> tasks = new List<Task>();
                for (var i = 0; i < 100; i++)
                {
                    tasks.Add(group.ReadAsync(group.Items, cts.Token));
                    tasks.Add(group.ReadMaxAgeAsync(group.Items, TimeSpan.Zero, cts.Token));
                    tasks.Add(group.RefreshAsync(OpcDaDataSource.Device, cts.Token));
                    tasks.Add(group.RefreshMaxAgeAsync(TimeSpan.Zero, cts.Token));
                    tasks.Add(group.WriteAsync(group.Items, Enumerable.Repeat((object)1, group.Items.Count).ToArray(), cts.Token));
                    tasks.Add(group.WriteVQTAsync(group.Items, Enumerable.Repeat(new OpcDaVQT(), group.Items.Count).ToArray(), cts.Token));
                }

                Task.WaitAll(tasks.ToArray());
            }
            GC.Collect();
        }
        public void Test_MemoryLeaks()
        {
            using (var server = new OpcDaServer(UrlBuilder.Build("Matrikon.OPC.Simulation.1")))
            {
                for (var gi = 0; gi < 500; gi++)
                {
                    var group = server.AddGroup("g" + gi);
                    var itemDefs = Enumerable.Repeat(
                        new[]
                        {
                            new OpcDaItemDefinition
                            {
                                ItemId = "Random.Int1"
                            },
                            new OpcDaItemDefinition
                            {
                                ItemId = "Random.Int2",
                                RequestedDataType = TypeConverter.FromVarEnum(VarEnum.VT_R4)
                            }
                        }, 1000).SelectMany(t => t).ToArray();

                    var cts = new CancellationTokenSource();

                    group.ValidateItems(itemDefs);
                    group.AddItems(itemDefs);
                    group.SetActiveItems(group.Items);
                    group.SetDataTypes(group.Items, TypeConverter.FromVarEnum(VarEnum.VT_I4));
                    group.SyncItems();

                    group.IsActive = true;
                    group.SyncState();

                    // sync read
                    group.Read(group.Items);
                    group.ReadMaxAge(group.Items, TimeSpan.Zero);

                    // async read
                    Task.WaitAll(group.ReadAsync(group.Items, cts.Token), group.ReadMaxAgeAsync(group.Items,
                        TimeSpan.Zero, cts.Token),
                        group.RefreshAsync(OpcDaDataSource.Cache, cts.Token)
                        );

                    group.RemoveItems(group.Items.Take(group.Items.Count/2).ToArray());

                    // sync read
                    group.Read(group.Items);
                    group.ReadMaxAge(group.Items, TimeSpan.Zero);

                    // async read
                    Task.WaitAll(group.ReadAsync(group.Items, cts.Token), group.ReadMaxAgeAsync(group.Items,
                        TimeSpan.Zero, cts.Token),
                        group.RefreshAsync(OpcDaDataSource.Cache, cts.Token)
                        );
                }
            }
            GC.Collect();
        }
Beispiel #20
0
        private void OpcConnect()
        {
            try
            {
                Uri url = UrlBuilder.Build("CoDeSys.OPC.DA");
                using (var server = new OpcDaServer(url))
                {
                    server.Connect();

                    OpcDaGroup group = server.AddGroup("MyGroup");
                    group.IsActive = true;

                    var definition1 = new OpcDaItemDefinition
                    {
                        ItemId   = "PLC_GW3.Application.GVL.ROLL_X",
                        IsActive = true
                    };
                    var definition2 = new OpcDaItemDefinition
                    {
                        ItemId   = "PLC_GW3.Application.GVL.PITCH_Y",
                        IsActive = true
                    };
                    var definition3 = new OpcDaItemDefinition
                    {
                        ItemId   = "PLC_GW3.Application.GVL.YAW_Z",
                        IsActive = true
                    };

                    OpcDaItemDefinition[] definitions = { definition1, definition2, definition3 };
                    OpcDaItemResult[]     results     = group.AddItems(definitions);

                    foreach (OpcDaItemResult result in results)
                    {
                        if (result.Error.Failed)
                        {
                            throw new Exception("Definicje nie weszły");
                        }
                    }

                    while (_continue)
                    {
                        try
                        {
                            OpcDaItemValue[] values = group.Read(group.Items, OpcDaDataSource.Device);
                            Dispatcher.Invoke((Action)(() =>
                            {
                                dataCont.x = Convert.ToInt32(values[0].Value) / 10;
                                dataCont.y = Convert.ToInt32(values[1].Value) / 10;
                                dataCont.z = Convert.ToInt32(values[2].Value) / 10;
                                ChartUpdate((double)dataCont.x, (double)dataCont.y, (double)dataCont.z);
                            }));
                            Thread.Sleep(80);
                        }
                        catch (NotSupportedException en)
                        {
                            _continue = false;
                            MessageBox.Show(en.Message + "\n" + en.StackTrace + "\n" + en.Source);
                        }
                    }
                    server.Disconnect();
                }
            }
            catch (Exception en)
            {
                MessageBox.Show(en.Message);
            }
        }