Пример #1
0
 private uint CreateVariableHandle(AdsReadVariable variable)
 {
     try
     {
         return(Connection.CreateVariableHandle(variable.Name));
     }
     catch (AdsErrorException ex) when(ex.ErrorCode == AdsErrorCode.DeviceSymbolNotFound)
     {
         throw new AdsException($"Could not create handle for '{variable.Name}'.", ex);
     }
 }
Пример #2
0
        private static void ReadMultipleVariables()
        {
            using (AdsSession session = new AdsSession(new AmsAddress(NetId, Port)))
            {
                var term4Channel1 = new AdsReadVariable()
                {
                    Name = "KLEMME 4 (EL3204).RTD INPUTS CHANNEL 1", Size = 4
                };

                AdsConnection connection = (AdsConnection)session.Connect();
                var           variables  = new AdsReadVariable[]
                {
                    term4Channel1,
                    new AdsReadVariable()
                    {
                        Name = "KLEMME 4 (EL3204).RTD INPUTS CHANNEL 2", Size = 4
                    },
                    new AdsReadVariable()
                    {
                        Name = "KLEMME 3 (EL1809).CHANNEL 1", Size = 1
                    },
                    new AdsReadVariable()
                    {
                        Name = "KLEMME 3 (EL1809).CHANNEL 2", Size = 1
                    },
                    new AdsReadVariable()
                    {
                        Name = "KLEMME 3 (EL1809).CHANNEL 3", Size = 1
                    },
                    new AdsReadVariable()
                    {
                        Name = "KLEMME 3 (EL1809).CHANNEL 4", Size = 1
                    },
                };

                SumVariableRead readCommand = new SumVariableRead(connection, variables);

                for (int i = 0; i < 20; i++)
                {
                    readCommand.ReadVariables();

                    var stopwatch = Stopwatch.StartNew();
                    var values    = readCommand.ReadRaw();
                    stopwatch.Stop();

                    var bReader = new BinaryReader(new MemoryStream(values[2]));
                    Console.WriteLine($"Input 1:{bReader.ReadBoolean()}");

                    PrintValue(0, term4Channel1.RawData);
                    PrintValue(1, values[1]);
                    Console.WriteLine($"Read in {stopwatch.Elapsed.TotalMilliseconds}ms.");
                }
            }

            void PrintValue(int index, byte[] values)
            {
                var    bReader = new BinaryReader(new MemoryStream(values));
                ushort status  = bReader.ReadUInt16();
                ushort value   = bReader.ReadUInt16();

                Console.WriteLine($"Value {index + 1}: {value}, Status {status}, Toggle {(status & 0x8000) > 0}, Overrrange {(status & 0x2) > 0}");
            }
        }