In this thread we would like to collect samples and recipes to show you how to implement more complex procedures in the CoDeSys script language.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2011-05-05
Originally created by: M.Schaber
This example is a further development of example 1 from the documentation.
Purpose: To read a controller variable out of an external program or a batch file and to print it on the standard display so that it can be evaluated, e.g.:
\# We use the python 3 syntax for printfrom__future__importprint_function Â
\# Our project file is in the data subdirectory of the directory containing
\# the script, so we calculate the appropriate path.importosscriptdir=os.path.dirname(__file__)# Directory of our script.datadir=os.path.join(scriptdir,"Data")# Enter the subdirectory.projectpath=os.path.join(datadir,"Ampel.project")# And add the project name.
\# Now, we can open the project.proj=projects.open(projectpath)
\# We fetch the active application.app=proj.active_application
\# And create the online application for it.onlineapp=online.create_online_application(app)
\# We login into the device.onlineapp.login(OnlineChangeOption.Try,True)
\# We start the application, if necessary.ifnotonlineapp.application_state==ApplicationState.run:Â Â onlineapp.start()
\# We let the app do its work for some time...system.delay(1000)
\# We read the value of the variable we're interested in.value=onlineapp.read_value("PLC_PRG.iVar1")
\# We output the value to standard output, so our caller can process it.print(value)
\# Last but not least, we clean up.onlineapp.logout()proj.close()
In a windows command window, this looks as follows:
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2011-05-05
Originally created by: M.Schaber
Expecially when one calls Scripts from the command line interpreter, a possibility to pass parameters to the scripts would be handy.
As a workaroound, one can use environment variables:
from__future__importprint_functionimportosprint("project to open: ",os.environ["PROJECT"])
\# We assert that the application on the device is already running.proj=projects.open(os.environ["PROJECT"])onlineapp=online.create_online_application(proj.active_application)onlineapp.login(OnlineChangeOption.Try,True)print("variables to read:")forvarinos.environ["VARIABLES"].split(","):Â Â print("Â Â ",var," = ",onlineapp.read_value(var))
This can be launched from a batch file as follows:
C:\ProgramFiles\3SCoDeSysV3.4SP3\CoDeSys\Common>d:\TestScripts\Forum_Environment.batprojecttoopen: D:\TestScripts\Data\OnlineTest.project------Buildstarted: Application: CoDeSys_Control_Win_V3.Application-------TheapplicationisuptodateBuild: Information: Sizeofgeneratedcode: 6453bytesBuild: Information: Sizeofglobaldata: 2606bytesBuild: Information: Totalallocatedmemorysizeforcodeanddata: 12505bytesBuild: Information: Memoryarea0contains Data, Input, Output, MemoryandCode: highestusedaddress: 1048576, largestcontiguousmemorygap: 1036071(98%)Compilecomplete--0errors, 0warningsvariablestoread:
   POU.testint = INT#1
   POU.testoutput = INT#3C:\ProgramFiles\3SCoDeSysV3.4SP3\CoDeSys\Common>
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2011-05-05
Originally created by: M.Schaber
Currently, scripts can add devices only via the Device ID. If one doesn't know the ID of a specific device, the following script can help to find out. Just insert the device into the current project, and launch the script:
\# Prints out all devices and other objects in the currently open project.
\# We enable the new python 3 print syntaxfrom__future__importprint_function
\# define the printing functiondefprinttree(treeobj,depth=0):Â Â name=treeobj.get_name(False)Â Â Â Â iftreeobj.is_device:Â Â Â Â deviceid=treeobj.get_device_identification()Â Â Â Â print("{0} - {1} {2}".format("Â Â "*depth,name,deviceid))Â Â Â Â forchildintreeobj.get_children(False):Â Â Â Â printtree(child,depth+1)forobjinprojects.primary.get_children():Â Â printtree(obj)print("--- Script finished. ---")
The output looks like this:
-CoDeSys_Control_Win_V3DeviceID(type=4096,Id='0000 0001',Version='3.4.2.0')Â Â -CANbusDeviceID(type=15,Id='181015',Version='3.4.2.0')Â Â Â -CANopen_ManagerDeviceID(type=16,Id='181016',Version='3.4.2.0')--- Script finished. ---
And now you know the Device ID you needed.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2011-05-05
Originally created by: M.Schaber
Some users want to fetch the starting basis for their projects from a Subversion repository.
The following example (which is similar to Example 4 in the documentation) creates a new project, and adds a device via PLCOpenXML.
\#ImportsaDeviceinPLCOpenXMLfromSubversionviacommandlinesvnclient.\#Weenablethenewpython3printsyntaxfrom__future__importprint_functionimportsys,os\#somevariabledefinitions:SVNEXE=r"C:\ProgramFiles\Subversion\bin\svn.exe"XMLURL = "file:///D:/testrepo/testfolder/TestExport.xml"PROJECT = r"D:\test.project"\#cleanupanyopenproject:ifprojects.primary:
  projects.primary.close()\#Fetchtheplcopenxmldatafromsubversion.\#The'with'constructautomaticallyclosestheopenpipeforus.withos.popen('"'+SVNEXE+'" cat '+XMLURL,'r')aspipe:
  xmldata=pipe.read()\#createanewproject:proj=projects.create(PROJECT)\#createtheimportreporterclassReporter(ImportReporter):
  deferror(self,message):
    system.write_message(Severity.Error,message)
  defwarning(self,message):
    system.write_message(Severity.Warning,message)
 Â
  defresolve_conflict(self,obj):
    returnConflictResolve.Copy
 Â
  defadded(self,obj):
    print("added: ",obj)
  defreplaced(self,obj):
    print("replaced: ",obj)
  defskipped(self,obj):
    print("skipped: ",obj)
   Â
  @property
  defaborting(self):
    returnFalse\#createtheimporterinstance.reporter=Reporter()   Â
\#importthedataintotheproject.proj.import_xml(reporter,xmldata)\#andfinallysave.:-)proj.save()print("--- Script finished. ---")
The subversion command line client which is needed for the script above is available for free from several providers. The Subversion Download Page contains links to some of them, the Cygwin-Project contains another one.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2011-08-02
Originally created by: M.Schaber
Example snippets: Quickly finding library managers:
Find all library managers in the project:
objects=proj.get_children(recursive=True)forcandidateinobjects:
  ifcandidate.is_libman: Â
    print(candidate)
About the device ID (for automatically inserting devices to your project):
Since 3.4.4.0 you can go to the information tab of the device which shows all details like type, ID and version.
This makes it much easier than first creating a script to get this information.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2011-08-02
Originally created by: M.Schaber
TimvH hat geschrieben:
About the device ID (for automatically inserting devices to your project):
Since 3.4.4.0 you can go to the information tab of the device which shows all details like type, ID and version.
This makes it much easier than first creating a script to get this information.
It seems you're right, but unfortunately, it seems that module IDs are not displayed there.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
# We use the python 3 syntax for print
from future import print_function
# Our project file is in the data subdirectory of the directory containing
# the script, so we calculate the appropriate path.
import os
scriptdir = os.path.dirname(file) # Directory of our script.
datadir = os.path.join(scriptdir, "Data") # Enter the subdirectory.
projectpath = os.path.join(datadir, "Ampel.project") # And add the project name.
...................................
but I recieve always the following error: NameError: name 'projects' is not defined
could u help please and tell me how to correct it?
Thank you
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2011-10-07
Originally created by: M.Schaber
Hi,
aoj hat geschrieben:
i was trying to understand the following code:
# We use the python 3 syntax for print
from future import print_function
This statement enables the python 3 syntax for print (See the python documentation for more details). I use this syntax so that this script can run with both Python 2 and python 3 without modifications.
aoj hat geschrieben:
# Our project file is in the data subdirectory of the directory containing
# the script, so we calculate the appropriate path.
import os
scriptdir = os.path.dirname(file) # Directory of our script.
datadir = os.path.join(scriptdir, "Data") # Enter the subdirectory.
projectpath = os.path.join(datadir, "Ampel.project") # And add the project name.
...................................
This code calculates the location of the project "Ampel.project" relative to the directory where the script itsself is. It assumes that in the directory containing the script file there is a subdirectory "Data" containing the "Ampel.project" file. This just happens to be the directory layout I used for my internal test scripts.
aoj hat geschrieben:
but I recieve always the following error:
could u help please and tell me how to correct it?
"projects" is an object imported into the namespace for scripts running in CoDeSys, so it is only available if your script is being started in CoDeSys directly (either via the "Execute Script" menu command or button, or via the "--runscript" command line parameter.)
For modules imported by your scripts, this automatic injection does not take place because it would break some modules from the standard library. Simply add "from scriptengine import *" at the top of your module.
If you run the script from outside of CoDeSys (for example in the py.exe or ipy.exe interpreters, or an IDE like IDLE or Eric), it won't work, since the codesys environment is not available there.
But remember that you can start CoDeSys with the "--runscript" and "--NoUI" parameters to execute python scripts within CoDeSys. (See the documentation for more information.)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
"If you run the script from outside of CoDeSys (for example in the py.exe or ipy.exe interpreters, or an IDE like IDLE or Eric), it won't work, since the codesys environment is not available there."
I run the script from IDLE! It means it won't work.
I want actually to export a xml-file with python. I have a CoDeSys-Project named Test.project. The first step ist to open this project, which i did with "os.startfile". The second step is to export the xml-file directly from python.
Could you give some tips??
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2011-10-07
Originally created by: M.Schaber
Hi,
aoj hat geschrieben:
"If you run the script from outside of CoDeSys (for example in the py.exe or ipy.exe interpreters, or an IDE like IDLE or Eric), it won't work, since the codesys environment is not available there."
I run the script from IDLE! It means it won't work.
Yes. If you run the script from IDLE, it has no way to access the CoDeSys functionality. The CoDeSys ScriptEngine is not a bunch of ordinary python modules, but a so-called hosted environment, that means the Python interpreter is embedded (and deeply integrated) into the CoDeSys application.
aoj hat geschrieben:
I want actually to export a xml-file with python. I have a CoDeSys-Project named Test.project. The first step ist to open this project, which i did with "os.startfile". The second step is to export the xml-file directly from python.
Could you give some tips??
Ah, now I see. Your current approach would require the Python script from IDLE to magically recognize the running CoDeSys instance, and then send the commands to that application. Unfortunately, that is not how it currently works. (Note that os.startfile() gives you no way to wait for the application to finish startup and loading, so you would not know when you could actually start to send commands to CoDeSys.)
``` and use an environment variable to pass the name of the CoDeSys project to the script. (See l viewtopic.php?p=3803#p3803 l for an example of parameter passing via environment variables.) Then, in the CoDeSysScript.py, you can load the project and export the files.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
if i use the code: os.system(r'c:\Path\to\CoDeSys.exe --runscript="c:\Path\to\CoDeSysScript.py"' and in my CoDeSysScript.py should only open the project Test.project; is it fine to use os.startfile()??? or is there a better command to load the project?
Another question: what is the difference between using PLCOpenXML and using the native Export format of CoDeSys?
Thanks a lot.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2011-10-10
Originally created by: M.Schaber
Hi, Aoj,
aoj hat geschrieben:
if i use the code: os.system(r'c:\Path\to\CoDeSys.exe --runscript="c:\Path\to\CoDeSysScript.py"' and in my CoDeSysScript.py should only open the project Test.project; is it fine to use os.startfile()??? or is there a better command to load the project?
Inside the CoDeSysScript.py, do not use os.startfile(). That will start a second instance of codesys loading the project, and you still don't have control over that second instance.
You need to open the project and do the export from within the CoDeSysScript.py. This may look like:
projects.open, and the find and export_xml methods are provided by the CoDeSys environment.
aoj hat geschrieben:
Another question: what is the difference between using PLCOpenXML and using the native Export format of CoDeSys?
PLCOpenXML is a standardized format for interchange of data between independent IEC-61131 implementations. Due to that, it is interoperable with other software, but it is not lossless (some aspects may be lost if you export in that format).
The native Export format is CoDeSys-specific and less human-readable, but it is lossless.
So if you only use those exported objects for re-importing them into CoDeSys later, you use the native format. If you want to process those files using 3rd-party tools, you should use the PLCOpenXML format.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you for the explanation about the difference between the PLCOpenXML and the native export format.
in a previous post we talked about the error: "NameError: name 'projects' is not defined".
you said i can correct this error by adding "from scriptengine import *" at the top of the module in condition i am starting CoDeSys with the "--runscript".
after adding "from scriptengine import *", i get the following error: "ImportError: No module named scriptengine" !!!!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2011-10-10
Originally created by: M.Schaber
aoj hat geschrieben:
in a previous post we talked about the error: "NameError: name 'projects' is not defined".
you said i can correct this error by adding "from scriptengine import *" at the top of the module in condition i am starting CoDeSys with the "--runscript".
It seems that you did misunderstand what I wrote. This works if used inside a python module which is imported by a script running inside CoDeSys, but it is not necessary for the main script itsself (the one whose name you pass to --runscript).
aoj hat geschrieben:
after adding "from scriptengine import *", i get the following error: "ImportError: No module named scriptengine" !!!!
Is this in the script running inside CoDeSys, or is it the outer script you use to start CoDeSys?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
it is the the script, which i use to start CoDeSys.
i have to write a script, which it starts a CoDeSys-project and export it as xml.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2011-10-10
Originally created by: M.Schaber
aoj hat geschrieben:
it is the the script, which i use to start CoDeSys.
Then it is in the wrong script. It should be in the script running inside CoDeSys.
aoj hat geschrieben:
i have to write a script, which it starts a CoDeSys-project and export it as xml.
As I told you, you cannot command CoDeSys to export a file as XML "from outside". The way to achieve that goal is to start a CoDeSys instance with the "--runscript" parameter, which makes CoDeSys executing the script you pass via --runscript inside the CoDeSys environment. Then this script running inside CoDeSys can perform the opening of the project and exporting of XML.
To rephrase it again in other words: You need two scripts:
All CoDeSys specific commands like projects.open or export_xml are only available to scripts running inside CoDeSys (the '' script). CoDeSys itsself must be executing that script using the built-in IronPython interpreter so that the script has access to all the CoDeSys commands. This concept is called a "Hosted Environment" or an "Embedded Interpreter".
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
a question about reading a variable with a python script. I get an exception (not a builtin one)
when I trying to read a variable (inte) in a program unit, PLC_PRG.
.
..
...
# read value of inte
value = onlineapp.read_value("PLC_PRG.inte")
The variable is declared:
PROGRAM PLC_PRG
VAR
inte: INT;
END_VAR
.
..
...
int := 42;
How come?
The whole trace on the script (a modified example script) looks like this:
[INFORMATION] test.py(4): Begin of script [INFORMATION] test.py(4): while len(projects.all) > 0: [INFORMATION] test.py(5): projects.all[0].close() [INFORMATION] test.py(8): proj = projects.open("C:\python\
\ObserverPattern.project") [INFORMATION] test.py(11): app = proj.active_application [INFORMATION] test.py(12): onlineapp =
online.create_online_application(app) [INFORMATION] test.py(15): onlineapp.login(OnlineChangeOption.Try,
True) [INFORMATION] test.py(18): if not onlineapp.application_state ==
ApplicationState.run: [INFORMATION] test.py(19): onlineapp.start() [INFORMATION] test.py(22): system.delay(1000) [INFORMATION] test.py(25): value =
onlineapp.read_value("PLC_PRG.inte") [INFORMATION] test.py(25): exception (<type 'exceptions.exception'="">,
Exception('Invalid expression',), None)</type>
Fredrik
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2011-10-11
Originally created by: M.Schaber
Hi,
nfredrik hat geschrieben:
a question about reading a variable with a python script. I get an exception (not a builtin one)
when I trying to read a variable (inte) in a program unit, PLC_PRG.
# read value of inte
value = onlineapp.read_value("PLC_PRG.inte")
As far as I can see, it should work this way. Could you please send me the project archive and script via our support contact, so I can investigate that case further? Thanks!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
first of all I want to thank you very much for yout help.
I have wrote a Python-script, which it execute CoDeSys and call a second scipt. This second script opens A CoDeSys-Project and should export it as XML. But after opening the CoDeSys-Project I got an Error:
What does this Error mean and how can I correct it?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2011-10-12
Originally created by: M.Schaber
aoj hat geschrieben:
first of all I want to thank you very much for yout help.
You're welcome!
aoj hat geschrieben:
I have wrote a Python-script, which it execute CoDeSys and call a second scipt. This second script opens A CoDeSys-Project and should export it as XML. But after opening the CoDeSys-Project I got an Error:
What does this Error mean and how can I correct it?
The error message shows an ArgumentOutOfRangeException. That means that some code accessed some array or list via an index which was out of range.
I don't know your script, so it's hard to diagnose the problem, but maybe you did use a construct like ```
device = proj.find('_3S_Testspec_Device')[0]
``` and there is no object with that name. In that case, find returns an empty list, so the index access with [0] fails.
You should enable script tracing to find out which line of the code is the one causing the error.
Yours, Markus
PS: To enable script tracing, you have three options:
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
defwarn(self,message):system.write_message(Severity.Warning,"Warning exporting %s: %s"%message)defskipped(self,type_name,value_name):system.write_message(Severity.Information,"Object not exportable: %s value %s"%(type_name,value_name))
Originally created by: M.Schaber
In this thread we would like to collect samples and recipes to show you how to implement more complex procedures in the CoDeSys script language.
Originally created by: M.Schaber
This example is a further development of example 1 from the documentation.
Purpose: To read a controller variable out of an external program or a batch file and to print it on the standard display so that it can be evaluated, e.g.:
This is the actual code:
In a windows command window, this looks as follows:
Originally created by: M.Schaber
Expecially when one calls Scripts from the command line interpreter, a possibility to pass parameters to the scripts would be handy.
As a workaroound, one can use environment variables:
This can be launched from a batch file as follows:
The result looks like this:
Originally created by: M.Schaber
Currently, scripts can add devices only via the Device ID. If one doesn't know the ID of a specific device, the following script can help to find out. Just insert the device into the current project, and launch the script:
The output looks like this:
And now you know the Device ID you needed.
Originally created by: M.Schaber
Some users want to fetch the starting basis for their projects from a Subversion repository.
The following example (which is similar to Example 4 in the documentation) creates a new project, and adds a device via PLCOpenXML.
The subversion command line client which is needed for the script above is available for free from several providers. The Subversion Download Page contains links to some of them, the Cygwin-Project contains another one.
Originally created by: M.Schaber
Example snippets: Quickly finding library managers:
Find all library managers in the project:
The quick way is via list comprehension:
Or via the filter function:
If you are only interested in the top level library manager (the one in the POU view), you set recursive to False.
Remind that you can also search under a specific device or application if you call get_children on that object instead of the project.
About the device ID (for automatically inserting devices to your project):
Since 3.4.4.0 you can go to the information tab of the device which shows all details like type, ID and version.
This makes it much easier than first creating a script to get this information.
Originally created by: M.Schaber
It seems you're right, but unfortunately, it seems that module IDs are not displayed there.
Hi hi,
i was trying to understand the following code:
# We use the python 3 syntax for print
from future import print_function
# Our project file is in the data subdirectory of the directory containing
# the script, so we calculate the appropriate path.
import os
scriptdir = os.path.dirname(file) # Directory of our script.
datadir = os.path.join(scriptdir, "Data") # Enter the subdirectory.
projectpath = os.path.join(datadir, "Ampel.project") # And add the project name.
...................................
but I recieve always the following error: NameError: name 'projects' is not defined
could u help please and tell me how to correct it?
Thank you
Originally created by: M.Schaber
Hi,
This statement enables the python 3 syntax for print (See the python documentation for more details). I use this syntax so that this script can run with both Python 2 and python 3 without modifications.
This code calculates the location of the project "Ampel.project" relative to the directory where the script itsself is. It assumes that in the directory containing the script file there is a subdirectory "Data" containing the "Ampel.project" file. This just happens to be the directory layout I used for my internal test scripts.
"projects" is an object imported into the namespace for scripts running in CoDeSys, so it is only available if your script is being started in CoDeSys directly (either via the "Execute Script" menu command or button, or via the "--runscript" command line parameter.)
For modules imported by your scripts, this automatic injection does not take place because it would break some modules from the standard library. Simply add "from scriptengine import *" at the top of your module.
If you run the script from outside of CoDeSys (for example in the py.exe or ipy.exe interpreters, or an IDE like IDLE or Eric), it won't work, since the codesys environment is not available there.
But remember that you can start CoDeSys with the "--runscript" and "--NoUI" parameters to execute python scripts within CoDeSys. (See the documentation for more information.)
Hi Hi,
thank u for the reply.
"If you run the script from outside of CoDeSys (for example in the py.exe or ipy.exe interpreters, or an IDE like IDLE or Eric), it won't work, since the codesys environment is not available there."
I run the script from IDLE! It means it won't work.
I want actually to export a xml-file with python. I have a CoDeSys-Project named Test.project. The first step ist to open this project, which i did with "os.startfile". The second step is to export the xml-file directly from python.
Could you give some tips??
Originally created by: M.Schaber
Hi,
Yes. If you run the script from IDLE, it has no way to access the CoDeSys functionality. The CoDeSys ScriptEngine is not a bunch of ordinary python modules, but a so-called hosted environment, that means the Python interpreter is embedded (and deeply integrated) into the CoDeSys application.
Ah, now I see. Your current approach would require the Python script from IDLE to magically recognize the running CoDeSys instance, and then send the commands to that application. Unfortunately, that is not how it currently works. (Note that os.startfile() gives you no way to wait for the application to finish startup and loading, so you would not know when you could actually start to send commands to CoDeSys.)
What you currently could do is something like ```
os.system(r'c:\Path\to\CoDeSys.exe --runscript="c:\Path\to\CoDeSysScript.py"')
``` and use an environment variable to pass the name of the CoDeSys project to the script. (See l viewtopic.php?p=3803#p3803 l for an example of parameter passing via environment variables.) Then, in the CoDeSysScript.py, you can load the project and export the files.
Hi M.Schaber,
if i use the code: os.system(r'c:\Path\to\CoDeSys.exe --runscript="c:\Path\to\CoDeSysScript.py"' and in my CoDeSysScript.py should only open the project Test.project; is it fine to use os.startfile()??? or is there a better command to load the project?
Another question: what is the difference between using PLCOpenXML and using the native Export format of CoDeSys?
Thanks a lot.
Originally created by: M.Schaber
Hi, Aoj,
Inside the CoDeSysScript.py, do not use os.startfile(). That will start a second instance of codesys loading the project, and you still don't have control over that second instance.
You need to open the project and do the export from within the CoDeSysScript.py. This may look like:
projects.open, and the find and export_xml methods are provided by the CoDeSys environment.
PLCOpenXML is a standardized format for interchange of data between independent IEC-61131 implementations. Due to that, it is interoperable with other software, but it is not lossless (some aspects may be lost if you export in that format).
The native Export format is CoDeSys-specific and less human-readable, but it is lossless.
So if you only use those exported objects for re-importing them into CoDeSys later, you use the native format. If you want to process those files using 3rd-party tools, you should use the PLCOpenXML format.
Thank you for the explanation about the difference between the PLCOpenXML and the native export format.
in a previous post we talked about the error: "NameError: name 'projects' is not defined".
you said i can correct this error by adding "from scriptengine import *" at the top of the module in condition i am starting CoDeSys with the "--runscript".
after adding "from scriptengine import *", i get the following error: "ImportError: No module named scriptengine" !!!!
Originally created by: M.Schaber
It seems that you did misunderstand what I wrote. This works if used inside a python module which is imported by a script running inside CoDeSys, but it is not necessary for the main script itsself (the one whose name you pass to --runscript).
Is this in the script running inside CoDeSys, or is it the outer script you use to start CoDeSys?
it is the the script, which i use to start CoDeSys.
i have to write a script, which it starts a CoDeSys-project and export it as xml.
Originally created by: M.Schaber
Then it is in the wrong script. It should be in the script running inside CoDeSys.
As I told you, you cannot command CoDeSys to export a file as XML "from outside". The way to achieve that goal is to start a CoDeSys instance with the "--runscript" parameter, which makes CoDeSys executing the script you pass via --runscript inside the CoDeSys environment. Then this script running inside CoDeSys can perform the opening of the project and exporting of XML.
To rephrase it again in other words: You need two scripts:
All CoDeSys specific commands like projects.open or export_xml are only available to scripts running inside CoDeSys (the '' script). CoDeSys itsself must be executing that script using the built-in IronPython interpreter so that the script has access to all the CoDeSys commands. This concept is called a "Hosted Environment" or an "Embedded Interpreter".
Hi,
a question about reading a variable with a python script. I get an exception (not a builtin one)
when I trying to read a variable (inte) in a program unit, PLC_PRG.
.
..
...
# read value of inte
value = onlineapp.read_value("PLC_PRG.inte")
[INFORMATION] test.py(25): exception (<type 'exceptions.exception'="">,
Exception('Invalid expression',), None)</type>
The variable is declared:
PROGRAM PLC_PRG
VAR
inte: INT;
END_VAR
.
..
...
int := 42;
How come?
The whole trace on the script (a modified example script) looks like this:
[INFORMATION] test.py(4): Begin of script
[INFORMATION] test.py(4): while len(projects.all) > 0:
[INFORMATION] test.py(5): projects.all[0].close()
[INFORMATION] test.py(8): proj = projects.open("C:\python\
\ObserverPattern.project")
[INFORMATION] test.py(11): app = proj.active_application
[INFORMATION] test.py(12): onlineapp =
online.create_online_application(app)
[INFORMATION] test.py(15): onlineapp.login(OnlineChangeOption.Try,
True)
[INFORMATION] test.py(18): if not onlineapp.application_state ==
ApplicationState.run:
[INFORMATION] test.py(19): onlineapp.start()
[INFORMATION] test.py(22): system.delay(1000)
[INFORMATION] test.py(25): value =
onlineapp.read_value("PLC_PRG.inte")
[INFORMATION] test.py(25): exception (<type 'exceptions.exception'="">,
Exception('Invalid expression',), None)</type>
Fredrik
Originally created by: M.Schaber
Hi,
As far as I can see, it should work this way. Could you please send me the project archive and script via our support contact, so I can investigate that case further? Thanks!
Hallo M. Schaber,
first of all I want to thank you very much for yout help.
I have wrote a Python-script, which it execute CoDeSys and call a second scipt. This second script opens A CoDeSys-Project and should export it as XML. But after opening the CoDeSys-Project I got an Error:
What does this Error mean and how can I correct it?
Originally created by: M.Schaber
You're welcome!
The error message shows an ArgumentOutOfRangeException. That means that some code accessed some array or list via an index which was out of range.
I don't know your script, so it's hard to diagnose the problem, but maybe you did use a construct like ```
device = proj.find('_3S_Testspec_Device')[0]
``` and there is no object with that name. In that case, find returns an empty list, so the index access with [0] fails.
You should enable script tracing to find out which line of the code is the one causing the error.
Yours, Markus
PS: To enable script tracing, you have three options:
you are right, the error is because of device = proj.find('_3S_Testspec_Device')[0].
What for is "_3S_Testspec_Device"?
Originally created by: M.Schaber
Hi,
"_3S_Testspec_Device" is the name of the device object I want to find.
The find() function returns a list of all objects it found with that name.
The reason why find() returns a list is that some objects (like folders) can have the same names, so a find() can return several objects.
If you want to export the complete project, you should call the export_xml or export_native methods of the proj object instead.
Hali Halo,
I am trying now with the following script:
[img]
from future import print_function
from scriptengine import *
import sys, io, utils
utils.ensure_tempdir()
utils.close_all()
system.trace = True
proj = projects.open(utils.data('.........../Test.project'))
class ExportHandler(NativeExportReporter):
def cancel(self, message):
system.write_message(Severity.Error, "Cancelling export: %s" % message)
reporter = ExportHandler()
proj.export_native("....../FileName.export", recursive = True, reporter = reporter)
[/img]
The script open the CoDeSys-Project and show the following error:
What does this error mean and what could be the solution?