Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Using TKDataAccess.Py

In this example, we will call a file named Main.py, which contains code that copies the content from tag1 (source) to tag2 (target).

The Python code using the TKDataAccess is in Main.py is described below:

Code Block
import sys
from Extensions.TKDataAccess import TKDataAccess dataAccess = TKDataAccess()
connectionStatus = dataAccess.Connect("127.0.0.1:3101", "guest", "") print("Connection: " + connectionStatus)

if dataAccess.IsConnected() :
ret  =  dataAccess.GetObjectValue("Tag.tag1") print("Value: " + str(ret)) dataAccess.SetObjectValue("Tag.tag2",  ret)

dataAccess.Disconnect()


Then, in the project, you need to create a code or a Python Script Task that executes the Main.py file that which contains the calling for TKDataAccess as showed in above. So here, we will use the Python namespace as previously described.

...

Code Block
try
{
string  error  =  Python.ExecutePyFile(@Info.GetExecutionFolder()  +  @"\Main.py",  null,  true); if (!string.IsNullOrEmpty(error))
throw new Exception(error);
}
catch (Exception ex)
{
@Info.Trace("Python: " + (ex.InnerException == null ? ex.Message : ex.InnerException.Message));
}


Note

For To use the Python namespace, you need to install Python for .NET.

...