Example #1
0
        private void Form1_Load(object sender, System.EventArgs e)
        {
            try
            {
                log.Debug("Start app debugging");
                log.Info("Start timer tick- 10sec ");
                InitTimer();

                //Connecto ADS Server
                log.Info("Connecting to PLC via ADS ");
                adsClient = new TcAdsClient();
                adsClient.Connect(netidplc, 851);

                symbolLoader = adsClient.CreateSymbolInfoLoader();

                //Open connection
                log.Info("Connecting to PostgreSQL Server ");
                TestconnOpen();

                //Open connection
                log.Info("Connecting to PostgreSQL Server ");

                PrepareLiveTable();
                PrepareAccuTable();
            }
            catch (Exception err)
            {
                log.Debug(err.Message);
            }
        }
 internal ControllerConnection(TcAdsClient client)
 {
     _client           = client;
     _declarationSpace = new DeclarationSpace(this);
     _infoLoader       = _client.CreateSymbolInfoLoader();
     _infoLoader.GetFirstSymbol(true);
     _symbols = _infoLoader.GetSymbols(forceReload: true);
 }
        private void btnLoad_Click(object sender, System.EventArgs e)
        {
            treeViewSymbols.Nodes.Clear();
            //check adr info
            AmsAddress serverAddress = null;

            try
            {
                // check if port is a hex value
                if (tbAdsPort.Text.StartsWith(("0x")) || tbAdsPort.Text.StartsWith(("0X")))
                {
                    string sHexValue = tbAdsPort.Text.Substring(2);
                    serverAddress = new AmsAddress(tbNetID.Text, Int32.Parse(sHexValue, System.Globalization.NumberStyles.HexNumber));
                }
                // interpret as dec value
                else
                {
                    serverAddress = new AmsAddress(tbNetID.Text, Int32.Parse(tbAdsPort.Text));
                }
            }
            catch
            {
                MessageBox.Show("Invalid AMS NetId " + tbNetID.Text + " or Ams port " + tbAdsPort.Text + "!");
                return;
            }
            //connect
            try
            {
                //orig for just 127.0...: adsClient.Connect(Convert.ToInt32(tbAdsPort.Text));
                adsClient.Connect(serverAddress.NetId, serverAddress.Port);
                symbolLoader = adsClient.CreateSymbolInfoLoader();

                if (!cbFlat.Checked)
                {
                    TcAdsSymbolInfo symbol = symbolLoader.GetFirstSymbol(true);
                    while (symbol != null)
                    {
                        treeViewSymbols.Nodes.Add(CreateNewNode(symbol));
                        symbol = symbol.NextSymbol;
                    }
                }
                else
                {
                    foreach (TcAdsSymbolInfo symbol in symbolLoader)
                    {
                        TreeNode node = new TreeNode(symbol.Name);
                        node.Tag = symbol;
                        treeViewSymbols.Nodes.Add(node);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
 private void Form1_Load(object sender, System.EventArgs e)
 {
     try
     {
         adsClient = new TcAdsClient();
         adsClient.Connect(851);
         symbolLoader = adsClient.CreateSymbolInfoLoader();
     }
     catch (Exception err)
     {
         MessageBox.Show(err.Message);
     }
 }
Example #5
0
        /// <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);
        }
        private void InitializieConnection(string adsAddress, int adsPort)
        {
            try
            {
                _twinCatClient = new TcAdsClient();

                _twinCatClient.Connect(adsAddress, adsPort);

                _symbolLoader = _twinCatClient.CreateSymbolInfoLoader();
            }
            catch (AdsErrorException e)
            {
                throw new PlcCommunicationException("Can't establish connection to PLC", adsAddress,
                                                    adsPort.ToString(CultureInfo.InvariantCulture), string.Empty, e);
            }
        }
Example #7
0
 /// <summary>
 /// Creates and initializes the <see cref="TcAdsClient"/>.
 /// </summary>
 /// <param name="path">The connection string or path (file path or IP address depending on implementation). In case of Beckhoff this is the AdsAddress.</param>
 /// <param name="port">The ADS Port in case of Beckhoff implementation.</param>
 public void Initialize(string path, int port)
 {
     _logger.Init(GetType());
     _path = path;
     _port = port;
     try
     {
         _twinCatClient = new TcAdsClient();
         _twinCatClient.Connect(path, port);
         _symbolLoader = _twinCatClient.CreateSymbolInfoLoader();
     }
     catch (AdsErrorException e)
     {
         throw new PlcCommunicationException("Can't establish connection to PLC", path,
                                             port.ToString(CultureInfo.InvariantCulture), string.Empty, e);
     }
 }
Example #8
0
        public List <string> ReadSQLTypes()
        {
            var listSQLTags = new List <string>();
            TcAdsSymbolInfoLoader symbolLoader;

            symbolLoader = adsClient.CreateSymbolInfoLoader();

            foreach (TcAdsSymbolInfo symbol in symbolLoader)
            {
                if (symbol.Type.Contains("ST_SQL"))
                {
                    PLCvar plcVar = new PLCvar(symbol.Name, symbol.Type);

                    listSQLTags.Add(symbol.Name);
                    System.Threading.Thread.Sleep(1);
                }
            }
            return(new List <string>());
        }
Example #9
0
        /// <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);
        }
Example #10
0
        public void TestClass()
        {
            TcAdsSymbolInfoLoader loader;

            using (var beckhoffClient = new TcAdsClient())
            {
                beckhoffClient.Connect("164.4.4.112.1.1", 853); // Release-datorn
                var isConnected = beckhoffClient.IsConnected;
                loader = beckhoffClient.CreateSymbolInfoLoader();
            }

            var symbols = loader.GetSymbols(true);

            // var symbol = loader.FindSymbol("BackForthSequence.start_ui");
            // var value = beckhoffClient.ReadSymbol(symbol);
            // var value2 = beckhoffClient.ReadSymbol("BackForthSequence.start_ui", typeof(bool), true);
            // foreach (TcAdsSymbolInfo tcAdsSymbolInfo in symbols)
            // {
            //    Console.WriteLine(tcAdsSymbolInfo.Name);
            // }
        }
Example #11
0
        public void loadHierarchy(WoopsaObject root)
        {
            try
            {
                _symbolLoader     = _tcAds.CreateSymbolInfoLoader();
                _woopsaObjects    = new Dictionary <string, WoopsaObject>();
                _woopsaProperties = new Dictionary <string, WoopsaAdsProperty>();

                foreach (TcAdsSymbolInfo symbol in _symbolLoader)
                {
                    WoopsaObject      newObject   = null;
                    WoopsaAdsProperty newProperty = null;
                    TcAdsSymbolInfo   parentInfo;
                    WoopsaValueType   propertyType;
                    string[]          path = symbol.Name.Split('.');
                    string            name = path[path.Length - 1];
                    bool isProperties      = BeckhoffToWoopsaValueType(symbol.Type, out propertyType);
                    if (symbol.Parent != null)
                    {
                        parentInfo = symbol.Parent;
                        if (_woopsaObjects.ContainsKey(parentInfo.Name))
                        {
                            if (isProperties)
                            {
                                newProperty = new WoopsaAdsProperty(_woopsaObjects[parentInfo.Name], name, propertyType,
                                                                    _woopsaAdsPropertyGet, _woopsaAdsPropertySet, symbol);
                            }
                            else
                            {
                                newObject = new WoopsaObject(_woopsaObjects[parentInfo.Name], name);
                            }
                        }
                        else
                        {
                            throw new Exception("Parent WoopsaObject not found !");
                        }
                    }
                    else
                    {
                        if (isProperties)
                        {
                            newProperty = new WoopsaAdsProperty(root, name, propertyType, _woopsaAdsPropertyGet, _woopsaAdsPropertySet, symbol);
                        }
                        else
                        {
                            newObject = new WoopsaObject(root, name);
                        }
                    }

                    if (isProperties)
                    {
                        _woopsaProperties.Add(symbol.Name, newProperty);
                    }
                    else
                    {
                        _woopsaObjects.Add(symbol.Name, newObject);
                    }
                }
            }catch (Exception e)
            {
                DiagnosticWindow.AddToDebug(e.Message);
            }
            isHierarchieLoaded = true;
        }