Esempio n. 1
0
        static async Task Execute()
        {
            try
            {
                GetClient(new HandlerConfig().WithElement("SetVariableAsync", SetVariableAsync).Elements);

                CustomLogger logger = new CustomLogger();

                var opcConnectionString = @"opc.tcp://127.0.0.1:49320";
                MyClient = new OPCClient(opcConnectionString, "", "", logger, "Mytest2", SecurityPolicy.Basic128, MessageSecurity.Sign);

                R1 = (OPCTag)MyClient.AddTag("RAMP1", "ns=2;s=TEST_OPC.RAMP.RAMP1", typeof(float));
                T0 = (OPCTag)MyClient.AddTag("WRITE1", "ns=2;s=TEST_OPC.WRITE.WRITE1", typeof(float));
                T1 = (OPCTag)MyClient.AddTag("WRITE2", "ns=2;s=TEST_OPC.WRITE.WRITE2", typeof(float));
                T2 = (OPCTag)MyClient.AddTag("WRITE3", "ns=2;s=TEST_OPC.WRITE.WRITE3", typeof(float));

                var T3 = (OPCTag)MyClient.AddTag("Cast", "ns=2;s=TEST_OPC.WRITE.WRITESLOW1", typeof(double));
                T3.ReadItem();

                List <string> TagNameList = new List <string>();
                TagNameList.Add(T0.Name);
                TagNameList.Add(T1.Name);
                TagNameList.Add(T2.Name);
                TagNameList.Add(R1.Name);

                var baseName1 = "ns=2;s=TEST_OPC.RAMP.RAMP";
                var baseName2 = "ns=2;s=TEST_OPC.RAND.RAND";

                for (int i = 1; i <= 150; i++)
                {
                    var tagName1 = $"{baseName1}{i}";
                    MyClient.AddTag(tagName1, tagName1, typeof(float));
                    TagNameList.Add(tagName1);

                    var tagName2 = $"{baseName2}{i}";
                    MyClient.AddTag(tagName2, tagName2, typeof(int));
                    TagNameList.Add(tagName2);
                }

                while (true)
                {
                    MyClient.ReadTags(TagNameList);
                    IotMessage message = new IotMessage();
                    message.Datetime = DateTime.Now;
                    message.DeviceId = "PLC0001";
                    message.Data     = new List <Payload>();
                    foreach (var x in TagNameList)
                    {
                        double y = 0;
                        if (MyClient.GetTag(x)?.Value != null)
                        {
                            y = Double.Parse(MyClient.GetTag(x).Value.ToString());
                        }

                        message.Data.Add(new Payload()
                        {
                            datetime = DateTime.Now, name = MyClient.GetTag(x).Name, value = y
                        });
                        Console.WriteLine(MyClient.GetTag(x).Name + " | " + MyClient.GetTag(x).Value);
                    }

                    SendEvent(_deviceClient, message).GetAwaiter().GetResult();

                    Thread.Sleep(100);
                    Console.WriteLine("----");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            try
            {
                Tag_List = new ObservableCollection <OPCTag>();

                CustomLogger logger = new CustomLogger();

                var opcConnectionString = @"opc.tcp://127.0.0.1:49320";
                //var opcConnectionString = @"opc.tcp://10.0.2.170:49320";

                //var endpoint1 = OPCCoreUtils.SelectEndpointBySecurity(opcConnectionString, SecurityPolicy.Basic128, MessageSecurityMode.Sign, 10000);


                MyClient = new OPCClient(opcConnectionString, "", "", logger, "Mytest2", SecurityPolicy.Basic128, MessageSecurity.Sign);

                T  = (OPCTag)MyClient.AddTag("RAMP1", "ns=2;s=TEST_OPC.RAMP.RAMP1", typeof(float));
                T0 = (OPCTag)MyClient.AddTag("WRITE1", "ns=2;s=TEST_OPC.WRITE.WRITE1", typeof(float));
                T1 = (OPCTag)MyClient.AddTag("WRITE2", "ns=2;s=TEST_OPC.WRITE.WRITE2", typeof(float));
                T2 = (OPCTag)MyClient.AddTag("WRITE3", "ns=2;s=TEST_OPC.WRITE.WRITE3", typeof(float));
                float val         = 5;
                var   writeResult = T0.WriteItem(val);
                T0.ReadItem();

                var T3 = (OPCTag)MyClient.AddTag("CAst", "ns=2;s=TEST_OPC.WRITE.WRITESLOW1", typeof(double));
                T3.ReadItem();
                Console.WriteLine(T3.Value);

                //T0 = (OPCTag)MyClient.AddTag("Write01", "ns=4;s=MAIN.iMS0", typeof(short));
                //T0.WriteItem((short)5);

                //T0.ReadItem();

                //val = 100f;
                //T0.WriteItem(val);
                //T0.ReadItem();

                //T0.SubscribeItem(null,null, Newvalue);
                List <string> TagNameList   = new List <string>();
                List <object> TagObjectList = new List <object>();
                TagNameList.Add(T0.Name);
                TagNameList.Add(T1.Name);
                TagNameList.Add(T2.Name);
                TagObjectList.Add(101f);
                TagObjectList.Add(102f);
                TagObjectList.Add(103f);
                bool Readok = MyClient.ReadTags(TagNameList);

                bool Writeok = MyClient.WriteTags(TagNameList, TagObjectList);

                //Tag_List.Add(T);
                //Tag_List.Add(T);

                //this.DataContext = Tag_List;

                ////T.WriteItem(10);

                // Read multiple variables at once
                var baseName1 = "ns=2;s=TEST_OPC.RAMP.RAMP";
                var baseName2 = "ns=2;s=TEST_OPC.RAND.RAND";

                tagsToRead = new List <string>();
                for (int i = 1; i <= 150; i++)
                {
                    var tagName1 = $"{baseName1}{i}";
                    MyClient.AddTag(tagName1, tagName1, typeof(float));
                    tagsToRead.Add(tagName1);

                    var tagName2 = $"{baseName2}{i}";
                    MyClient.AddTag(tagName2, tagName2, typeof(int));
                    tagsToRead.Add(tagName2);
                }

                concurrentQueue = new ConcurrentQueue <Tuple <bool, long> >();

                Timer _timer = new Timer(50);
                _timer.Elapsed += _timer_Elapsed;
                _timer.Start();

                Timer _health = new Timer(1000);
                _health.Elapsed += _health_Elapsed;
                _health.Start();

                //MyClient.SubScribeTag(T, 0, 0, null);
                MyClient.SubscribeTag(T0, 0, 0, Newvalue);
                //var name = Console.ReadLine();

                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }