CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Tue Aug 20, 2019 12:42 pm

All times are UTC+01:00

Post new topic  Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Simple Code with Script
PostPosted: Mon Feb 05, 2018 9:13 am 

Joined: Mon Nov 27, 2017 3:12 pm
Posts: 4

Is it possible to write a simple Codefragment, something like "count := count + 1; etc.. " including creating the variable count over a Python Script?

I already have written a script which creates a POU (ST), but i can not find any topics about writing an application over a script.


PostPosted: Tue Feb 06, 2018 6:49 am 

Joined: Tue Oct 14, 2014 1:38 pm
Posts: 8
You have to use textual_implementation and textual_declaration properties of IScriptTextDocument.

There are few methods like replace, insert, append and so on (see ScriptEngine.chm for more information).

proj = projects.primary

found = proj.find("Application", True)
app = found[0]

# Create FB
mypou = app.create_pou("MyPou")

# Change declaration of the FB
implementation = mypou.textual_declaration.replace("""FUNCTION_BLOCK MyPou
   iValue : INT;

# Change implementation of the FB
mypou.textual_implementation.replace("""iValue := iValue + 1;""")

# Add method to FB
dosomething = mypou.create_method("DoSomething", "INT")

# Change declaration of the method
dosomething.textual_declaration.replace("""METHOD DoSomething : INT
   iVal1 : INT;
   iVal2 : INT;

# Change implementation of the method
dosomething.textual_implementation.replace("""DoSomething := iVal1 + iVal2;""")

# Find the pou and delete it
found = app.find("MyPou")
if found and len(found) == 1:
   print("POU 'MyPou' was not found"

Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 2 posts ] 

All times are UTC+01:00

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited