CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum
Deutsche Version English version russian version 
It is currently Fri Jun 23, 2017 4:22 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Get Variables from GVL
PostPosted: Fri Jan 13, 2017 12:14 pm 
Offline

Joined: Thu Oct 13, 2016 4:19 pm
Posts: 45
Location: Presezzo, Bergamo (ITA)
Hi all,
I need to get the name of the variables from the GVL of my application in order to perform some check on the names. How can I get these informations?
Should I read the entire GVL file and parse the text line by line? In that case how can I do it?
I'm totally new in this field (execute Python script in Codesys) and I don't know which functions are available...do you have a guide or something similar?

Thank you very much,
Best regards.
Massimo


Top
   
PostPosted: Fri Jan 13, 2017 2:26 pm 
Offline
Frequent User
Frequent User

Joined: Tue Nov 13, 2012 9:20 am
Posts: 199
Hi Massimo.

Massimo.Milluzzo wrote:
I need to get the name of the variables from the GVL of my application in order to perform some check on the names. How can I get these informations?
Should I read the entire GVL file and parse the text line by line? In that case how can I do it?

You can export your GVL in PLCopenXML and look for the XML node "globalVars" which represents a GVL. You find the variables below that node.

Here some code for exporting a GVL as PLCopenXML:
Code:
class ExportHandler(ExportReporter):    
    def error(self, object, message):   
        system.write_message(Severity.Error, "Error exporting %s: %s" % (object, message))
    def warning(self, object, message):   
        system.write_message(Severity.Warning, "Warning exporting %s: %s" % (object, message))
    def nonexportable(self, object):   
        system.write_message(Severity.Information, "Object not exportable: %s" % object)
    @property
    def aborting(self):
        return False;

#
# Main program
#
proj = projects.primary
found = proj.find('GVL', True)
assert(found)
gvl = found[0]

reporter = ExportHandler()

print "exporting to string:"
xmldata = gvl.export_xml(reporter)

# or

print "exporting to file:"
gvl.export_xml(reporter, r"c:\Temp\test_gvl.xml")

print "script finished."

For the XML you can use one of the Python modules included in the standard library.

Massimo.Milluzzo wrote:
I'm totally new in this field (execute Python script in Codesys) and I don't know which functions are available...do you have a guide or something similar?

The documentation of the Scripting API is in the file "ScriptEngine.chm" which is in the sub directory "Online Help" of the CODESYS directory.

BR
Martin

_________________
Check out the CODESYS store: http://store.codesys.com/

CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions


Top
   
PostPosted: Mon Jan 16, 2017 8:12 am 
Offline

Joined: Thu Oct 13, 2016 4:19 pm
Posts: 45
Location: Presezzo, Bergamo (ITA)
Hi Martin,
Thank you very much for the reply, it is very useful

Best regards.
Massimo


Top
   
PostPosted: Mon Jan 30, 2017 11:26 am 
Offline

Joined: Thu Oct 13, 2016 4:19 pm
Posts: 45
Location: Presezzo, Bergamo (ITA)
Hi all,
I'm still in trouble with my project. I only recently tried to perform the operations you suggest but I always got an error (see attachments)
Here's my python code
Code:
class ExportHandler(ExportReporter):    
    def error(self, object, message):   
        system.write_message(Severity.Error, "Error exporting %s: %s" % (object, message))
    def warning(self, object, message):   
        system.write_message(Severity.Warning, "Warning exporting %s: %s" % (object, message))
    def nonexportable(self, object):   
        system.write_message(Severity.Information, "Object not exportable: %s" % object)
    @property
    def aborting(self):
        return False;

#
# Main program
#
proj = projects.primary
reporter = ExportHandler()
import xml.etree.ElementTree as ET
found = proj.find('GVL', True)
assert(found)
gvl = found[0]
#print "exporting GVL to string:"
#xmldata = gvl.export_xml(reporter)
print "exporting GVL to file:"
gvl.export_xml(reporter, r"c:/temp/gvl.xml")

print "Parse the file:"
tree = ET.parse("c:/temp/gvl.xml")
print "Get the root:"
root = tree.getroot()
print "Attribute: %s" % (root.attrib)


Every time I run the script I get an error on the parse function (see "XMLError.png"). I tried to modify the file "gvl.xml" removing the first line but the result is the same (see "XMLError2.png")
I checked the file with an XML validator and everything seems ok.
Do you have any suggestion?
Thank you in advance,
Regards.
Massimo

Attachment:
XMLError.png

Attachment:
XMLError2.png


You do not have the required permissions to view the files attached to this post.


Top
   
PostPosted: Mon Jan 30, 2017 2:39 pm 
Offline

Joined: Thu Oct 13, 2016 4:19 pm
Posts: 45
Location: Presezzo, Bergamo (ITA)
Update:

If I try to parse the following file with the same python code I have no problem.

Code:
<nodes att="Root node">
   <node1 att="First child">
      <node1Child1 att="Attribute 1"></node1Child1>
      <node1Child2 att="Attribute 2"></node1Child2>
      <node1Child3 att="Attribute 3"></node1Child3>
   </node1>
   <node1 att="Second child">
      <node1Child4 att="Attribute 4"></node1Child4>
      <node1Child5 att="Attribute 5"></node1Child5>
      <node1Child6 att="Attribute 6"></node1Child6>
   </node1>
   <node2 att="Third child">
      <node2Child1 att="Attribute 1"></node2Child1>
      <node2Child2 att="Attribute 2"></node2Child2>
      <node2Child3 att="Attribute 3"></node2Child3>
   </node2>
</nodes>


Top
   
PostPosted: Mon Jan 30, 2017 4:39 pm 
Offline

Joined: Thu Oct 13, 2016 4:19 pm
Posts: 45
Location: Presezzo, Bergamo (ITA)
Another update.
It seems the problem lies in the "export_xml" function or in the "reporter" object. I cannot parse either any file or string created by the function. If I try to parse either a file or a string created manually I have no problems.
Furthermore I can parse the xml generated by the "export_xml" function if I create a new file and paste the code in it.


Top
   
PostPosted: Mon Jan 30, 2017 5:34 pm 
Offline
Frequent User
Frequent User

Joined: Tue Nov 13, 2012 9:20 am
Posts: 199
Hi Massimo.

Massimo.Milluzzo wrote:
Another update.
It seems the problem lies in the "export_xml" function or in the "reporter" object. I cannot parse either any file or string created by the function. If I try to parse either a file or a string created manually I have no problems.
Furthermore I can parse the xml generated by the "export_xml" function if I create a new file and paste the code in it.

The problem is the BOM (Byte Order Mark) which the XML writer of .Net automatically adds when writing XML files. You have to remove or skip it before parsing the XML content of the file. Or you can use .Net API which automatically skips the BOM when reading XML files if you run your python script in CODESYS Scripting and IronPython v2.7.x.

I used this workaround when I had to parse a PLCopenXML file generated by CODESYS:
Code:
# Load XML file
xmlfile = io.open(r"c:\tmp\test_gvl.xml", "r")
xmlfile.seek(3)
tree = ET.parse(xmlfile)
xmlfile.close()

BR
Martin

_________________
Check out the CODESYS store: http://store.codesys.com/

CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions


Top
   
PostPosted: Tue Jan 31, 2017 8:47 am 
Offline

Joined: Thu Oct 13, 2016 4:19 pm
Posts: 45
Location: Presezzo, Bergamo (ITA)
Hi Martin,
Thank you very much, I had no clue about that problem. Now I can read and parse the XML.
I will write soon for another problem :lol:

Regards,
Massimo


Top
   
PostPosted: Tue Feb 07, 2017 9:39 am 
Offline

Joined: Thu Oct 13, 2016 4:19 pm
Posts: 45
Location: Presezzo, Bergamo (ITA)
Hi all,
here I'm again. How can I export a visualization page? When I try I get the error:

Code:
[INFORMATION]   Object not exportable: ScriptObject{NoDeviceObject, NoExplicitConnectorObject, NoSymbolConfigObject, NoLibManObject, ScriptNoProjectInfoMarker,
NoScriptApplicationObject, ScriptNonTextualObject, NoTaskConfigObject, NoTaskObject, NoImagePoolObject, NoScriptApplicationComposerObject,
NoScriptApplicationComposerObject}(Project=79, Name=VisualizationExportTest, guid=9c501cb8-67a5-4bd8-9172-f67c77d0a324)


Thank you in advance,
Regards.
Massimo


Top
   
PostPosted: Tue Feb 07, 2017 1:36 pm 
Offline
Frequent User
Frequent User

Joined: Tue Nov 13, 2012 9:20 am
Posts: 199
Hi Massimo.

Massimo.Milluzzo wrote:
How can I export a visualization page? When I try I get the error:

No PLCopenXML export for Visualization objecs. You can use the native export but the XML format is not documented.

You can call the method export_native(...) from a common object which allows to export the object and its children or you can call it from a project which allows you to provide a list of objects which you want to export.

BR
Martin

_________________
Check out the CODESYS store: http://store.codesys.com/

CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions


Top
   
PostPosted: Wed Feb 08, 2017 2:26 pm 
Offline

Joined: Thu Oct 13, 2016 4:19 pm
Posts: 45
Location: Presezzo, Bergamo (ITA)
Thank you very much.


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

All times are UTC+01:00


Who is online

Users browsing this forum: A. Akkerman 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:  
cron
Powered by phpBB® Forum Software © phpBB Limited