/// <summary> /// Imports the tags recursively from startTag. /// </summary> /// <param name="startTag">The startTag address indicates the start position for import. Must include scope and full nested name.</param> /// <param name="adsAddress">The ads address.</param> /// <param name="adsPort">The ads port.</param> /// <returns> /// Collection of imported tags. /// </returns> /// <exception cref="PlcCommunicationException">Cannot import tags online. Connection to PLC failed.</exception> public ICollection <Tag> ImportTags(string startTag, string adsAddress, int adsPort) { _logger.Debug(string.Format("Importing tags starting at '{0}' on plc '{1}:{2}'.", startTag, adsAddress, adsPort)); TcAdsSymbolInfo symbol; try { InitializieConnection(adsAddress, adsPort); symbol = _symbolLoader.FindSymbol(startTag); } catch (AdsErrorException e) { throw new PlcCommunicationException("Cannot import tags online. Connection to PLC failed.", adsAddress, adsPort.ToString(CultureInfo.InvariantCulture), string.Empty, e); } var tmpRootTag = new Tag(); ImportChildTagsRecursively(tmpRootTag, symbol, adsPort); return(tmpRootTag.Childs.ToList()); }
/// <summary> /// Parses an PLC array of type T. Supports pointered arrays (POINTER TO ...). /// In case of pointered array it will skipp NULL pointers and import only valid instances. /// </summary> /// <typeparam name="T"> /// Marshall type representation in .NET. See Beckhoff TwinCat 3 manual for an example. /// </typeparam> /// <param name="plcPath">The path in PLC to the array.</param> /// <param name="symbolLoader">The symbol loader instance.</param> /// <param name="twinCatClient">The adsClient instance.</param> /// <returns> /// Dictionary of imported and converted (.NET type) array elements and their pathes. /// </returns> public static IDictionary <string, T> GetArrayElementsWithPathes <T>(this TcAdsClient twinCatClient, TcAdsSymbolInfoLoader symbolLoader, string plcPath) { IDictionary <string, T> array = new Dictionary <string, T>(); TcAdsSymbolInfo arraySymbol = symbolLoader.FindSymbol(plcPath); if (arraySymbol == null) { return(array); } var stream = new AdsStream(8); var reader = new AdsBinaryReader(stream); TcAdsSymbolInfo elementSymbol = arraySymbol.FirstSubSymbol; while (elementSymbol != null) { stream.Position = 0; twinCatClient.Read(elementSymbol.IndexGroup, elementSymbol.IndexOffset, stream); var pointerValue = PlcSystem.IsX64Mode ? reader.ReadInt64() : reader.ReadInt32(); if (pointerValue != 0) { string plcArrayElementPath = elementSymbol.Name; if (elementSymbol.IsPointer) { plcArrayElementPath = string.Format("{0}^", plcArrayElementPath); } var handle = twinCatClient.CreateVariableHandle(plcArrayElementPath); try { var element = (T)twinCatClient.ReadAny(handle, typeof(T)); array.Add(plcArrayElementPath, element); } finally { twinCatClient.DeleteVariableHandle(handle); } } elementSymbol = elementSymbol.NextSymbol; } return(array); }
/// <summary> /// Parses an PLC array of type T. Supports pointered arrays (POINTER TO ...). /// In case of pointered array it will skipp NULL pointers and import only valid instances. /// </summary> /// <param name="plcPath">The path in PLC to the array.</param> /// <param name="twinCatClient">The adsClient instance.</param> /// <param name="typeOfElements">Marshall type representation in .NET. See Beckhoff TwinCat 3 manual for an example.</param> /// <returns> /// Dictionary of imported and converted (.NET type) array elements and their pathes. /// </returns> public static IEnumerable GetArrayElements(this TcAdsClient twinCatClient, string plcPath, Type typeOfElements) { var elements = new ArrayList(); TcAdsSymbolInfoLoader symbolLoader = twinCatClient.CreateSymbolInfoLoader(); TcAdsSymbolInfo arraySymbol = symbolLoader.FindSymbol(plcPath); if (arraySymbol == null) { return(elements); } var stream = new AdsStream(8); var reader = new AdsBinaryReader(stream); TcAdsSymbolInfo elementSymbol = arraySymbol.FirstSubSymbol; while (elementSymbol != null) { stream.Position = 0; twinCatClient.Read(elementSymbol.IndexGroup, elementSymbol.IndexOffset, stream); var pointerValue = PlcSystem.IsX64Mode ? reader.ReadInt64() : reader.ReadInt32(); if (pointerValue != 0) { string plcArrayElementPath = elementSymbol.Name; if (elementSymbol.IsPointer) { plcArrayElementPath = string.Format("{0}^", plcArrayElementPath); } var handle = twinCatClient.CreateVariableHandle(plcArrayElementPath); try { object element = twinCatClient.ReadAny(handle, typeOfElements); elements.Add(element); } finally { twinCatClient.DeleteVariableHandle(handle); } } elementSymbol = elementSymbol.NextSymbol; } return(elements); }
public ITcAdsSymbol FindSymbol(string symbolName) { try { ITcAdsSymbol symbol = symbolLoader.FindSymbol(symbolName); if (symbol == null) { return(null); } return(symbol); } catch (Exception err) { return(null); } }
private void btnFindSymbol_Click(object sender, System.EventArgs e) { try { ITcAdsSymbol symbol = symbolLoader.FindSymbol(tbSymbolname.Text); if (symbol == null) { MessageBox.Show("Symbol " + tbSymbolname.Text + " not found"); return; } SetSymbolInfo(symbol); } catch (Exception err) { MessageBox.Show(err.Message); } }
/// <summary> /// Reads an elements array of type TElement. Supports pointered arrays (POINTER TO ...). /// </summary> /// <typeparam name="TElement">Marshall type representation in .NET. See Beckhoff TwinCat 3 manual for an example.</typeparam> /// <param name="elementsArrayPath">The path in PLC to the array.</param> /// <returns> /// A dictionary of imported and converted (.NET type) array elements (value) sorted by their pathes (key). /// </returns> public IDictionary <string, TElement> ReadElementsArrayWithPathes <TElement>(string elementsArrayPath) { _logger.Debug(string.Format("Read elements array of type '{0}' from path '{1}' on '{2}'.", typeof(TElement).Name, elementsArrayPath, this)); IDictionary <string, TElement> array = new Dictionary <string, TElement>(); var arraySymbol = _symbolLoader.FindSymbol(elementsArrayPath); if (arraySymbol == null) { return(array); } var stream = new AdsStream(8); var reader = new AdsBinaryReader(stream); TcAdsSymbolInfo elementSymbol = arraySymbol.FirstSubSymbol; while (elementSymbol != null) { stream.Position = 0; _twinCatClient.Read(elementSymbol.IndexGroup, elementSymbol.IndexOffset, stream); var pointerValue = PlcSystem.IsX64Mode ? reader.ReadInt64() : reader.ReadInt32(); if (pointerValue != 0) { string plcArrayElementPath = elementSymbol.Name; if (elementSymbol.IsPointer) { plcArrayElementPath = string.Format("{0}^", plcArrayElementPath); } var element = ReadElement <TElement>(plcArrayElementPath); array.Add(plcArrayElementPath, element); } elementSymbol = elementSymbol.NextSymbol; } _logger.Debug(string.Format("Finished read of elements array of type '{0}' from path '{1}' on '{2}'.", typeof(TElement).Name, elementsArrayPath, this)); return(array); }
/// <summary> /// Reads a pointer value of the specified <paramref name="typeOfValue"/>. /// </summary> /// <param name="twinCatClient">The twin cat client to read from.</param> /// <param name="plcPath">The path in the PC to the element.</param> /// <param name="typeOfValue">The expected type of the value.</param> /// <returns> /// The value of the element at the specified <paramref name="plcPath"/>. /// </returns> public static object GetPointerValue(this TcAdsClient twinCatClient, string plcPath, Type typeOfValue) { TcAdsSymbolInfoLoader symbolLoader = twinCatClient.CreateSymbolInfoLoader(); TcAdsSymbolInfo symbol = symbolLoader.FindSymbol(plcPath); if (symbol == null) { return(null); } var stream = new AdsStream(8); var reader = new AdsBinaryReader(stream); stream.Position = 0; twinCatClient.Read(symbol.IndexGroup, symbol.IndexOffset, stream); var pointerValue = PlcSystem.IsX64Mode ? reader.ReadInt64() : reader.ReadInt32(); if (pointerValue != 0) { string plcArrayElementPath = symbol.Name; if (symbol.IsPointer) { plcArrayElementPath = string.Format("{0}^", plcArrayElementPath); } var handle = twinCatClient.CreateVariableHandle(plcArrayElementPath); try { return(twinCatClient.ReadAny(handle, typeOfValue)); } finally { twinCatClient.DeleteVariableHandle(handle); } } return(null); }