I have to use Beckhoff I/O in real-time mode (not PLC project), because the final goal is to make a Soft-PLC, where the program runs on the cumputer and not on Beckhoff. So in Visual Studio I wrote a c# code which opens an existing TwinCAT project in background (with a task and some variables linked), activates the configuration and then gets the symbols from that task, so I can use them to control in realtime the variables (e.g. reading an input or writing an output). The problem is I can't get the symbols! (Im using the SymbolLoaderFactory.Create method). I created a class "Controlling" in the same namespace which contains some methods like GoConfig(), Open(), Activate() etc..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EnvDTE;
using System.IO;
using TCatSysManagerLib;
using System.Management;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Collections;
using System.Xml;
using TwinCAT.Ads;
using TwinCAT.Ads.ValueAccess;
using TwinCAT.Ads.TypeSystem;
using TwinCAT.TypeSystem;
namespace TCControl
{
class Program
{
[STAThread]
static void Main(string[] args)
{
string solName = "solution1";
string solPath = $@"B:\...solutionPath...\{solName}";
string[] terminal = new string[1];
terminal[0] = "EK1100";
string ipAddress = "10.157.4.69.1.1";
int port = 301;
Controlling co = new Controlling();
MessageFilter.Register();
//go to configuration mode
co.GoConfig(ipAddress);
//open the solution
string[,] treeItemsandTypes = co.Open(solPath, solName, ipAddress, terminal);
co.Activate(solPath, solName);
//obtain symbols
ISymbolLoader symbolLoader = co.SymbolsGetter(ipAddress, port);
ISymbol[] symbols = new ISymbol[symbolLoader.Symbols.Count];
int i = 0;
foreach (ISymbol symbol in symbolLoader.Symbols)
{
symbols[i] = symbol;
i++;
}
Console.WriteLine("\n" + symbols[0].ToString());
Console.WriteLine("\n" + symbols[1].ToString());
All projects I created to test this had 2 variables linked to the task. Everything works fine until the SymbolsGetter method. From the Controlling class:
public ISymbolLoader SymbolsGetter(string indirizzo, int port)
{
using (TcAdsClient client = new TcAdsClient())
{
System.Threading.Thread.Sleep(2000);
client.Connect(indirizzo, port);
//Creates the Symbol Objects as hierarchical tree
SymbolLoaderSettings settings = new SymbolLoaderSettings(SymbolsLoadMode.Flat, ValueAccessMode.IndexGroupOffsetPreferred);
ISymbolLoader symbolLoader = SymbolLoaderFactory.Create(client, settings);
//System.Threading.Thread.Sleep(2000);
return symbolLoader;
}
}
The Thread.Sleep is because otherwise the client.Connect generated an error. It only works in debug if I add a break point on "return symbolLoader" and if I pass over it with the mouse puntator to see the value (of symbolLoader). I tried to insert another sleep but nothing, it doesn't work. Im actually starting thinking it is black magic. Another problem is that I can't get symbols from all projects (nether using the debug technique of before). And even when it works I can't use the name of the symbol to write a high/low signal to that variable. So nothing is working, I'm just able to create and open a project, a task etc. Just one more information: im using windows 10 (maybe TwinCAT works better on windows 7 or xp, but I have to make it work on windows 10). I have to do everything without touching or seeing the TwinCAT project window, it has to stay in background. I'm not a programmer and I'm quite new into the Beckhoff/TwinCAT world. Please if anyone can help me I'd be incredibly thankful to him.