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}"); }
public AdsPlc Init() { adsSession.Connect(); adsSession.ConnectionStateChanged += Session_ConnectionStateChanged; tags = new TagCollection(); allEnabs.AddRange(VM.Axes.SelectMany(ax => enabProps.Select(prop => tags[ax, prop]))); symbolNotificationObserver = Observer.Create <ValueChangedArgs>(OnSymbolNotification); StartMonitor(); return(this); }
/// <summary> /// Get active ports from PLC. This simply tries to establish a connection to the PLC with the starting port /// and increases that by one. After 5 ports with unsuccessful connection this function returns a list of port numbers /// </summary> /// <param name="amsIp">PLC AMS IP</param> /// <param name="startPort">Start port, increased by one after succesful connection</param> /// <returns>Array of int with active ports</returns> private static int[] GetActivePlcPorts(string amsIp, int startPort) { List <int> activePorts = new List <int>(); int port = startPort; int unsuccessfulAttempts = 0; while (true) { if (unsuccessfulAttempts >= 5 || activePorts.Count >= 25) { break; } using (AdsSession session = new AdsSession(new AmsAddress($"{amsIp}:{port}"))) { session.Connect(); try { StateInfo stateInfo = session.Connection.ReadState(); if (stateInfo.AdsState != AdsState.Invalid) { activePorts.Add(port); } else { unsuccessfulAttempts++; } session.Disconnect(); } catch (Exception ex) { Debug.WriteLine(ex.Message); unsuccessfulAttempts++; } } port++; } return(activePorts.ToArray()); }
public void CopyPasteTestStatic() { Console.WriteLine(string.Empty); Console.WriteLine("Press [Enter] for start:"); // logger.Active = false; Stopwatch stopper = new Stopwatch(); stopper.Start(); using (AdsSession session = new AdsSession(new AmsNetId("164.4.4.112.1.1"), 853)) { // client.Synchronize = false; // Connect the AdsClient to the device target. var connection = session.Connect(); // Creates the Symbol Objects as hierarchical tree SymbolLoaderSettings settings = new SymbolLoaderSettings(SymbolsLoadMode.VirtualTree, ValueAccessMode.IndexGroupOffsetPreferred); var symbolLoader = session.SymbolServer.Symbols; // SymbolLoaderFactory.Create(client, settings); //// Dump Datatypes from Target Device // Console.WriteLine(string.Format("Dumping '{0}' DataTypes:", symbolLoader.DataTypes.Count)); // foreach (IDataType type in symbolLoader.DataTypes) // { // Console.WriteLine(type); // } // Console.WriteLine(""); // Dump Symbols from target device Console.WriteLine("Dumping '{0}' Symbols:", symbolLoader.Count); WriteSymbolTree(symbolLoader, (AdsConnection)connection); } stopper.Stop(); TimeSpan elapsed = stopper.Elapsed; Console.WriteLine(string.Empty); }
/// <summary> /// Connect to PLC /// </summary> public void Connect() { // If already connected, disconnect first if (Session != null && Connection.ConnectionState == ConnectionState.Connected) { Disconnect(); } try { Session?.Dispose(); Session = new AdsSession(new AmsAddress(_address), SessionSettings.Default); Connection = (AdsConnection)Session.Connect(); _symbolLoader = SymbolLoaderFactory.Create(Connection, _symbolLoaderSettings); StateInfo stateInfo = Connection.ReadState(); AdsState state = stateInfo.AdsState; if (state == AdsState.Run || state == AdsState.Stop) { Connected = true; GetSymbols(); } else { Disconnect(); } } catch (Exception ex) { Debug.WriteLine(ex.Message); string port = Session == null ? string.Empty : $"Port {Session.Port}: "; OnPlcConnectionError(new PlcConnectionErrorEventArgs($"{port}{ex.Message}")); Disconnect(); } }
public PlcControl(AmsAddress addr) { _session = new AdsSession(addr); _connection = (IAdsConnection)_session.Connect(); }
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}"); } }