Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Get Variables from GVL

Anonymous
2017-01-13
2017-02-08
  • Anonymous - 2017-01-13

    Originally created by: Massimo.Milluzzo

    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

     
  • mkeller - 2017-01-13

    Hi Massimo.

    Massimo.Milluzzo hat geschrieben:
    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:

    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 hat geschrieben:
    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

     
  • Anonymous - 2017-01-16

    Originally created by: Massimo.Milluzzo

    Hi Martin,
    Thank you very much for the reply, it is very useful

    Best regards.
    Massimo

     
  • Anonymous - 2017-01-30

    Originally created by: Massimo.Milluzzo

    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

    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

    IMG: XMLError.png

    IMG: XMLError2.png

     
  • Anonymous - 2017-01-30

    Originally created by: Massimo.Milluzzo

    Update:

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

    <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>
    
     
  • Anonymous - 2017-01-30

    Originally created by: Massimo.Milluzzo

    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.

     
  • mkeller - 2017-01-30

    Hi Massimo.

    Massimo.Milluzzo hat geschrieben:
    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:

    \# Load XML file
    xmlfile = io.open(r"c:\tmp\test_gvl.xml", "r")
    xmlfile.seek(3)
    tree = ET.parse(xmlfile)
    xmlfile.close()
    

    BR
    Martin

     
  • Anonymous - 2017-01-31

    Originally created by: Massimo.Milluzzo

    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

    Regards,
    Massimo

     
  • Anonymous - 2017-02-07

    Originally created by: Massimo.Milluzzo

    Hi all,
    here I'm again. How can I export a visualization page? When I try I get the error:

    [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

     
  • mkeller - 2017-02-07

    Hi Massimo.

    Massimo.Milluzzo hat geschrieben:
    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 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

     
  • Anonymous - 2017-02-08

    Originally created by: Massimo.Milluzzo

    Thank you very much.

     

Log in to post a comment.