Versions Compared

Key

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

...

Info
iconfalse

Quick video tutorial: Using Python with TK Data Access, Using Python with Script Task, Using Python with Code Behind (no audio)

Overview

The programming in many of your projects will consist of C# or VB.Net 100% managed code that is designed to run in the Microsoft .NET framework.

FactoryStudio now includes Python as a programming language you can use in Code Behind, Scripts, Tasks, and interactively via external Python code. 

Python is an interpreted, high-level, general purpose language.   It is a popular language for machine learning, which is useful for things like Predictive Maintenance algorithms.

...

Note

Once a new task is created, the language type cannot be altered through the Code column.

...

Warning

After making any changes, click the Apply Changes button.


...

Python Namespace

Must use for .Net developers, the The Python namespace can be used in any script editor inside your project environment (Tasks, Classes and , or CodeBehind) . Basically, all you need is to install Python for .NETinside your project environment. To use the Python namespace, you simply need to install the Python.NET package, available on github.

The Python namespace provides several .Net methods that interact with Python codes and objects. See some of those methods below:                        

...

  • Convert a Python value to a .NET value
Code Block
public static object FromPython(object value)

object value = Python value
object returns = NET value

...

  • Dump a python object to a string to send it to a TraceWindow
Code Block
public static string DumpPythonObjectToString(object pythonObject)

object pythonObject = Python object.
string returns = Dump information of object.

...

Note

If you need to install other Python modules and libraries (such as numpy, pythonnet, matplotlib, etc.), you must install them in the same location as the Python Engine (python.exe).


Warning

Those All of the methods listed above are disabled for Mono project projects and HTML5 displays.


...

TKDataAccess.py

Must use for Python developers, you can You can create code in the Python environment and use the TKDataAccess.py file to interact with the projects. 

Warning

Tatsoft provides the TKDataAccess.py is provided by Tatsoft, and when usedfile. When you use it, you need to make sure the installPath into the TKDataAccess.py file is with the correct path of Studio installation.it is installed in the same folder as FactoryStudio. 


Below are some methods from TKDataAccess.py that you can use:

...

Code Block
GetConnectionStatus ()                 

           

  • Check whether is connected to your script's connection to the server
Code Block
IsConnected ()

...

The Python code using the TKDataAccess.py module 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, you need to create a the code that is shown below or create a Python Script Task that executes the Main.py file, which contains the calling for TKDataAccess shown above. So here, we will use the Python namespace as previously described.

...