private static void ReadMultipleVariablesAsDynamic() { using (AdsSession session = new AdsSession(new AmsAddress(NetId, Port))) { AdsConnection connection = (AdsConnection)session.Connect(); var symbols = session.SymbolServer.Symbols; ISymbol var1 = symbols["KLEMME 4 (EL3204).RTD INPUTS CHANNEL 1"]; ISymbol var2 = symbols["KLEMME 4 (EL3204).RTD INPUTS CHANNEL 2"]; ISymbol var3 = symbols["KLEMME 4 (EL3204).RTD INPUTS CHANNEL 1.STATUS.OVERRANGE"]; var symbolsToRead = new SymbolCollection() { var1, var2, var3 }; SumSymbolRead readCommand = new SumSymbolRead(connection, symbolsToRead); for (int i = 0; i < 20; i++) { dynamic[] values = readCommand.Read(); PrintValue(0, values); PrintValue(1, values); Console.WriteLine(values[2]); Console.WriteLine(); } } void PrintValue(int index, dynamic[] values) => Console.WriteLine($"Value {index + 1}: {values[index].VALUE}, Status {values[index].STATUS}, Overrange {values[index].STATUS.OVERRANGE}"); }
private static void WriteSymbolTree(ReadOnlySymbolCollection symbolLoaderSymbols, AdsConnection connection) { Console.WriteLine("\t"); foreach (var symbolLoaderSymbol in symbolLoaderSymbols) { Console.Write(symbolLoaderSymbol.InstancePath); // var symbolInfo = client.ReadSymbolInfo(symbolLoaderSymbol.InstancePath); // var symbolInfor = new symbolin try { if (symbolLoaderSymbol is ISymbol info) { if (info.Category == DataTypeCategory.Primitive) { SymbolCollection coll = new SymbolCollection() { symbolLoaderSymbol }; SumSymbolRead readCommand = new SumSymbolRead(connection, coll); object[] values = readCommand.Read(); Console.Write(": " + values.FirstOrDefault()); } else { Console.Write(": " + info.Category); } } else { Console.Write(": -"); } } catch (Exception e) { var type = symbolLoaderSymbol.DataType.GetType(); var symbol = connection.ReadSymbol(symbolLoaderSymbol.InstanceName, symbolLoaderSymbol.DataType.GetType(), true); Console.Write("Exception: " + e); } Console.WriteLine(string.Empty); if (symbolLoaderSymbol.SubSymbols.Any()) { Console.WriteLine("__________subs to " + symbolLoaderSymbol.InstancePath); WriteSymbolTree(symbolLoaderSymbol.SubSymbols, connection); } } }
private async Task ReadAll(PlcConnection plcCon) { // Stopwatch sw = Stopwatch.StartNew(); _symbolValues.Clear(); // Split up load in packages of 500 Variables at once. More would be faster but stresses the PLC more. SymbolCollection symbolColl = new SymbolCollection(); int cnt = 0; foreach (var symbol in plcCon.Symbols) { if (cnt >= 500) { SumSymbolRead sumSymbolRead = new SumSymbolRead(plcCon.Connection, symbolColl); await Task.Run(() => { object[] symbolValuesReturn = sumSymbolRead.Read(); _symbolValues.AddRange(symbolValuesReturn); }); cnt = 0; symbolColl = new SymbolCollection(); } symbolColl.Add(symbol); cnt++; } // Final sum symbol read for the rest in symbolColl if (cnt != 0) { SumSymbolRead sumSymbolRead = new SumSymbolRead(plcCon.Connection, symbolColl); await Task.Run(() => { object[] symbolValuesReturn = sumSymbolRead.Read(); _symbolValues.AddRange(symbolValuesReturn); }); } // Debug.WriteLine($"Collecting data from PLC: {sw.Elapsed}"); }