コード例 #1
0
 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();
 }
コード例 #2
0
        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();
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: masmeka/wfopc
        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();
        }
コード例 #4
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();
            }
        }
コード例 #5
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);
            }
        }
コード例 #6
0
 public void TestCleanup()
 {
     _server.Disconnect();
 }
コード例 #7
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();
        }