コード例 #1
0
        public void LoadSymbols()
        {
            try
            {
                using (var adsClient = new AdsClient())
                {
                    // connection
                    adsClient.Connect(new AmsAddress(_appOptions.Value.AMSNetId, _appOptions.Value.Port));

                    // get the symbol loader, FLAT mode
                    var symbolLoader = SymbolLoaderFactory.Create(adsClient, new SymbolLoaderSettings(SymbolsLoadMode.Flat));

                    if (symbolLoader != null)
                    {
                        _symbolCollection = symbolLoader.Symbols;
                        SymbolInfos       = new BindableCollection <ISymbol>(_symbolCollection);
                        NotifyOfPropertyChange(() => SymbolInfos);
                    }
                }
            }
            catch (ObjectDisposedException exDisposed)
            {
                Console.WriteLine("exception\n" + exDisposed.Message);
            }
            catch (InvalidOperationException exInvalid)
            {
                Console.WriteLine("exception\n" + exInvalid.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Undentified exception\n" + ex.Message);
            }
        }
コード例 #2
0
        private static void PrintSymbols(AdsClient client)
        {
            SymbolLoaderSettings settings = new SymbolLoaderSettings(TwinCAT.SymbolsLoadMode.VirtualTree);
            ISymbolLoader        loader   = SymbolLoaderFactory.Create(client, settings);

            var symbols = loader.Symbols;

            PrintSymbols(symbols);
        }
コード例 #3
0
        /// <summary>
        /// Constructor - establishes connection and creates symbol loader
        /// </summary>
        /// <param name="AmsNetId">ADS Client AMS Net Id "x.x.x.x.x.x:port" (127.0.0.1.1.1:851)</param>
        public AdsConnection(string AmsNetId)
        {
            _client = new TcAdsClient();

            // connect ADS client
            _client.Connect(AmsAddress.Parse(AmsNetId));

            if (_client.IsConnected)
            {
                // create symbol loader
                _symbolLoader = SymbolLoaderFactory.Create(_client,
                                                           new SymbolLoaderSettings(SymbolsLoadMode.VirtualTree));
            }
        }
コード例 #4
0
        private void UpdateSymbols(ConnectionState state)
        {
            if (state == TwinCAT.ConnectionState.Connected)
            {
                var loader = SymbolLoaderFactory.Create(Client, new SymbolLoaderSettings(SymbolsLoadMode.VirtualTree));
                TreeViewSymbols = loader.Symbols;

                var loader2 = SymbolLoaderFactory.Create(Client, new SymbolLoaderSettings(SymbolsLoadMode.Flat));
                FlatViewSymbols = loader2.Symbols;
            }
            else
            {
                TreeViewSymbols = null;
            }
        }
コード例 #5
0
        private void UpdateSymbols(AdsState state)
        {
            if (state == TwinCAT.Ads.AdsState.Run)
            {
                logger.LogDebug($"Update symbols on beckhoff change to {state}");

                var loader = SymbolLoaderFactory.Create(Client, new SymbolLoaderSettings(SymbolsLoadMode.Flat));
                symbolsSubject.OnNext(loader.Symbols);
            }
            else
            {
                logger.LogDebug($"Deleting symbols on beckhoff state change to {state}");
                symbolsSubject.OnNext(null);
            }
        }
        public async Task Should_read_symbols_from_server()
        {
            using (var serverMock = new Mock(12347, "AdsServerMock"))
            {
                serverMock.RegisterReplay(@".\TestFiles\ReadSymbolsPort851.cap");
                using (var client = new AdsClient())
                {
                    // connect to our mocking server
                    client.Connect(serverMock.ServerAddress.Port);
                    if (client.IsConnected)
                    {
                        var symbolLoader = SymbolLoaderFactory.Create(client, new SymbolLoaderSettings(SymbolsLoadMode.Flat, TcAds.ValueAccess.ValueAccessMode.Default));
                        var symbols      = await symbolLoader.GetSymbolsAsync(CancellationToken.None);

                        Assert.IsTrue(symbols.Succeeded);
                    }
                }
            }
        }
コード例 #7
0
        private void RegisterSubscriptions()
        {
            try
            {
                // create client
                _adsClient = new AdsClient();

                // connection
                _adsClient.Connect(new AmsAddress(_appOptions.AMSNetId, _appOptions.Port));

                // get the symbol loader
                _symbolLoader = SymbolLoaderFactory.Create(_adsClient, SymbolLoaderSettings.Default);

                if (_symbolLoader == null)
                {
                    return;
                }

                // symbol from appsetting.json
                if (_appOptions.ValueSymbols.Count > 0)
                {
                    for (int i = 0; i < _appOptions.ValueSymbols.Count; i++)
                    {
                        var currentValueSymbol = _appOptions.ValueSymbols[i];

                        // GetIValueSymbol
                        var res = GetIValueSymbol(_symbolLoader, currentValueSymbol);
                        if (res != null)
                        {
                            SubscribeIValueSymbol(res);
                        }
                    }
                }
            }
            catch (ObjectDisposedException ex_objectDisposed)
            {
                //throw;
            }
        }
コード例 #8
0
        /// <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();
            }
        }
コード例 #9
0
        public void CopyPasteTestDynamic()
        {
            using (TcAdsClient client = new TcAdsClient())
            {
                client.Synchronize = false;

                // Connect to the target device
                client.Connect("164.4.4.112.1.1", 853);

                // Usage of "dynamic" Type and Symbols (>= .NET4 only)
                SymbolLoaderSettings settings  = new SymbolLoaderSettings(SymbolsLoadMode.DynamicTree);
                IAdsSymbolLoader     dynLoader = (IAdsSymbolLoader)SymbolLoaderFactory.Create(client, settings);

                // Set the Default setting for Notifications
                dynLoader.DefaultNotificationSettings = new AdsNotificationSettings(AdsTransMode.OnChange, 200, 2000);

                // Get the Symbols (Dynamic Symbols)
                dynamic dynamicSymbols = ((IDynamicSymbolLoader)dynLoader).SymbolsDynamic;

                dynamic adsPort = dynamicSymbols.TwinCAT_SystemInfoVarList._AppInfo.AdsPort;

                // Access Main Symbol with Dynamic Language Runtime support (DLR)
                // Dynamically created property "Main"
                // dynamic symMain = dynamicSymbols.Main;

                // Main is an 'VirtualSymbol' / Organizational unit that doesn't have a value
                // Calling ReadValue is not allowed
                // bool test = symMain.HasValue;
                // dynamic invalid = symMain.ReadValue();

                // Reading TaskInfo Value
                // With calling ReadValue() a 'snapshot' of the Symbols Instance is taken
                dynamic vTaskInfoArray = dynamicSymbols.TwinCAT_SystemInfoVarList._TaskInfo.ReadValue();

                // Getting the Snapshot time in UTC format
                DateTime timeStamp1 = vTaskInfoArray.UtcTimeStamp;

                // Getting TaskInfo Symbol for Task 1
                dynamic symTaskInfo1 = dynamicSymbols.TwinCAT_SystemInfoVarList._TaskInfo[1];

                // Getting CycleCount Symbol
                dynamic symCycleCount = symTaskInfo1.CycleCount;

                // Take Snapshot value of the ApplicationInfo struct
                dynamic vAppInfo = dynamicSymbols.TwinCAT_SystemInfoVarList._AppInfo.ReadValue();

                // Get the UTC Timestamp of the snapshot
                DateTime timeStamp2 = vAppInfo.UtcTimeStamp;

                // Access the ProjectName of the ApplicationInfo Snapshot (type-safe!)
                string projectNameValue = vAppInfo.ProjectName;

                // Reading the CycleCount Value
                uint cycleCountValue = symTaskInfo1.CycleCount.ReadValue(); // Taking a Value Snapshot

                // Registering for dynamic "ValueChanged" events for the Values
                // Using Default Notification settings
                symCycleCount.ValueChanged += new EventHandler <ValueChangedArgs>(CycleCountValueChanged);

                // Override default notification settings
                symTaskInfo1.NotificationSettings = new AdsNotificationSettings(AdsTransMode.Cyclic, 500, 0);

                // Register for ValueChanged event.
                symTaskInfo1.ValueChanged +=
                    new EventHandler <ValueChangedArgs>(TaskInfo1ValueValueChanged); // Struct Type

                Thread.Sleep(10000);                                                 // Sleep main thread for 10 Seconds
            }

            Console.WriteLine("CycleCount Changed events received: {0}", cycleCountEvents);
            Console.WriteLine("taskInfo1 Changed events received: {0}", taskInfo1Events);
        }