Example #1
0
        private static async Task <MethodResponse> SetVariableAsync(MethodRequest methodRequest, object userContext)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Received Command SetVariable");
            string data = Encoding.UTF8.GetString(methodRequest.Data);

            MyClient.WriteTag("WRITE1", Convert.ToSingle(666.0));
            MyClient.ReadTag("WRITE1");
            T0.WriteItem(Convert.ToSingle(666.0));
            T0.ReadItem();
            Console.ReadLine();
            Console.WriteLine("Value was: " + MyClient.GetTag("WRITE1")?.Value);
            Console.WriteLine("Value is change to: " + MyClient.GetTag("WRITE1")?.Value);
            Console.ReadLine();
            string result = "{\"result\":\"Executed direct method: " + methodRequest.Name + "\"}";

            Console.WriteLine(result + " || " + data);
            Console.ResetColor();
            return(await Task.FromResult(new MethodResponse(Encoding.UTF8.GetBytes(result), 200)));
        }
Example #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);
            }
        }