public void Test_Connect_Disconnect()
 {
     var serv = new OpcDaServer(UrlBuilder.Build("Matrikon.OPC.Simulation.1"));
     serv.Connect();
     serv.IsConnected.Should().BeTrue();
     serv.Disconnect();
     serv.IsConnected.Should().BeFalse();
     serv.Connect();
     serv.IsConnected.Should().BeTrue();
     serv.Disconnect();
     serv.IsConnected.Should().BeFalse();
 }
        public void Test_Connect_Disconnect()
        {
            var serv = new OpcDaServer(UrlBuilder.Build("Matrikon.OPC.Simulation.1"));

            serv.Connect();
            serv.IsConnected.Should().BeTrue();
            serv.Disconnect();
            serv.IsConnected.Should().BeFalse();
            serv.Connect();
            serv.IsConnected.Should().BeTrue();
            serv.Disconnect();
            serv.IsConnected.Should().BeFalse();
        }
Exemplo n.º 3
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);
                }
            }
        }
Exemplo n.º 4
0
        public void Should_Trace_Log_On_Sucessfully_Call_OPC_Wrapper_Methods()
        {
            //arrange
            var adapter = (CapturingLoggerFactoryAdapter)LogManager.Adapter;

            //act
            _server.Connect();

            //assert
            List <CapturingLoggerEvent> events = adapter.LoggerEvents.ToList();

            events.Should().NotBeEmpty();
            events.Should().NotContain(ev => ev.Exception != null);
            events.Should().Contain(ev => ev.RenderedMessage.Contains("Calling com method:"));
            events.Should().Contain(ev => ev.RenderedMessage.Contains("Success call com method:"));
        }
        public async Task GetAValueOfAnItemBySubscription()
        {
            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);

                // Configure subscription.
                group.ValuesChanged += OnGroupValuesChanged;
                group.UpdateRate     = TimeSpan.FromMilliseconds(100); // ValuesChanged won't be triggered if zero

                // Wait some time.
                await Task.Delay(1000);

                // The output should be like the following:
                //   ItemId: Bucket Brigade.Int4; Value: 0; Quality: Good+Good+NotLimited; Timestamp: 04/19/2016 12:41:11 +03:00
                //   ItemId: Random.Int2; Value: 0; Quality: Bad+BadOutOfService+NotLimited; Timestamp: 04/19/2016 12:41:11 +03:00
                //   ItemId: Random.Int2; Value: 41; Quality: Good+Good+NotLimited; Timestamp: 04/19/2016 12:41:13 +03:00
                //   ItemId: Random.Int2; Value: 18467; Quality: Good+Good+NotLimited; Timestamp: 04/19/2016 12:41:13 +03:00
                // ...
            }
        }
 public void TestInitialize()
 {
     var uri = UrlBuilder.Build("Matrikon.OPC.Simulation.1");
     _server = new OpcDaServer(uri);
     _server.Connect();
     _opcBrowser = new OpcDaBrowser1(_server);
 }
        public void BrowsingAnOpcDaServerLocally()
        {
            Uri url = UrlBuilder.Build("Matrikon.OPC.Simulation.1");
            using (var server = new OpcDaServer(url))
            {
                // Connect to the server first.
                server.Connect();

                // Browse elements.
                var browser = new OpcDaBrowserAuto(server);
                BrowseChildren(browser);

                // The output should be like the following:
                // #MonitorACLFile (ItemId: #MonitorACLFile, IsHint: False, IsItem: True, HasChildren: False)
                // @Clients (ItemId: @Clients, IsHint: False, IsItem: True, HasChildren: False)
                // Configured Aliases (ItemId: Configured Aliases, IsHint: False, IsItem: False, HasChildren: True)
                // Simulation Items (ItemId: Simulation Items, IsHint: False, IsItem: False, HasChildren: True)
                //   Bucket Brigade (ItemId: Bucket Brigade, IsHint: False, IsItem: False, HasChildren: True)
                //     ArrayOfReal8 (ItemId: Bucket Brigade.ArrayOfReal8, IsHint: False, IsItem: True, HasChildren: False)
                //     ArrayOfString (ItemId: Bucket Brigade.ArrayOfString, IsHint: False, IsItem: True, HasChildren: False)
                //     Boolean (ItemId: Bucket Brigade.Boolean, IsHint: False, IsItem: True, HasChildren: False)
                //     Int1 (ItemId: Bucket Brigade.Int1, IsHint: False, IsItem: True, HasChildren: False)
                //     Int2 (ItemId: Bucket Brigade.Int2, IsHint: False, IsItem: True, HasChildren: False)
                //     Int4 (ItemId: Bucket Brigade.Int4, IsHint: False, IsItem: True, HasChildren: False)
                // ...
            }
        }
        public async Task ReadValiesAsynchronously()
        {
            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);

                // Read values of items from device asynchronously.
                OpcDaItemValue[] values = await group.ReadAsync(group.Items);

                // Output values
                foreach (OpcDaItemValue value in values)
                {
                    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: 0; Quality: Good+Good+NotLimited; Timestamp: 04/18/2016 13:40:57 +03:00
                //   ItemId: Random.Int2; Value: 26500; Quality: Good+Good+NotLimited; Timestamp: 04/18/2016 13:40:57 +03:00

                values.Should().OnlyContain(v => v.Error.Succeeded);
                values.Should().OnlyContain(v => v.Quality.Master == OpcDaQualityMaster.Good);
            }
        }
        public void BrowsingAnOpcDaServerLocally()
        {
            Uri url = UrlBuilder.Build("Matrikon.OPC.Simulation.1");

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

                // Browse elements.
                var browser = new OpcDaBrowserAuto(server);
                BrowseChildren(browser);

                // The output should be like the following:
                // #MonitorACLFile (ItemId: #MonitorACLFile, IsHint: False, IsItem: True, HasChildren: False)
                // @Clients (ItemId: @Clients, IsHint: False, IsItem: True, HasChildren: False)
                // Configured Aliases (ItemId: Configured Aliases, IsHint: False, IsItem: False, HasChildren: True)
                // Simulation Items (ItemId: Simulation Items, IsHint: False, IsItem: False, HasChildren: True)
                //   Bucket Brigade (ItemId: Bucket Brigade, IsHint: False, IsItem: False, HasChildren: True)
                //     ArrayOfReal8 (ItemId: Bucket Brigade.ArrayOfReal8, IsHint: False, IsItem: True, HasChildren: False)
                //     ArrayOfString (ItemId: Bucket Brigade.ArrayOfString, IsHint: False, IsItem: True, HasChildren: False)
                //     Boolean (ItemId: Bucket Brigade.Boolean, IsHint: False, IsItem: True, HasChildren: False)
                //     Int1 (ItemId: Bucket Brigade.Int1, IsHint: False, IsItem: True, HasChildren: False)
                //     Int2 (ItemId: Bucket Brigade.Int2, IsHint: False, IsItem: True, HasChildren: False)
                //     Int4 (ItemId: Bucket Brigade.Int4, IsHint: False, IsItem: True, HasChildren: False)
                // ...
            }
        }
Exemplo n.º 10
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);
                }
            }
        }
        public void TestInitialize()
        {
            var uri = UrlBuilder.Build("Matrikon.OPC.Simulation.1");

            _server = new OpcDaServer(uri);
            _server.Connect();
            _opcBrowser = new OpcDaBrowser1(_server);
        }
Exemplo n.º 12
0
 public void TestInitialize()
 {
     _matrikon = new OpcDaServer(UrlBuilder.Build("Matrikon.OPC.Simulation.1"));
     _matrikon.Connect();
     _graybox = new OpcDaServer(UrlBuilder.Build("Graybox.Simulator.1"));
     _graybox.Connect();
     _matrikonBrowser = new OpcDaBrowser3(_matrikon);
     _grayboxBrowser  = new OpcDaBrowser3(_graybox);
 }
Exemplo n.º 13
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);*/
            }
        }
Exemplo n.º 14
0
        static void Main(string[] args)
        {
            cfg             = new OPCAgentConfig();
            cfg.Host        = ConfigurationManager.AppSettings["host"];
            cfg.OPCName     = ConfigurationManager.AppSettings["opcname"];
            cfg.Period      = Convert.ToInt32(ConfigurationManager.AppSettings["period"]);
            cfg.AssetFolder = ConfigurationManager.AppSettings["assetfolder"];

            var server = new OpcDaServer(UrlBuilder.Build(cfg.OPCName, cfg.Host));

            server.Connect();
            BuildSubcribtion(server, cfg);
            System.Threading.Thread.Sleep(cfg.Period * 10);
            server.Disconnect();
            Console.WriteLine("Press any key ....");
            Console.ReadLine();
        }
        public void CreatingAGroupWithItemsInAnOpcDaServer()
        {
            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.
                CreateGroupWithItems(server);

                server.Groups.Should().HaveCount(1);
                server.Groups[0].Name.Should().Be("MyGroup");
                server.Groups[0].Items.Should().HaveCount(2);
                server.Groups[0].Items[0].ItemId.Should().Be("Bucket Brigade.Int4");
                server.Groups[0].Items[1].ItemId.Should().Be("Random.Int2");
            }
        }
Exemplo n.º 16
0
        private static OpcDaServer GetOrCreateServer(string opcServerUrl)
        {
            if (string.IsNullOrEmpty(opcServerUrl))
            {
                throw new Exception("URL is null or empty");
            }
            OpcDaServer server;

            if (servers.TryGetValue(opcServerUrl, out server))
            {
                return(server);
            }

            server = new OpcDaServer(UrlBuilder.Build(opcServerUrl));
            server.Connect();
            servers[opcServerUrl] = server;
            return(server);
        }
        public void CreatingAGroupWithItemsInAnOpcDaServer()
        {
            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.
                CreateGroupWithItems(server);

                server.Groups.Should().HaveCount(1);
                server.Groups[0].Name.Should().Be("MyGroup");
                server.Groups[0].Items.Should().HaveCount(2);
                server.Groups[0].Items[0].ItemId.Should().Be("Bucket Brigade.Int4");
                server.Groups[0].Items[1].ItemId.Should().Be("Random.Int2");
            }
        }
        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();
        }
Exemplo n.º 19
0
        public void Init()
        {
            cfg             = new OPCAgentConfig();
            cfg.Host        = Common.GetConfig("host");
            cfg.OPCName     = Common.GetConfig("opcname");
            cfg.Period      = Convert.ToInt32(Common.GetConfig("period"));
            cfg.AssetFolder = Common.GetConfig("assetfolder");

            ErrorTags = new List <string>();

            if (!String.IsNullOrEmpty(cfg.Host))
            {
                server = new OpcDaServer(UrlBuilder.Build(cfg.OPCName, cfg.Host));
                server.Connect();
                BuildSubcribtion(server, cfg);
            }
            else
            {
                Log.Write("Cannot find host address.", LogType.ERROR);
            }
        }
Exemplo n.º 20
0
        //Подключение к OPC
        internal async Task <bool> OpcServerConnect()
        {
            await Task.Run(() =>
            {
                try
                {
                    // Connect to the server first.
                    if (!opcServer.IsConnected)
                    {
                        opcServer.Connect();
                        errorRaisedEvent.Invoke(this, new CustomEventArgs {
                            ErrorMessage = "Server - ok"
                        });
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Cant connect to OPC", "Alert", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            });

            return(opcServer.IsConnected);
        }
        public async Task WriteValiesToAnItemOfAnOpcServerAsynchronously()
        {
            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 = await group.WriteAsync(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);
            }
        }
Exemplo n.º 22
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();
            }
        }
        public async Task WriteValiesToAnItemOfAnOpcServerAsynchronously()
        {
            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 = await group.WriteAsync(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);
            }
        }
        public async Task ReadValiesAsynchronously()
        {
            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);

                // Read values of items from device asynchronously.
                OpcDaItemValue[] values = await group.ReadAsync(group.Items);

                // Output values
                foreach (OpcDaItemValue value in values)
                {
                    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: 0; Quality: Good+Good+NotLimited; Timestamp: 04/18/2016 13:40:57 +03:00
                //   ItemId: Random.Int2; Value: 26500; Quality: Good+Good+NotLimited; Timestamp: 04/18/2016 13:40:57 +03:00

                values.Should().OnlyContain(v => v.Error.Succeeded);
                values.Should().OnlyContain(v => v.Quality.Master == OpcDaQualityMaster.Good);
            }
        }
Exemplo n.º 25
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;;
        }
Exemplo n.º 26
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);
            }
        }
        public async Task GetAValueOfAnItemBySubscription()
        {
            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);

                // Configure subscription.
                group.ValuesChanged += OnGroupValuesChanged;
                group.UpdateRate = TimeSpan.FromMilliseconds(100); // ValuesChanged won't be triggered if zero

                // Wait some time.
                await Task.Delay(1000);

                // The output should be like the following:
                //   ItemId: Bucket Brigade.Int4; Value: 0; Quality: Good+Good+NotLimited; Timestamp: 04/19/2016 12:41:11 +03:00
                //   ItemId: Random.Int2; Value: 0; Quality: Bad+BadOutOfService+NotLimited; Timestamp: 04/19/2016 12:41:11 +03:00
                //   ItemId: Random.Int2; Value: 41; Quality: Good+Good+NotLimited; Timestamp: 04/19/2016 12:41:13 +03:00
                //   ItemId: Random.Int2; Value: 18467; Quality: Good+Good+NotLimited; Timestamp: 04/19/2016 12:41:13 +03:00
                // ...
            }
        }
 public void TestInitialize()
 {
     _server = new OpcDaServer(UrlBuilder.Build("Matrikon.OPC.Simulation.1"));
     _server.Connect();
 }
        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();
        }
Exemplo n.º 30
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;
        }
 public void TestInitialize()
 {
     _server = new OpcDaServer(UrlBuilder.Build("Matrikon.OPC.Simulation.1"));
     _server.Connect();
 }
Exemplo n.º 32
0
        static void Main(string[] args)
        {
            MySQL_Connect bazaDanych = new MySQL_Connect();
            int           czasOdpOPC = 5; // Czas odpytywania serwera OPC w sekundach
            string        query, ssId, varState, val;

            Console.WriteLine("Oczekiwanie na połączenie z serwerem OPC...");


            // Cykliczne odpytywanie servera OPC
            try
            {
                Uri url = UrlBuilder.Build("Matrikon.OPC.Simulation.1");
                using (var server = new OpcDaServer(url))
                {
                    // Connect to the server first.
                    server.Connect();
                    Console.WriteLine(server.GetStatus().CurrentTime);
                    Console.WriteLine(value: server.IsConnected);

                    String[]   list = { "MojaGrupa.OUTPUT", "MojaGrupa.LEVEL 1",
                                        "MojaGrupa.LEVEL 2",  "MojaGrupa.LEVEL 3",
                                        "MojaGrupa.VALVE 1",  "MojaGrupa.VALVE 2",
                                        "MojaGrupa.VALVE 3",  "MojaGrupa.VALVE 4" };// lista tagów które chcemy odczytać w kolejności zdefiniowanej w tabeli State_Space_Id
                    TimeSpan[] tab = new TimeSpan[list.Length];
                    for (int i = 0; i < tab.Length; i++)
                    {
                        tab[i] = TimeSpan.FromSeconds(1);
                    }
                    int t   = 0;
                    int sim = 0;
                    while (sim < 12)
                    {
                        OpcDaVQTE[] vqt = server.Read(list.ToList(), tab.ToList());

                        //  logowanie asynchroniczne
                        // Jezeli wartosc przekracza limit to historii zmienic variable_state_id na andon !!!
                        //
                        //

                        // logowanie cykliczne
                        if (t == czasOdpOPC)
                        {
                            for (int i = 0; i < list.Length; i++)
                            {
                                // parametry logowania
                                val = vqt[i].Value.ToString();
                                int ssIdInt = i + 1;
                                ssId = ssIdInt.ToString();
                                if (ssIdInt > 1 && ssIdInt < 5)
                                {
                                    varState = VariableState(val, ssIdInt, bazaDanych).ToString();
                                }
                                else
                                {
                                    varState = 2.ToString();
                                }
                                val   = val.Replace(',', '.');
                                query = "INSERT INTO scada.history(variable_state_id, state_space_id, event_id, value, date) VALUES(1," + ssId + ",1," + val + ", now())";
                                //query = "INSERT INTO scada.state_space (tag, name) VALUES ('Output32', 'wyjscie')";
                                //Console.WriteLine(list[i]);
                                //Console.WriteLine(vqt[i].Value.ToString());
                                //Console.WriteLine(query);
                                bazaDanych.Insert(query);
                            }
                            t = 0;
                        }
                        Console.WriteLine(vqt[0].Value.ToString());
                        Console.WriteLine(vqt[0].Quality.ToString());
                        Console.WriteLine(vqt[0].Timestamp.ToString());
                        Console.WriteLine("------------------");
                        t   += 1;
                        sim += 1;
                        Thread.Sleep(1000);
                    }
                    server.Disconnect();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Cannot connect to OPC server");
                Console.WriteLine(e);
            }
            finally
            {
                //server.Disconnect();
            }
            //Console.ReadKey();
        }
 public void TestInitialize()
 {
     _matrikon = new OpcDaServer(UrlBuilder.Build("Matrikon.OPC.Simulation.1"));
     _matrikon.Connect();
     _graybox = new OpcDaServer(UrlBuilder.Build("Graybox.Simulator.1"));
     _graybox.Connect();
     _matrikonBrowser = new OpcDaBrowser3(_matrikon);
     _grayboxBrowser = new OpcDaBrowser3(_graybox);
 }