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 Thu Dec 13, 2018 7:36 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 
Offline

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

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.


Regards!


Top
   
PostPosted: Tue Feb 06, 2018 6:49 am 
Offline

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).

Code:
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
VAR_INPUT
   iValue : INT;
END_VAR
VAR_OUTPUT
END_VAR
VAR
END_VAR""")

# 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
VAR_INPUT
   iVal1 : INT;
   iVal2 : INT;
END_VAR""")

# 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:
   found[0].remove()
else:
   print("POU 'MyPou' was not found"


Top
   
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 3 guests


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