예제 #1
0
        static void Main(string[] args)
        {
            // Example client
            using (var connection = new AdsConnection("127.0.0.1.1.1:851"))
            {
                if (connection.IsConnected)
                {
                    Console.WriteLine("Connection Established");

                    try
                    {
                        //read structure(simple) with no definition specified and write to console
                        var readStruct = connection.ReadStructSymbol("GVL.PlcStruct");
                        Console.WriteLine(JsonConvert.SerializeObject(readStruct));

                        //read structure(complex) with no definition specified and write to console
                        var readStructComplex = connection.ReadStructSymbol("GVL.PlcStructComplex");
                        Console.WriteLine(JsonConvert.SerializeObject(readStructComplex));

                        //var readarray = connection.ReadStructSymbol("GVL.aString");
                        //Console.WriteLine(JsonConvert.SerializeObject(readarray));

                        //connection.WritePrimitiveSymbol("GVL.aString", JsonConvert.SerializeObject(readarray));
                        connection.WriteStructSymbol("GVL.PlcStructComplex", readStructComplex);

                        // read structure data
                        var readData = connection.ReadStructSymbol <TestStruct>("GVL.PlcStruct");

                        // modify data
                        readData.iVal = readData.iVal + 1;
                        readData.fVal = readData.fVal + 1.1f;
                        readData.sVal = readData.sVal + '1';

                        // write back
                        connection.WriteStructSymbol("GVL.PlcStruct", readData);

                        // global event handler example
                        connection.OnSymbolValueChanged += Connection_OnSymbolValueChanged;
                        connection.SubscribeOnValueChange("GVL.iEventData1");
                        connection.SubscribeOnValueChange("GVL.fEventData2");

                        // custom event handler example
                        connection.SubscribeOnValueChange("GVL.iCustomEvent", Connection_CustomValueChanged);

                        // suspend to test events
                        Console.ReadLine();
                    }
                    catch (Exception Ex)
                    {
                        Console.WriteLine(Ex.Message);
                        Console.ReadLine();
                    }
                }
                else
                {
                    Console.WriteLine("Connection Failed. Check AmsNetId and Target status.");
                    Console.ReadLine();
                }
            }
        }