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.

...

To create scripts based on the Python programming language, navigate to Scripts > Tasks, select a new, blank new row, and double-click on the Code column. A After clicking Code, a combobox will appear and you should be able to select Python.  . Select Python from the combobox. 

Note

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

...

After creating a new script task, you need to edit it through in the CodeEditor tab.



The configurations settings are detailed below:

...

Warning

After making any changes, you need 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 to 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.), they you must be installed 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 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
Connect(runtimeHostAddress, userName, password):

runtimeHostAddress  =  IP address or server name
userName  = User name.
password = Password


  • Get a Status of Server Connectionserver connection status
Code Block
GetConnectionStatus ()                 

           

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

...

Code Block
Disconnect()


  • Set a flag waiting value from server
Code Block
SetSyncFlag(flag):

flag = True wait value from server, false does not wait value from server.


  • Retrieve a current value
Code Block
GetObjectValue(name)

name = TagName


  • Set a new value for an object
Code Block
SetObjectValue(name, newValue)

name = TagName
newValue = new value to set in the tag.

...

Using Namespace in CodeBehind

In this first example, there are two input parameters called val1 and val2 that will be summarized and the result will be stored in the result variable.

...

In this scenario, we configure a task for the Python language. In the Python file name field, you need to set the Python file that will be to be executed. In this example, we used the Main2.py.

...

In the Standard Output field, we selected a tag called output. This tag type must be text. In the Arguments field, we selected another type of tag called script.

The Using the print method, the Python file called Main2.py gets the retrieves the input data and outputs its value inside a string, using the print method. Where the . The sys.argv will receive the Tag.script and the output tag will receive all the values from the print() method.


Code Block
import sys

value = sys.argv[1] print("Value: " + value) print("That’s  all  folks!")

...

Using TKDataAccess.Py

In this example, we will you need to 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.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, in your project, 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.

...