Python script: Launch Codesys, Execute Script, Exit Codesys

jst69
2024-01-16
2024-09-06
  • jst69 - 2024-01-16

    Dear all:
    Question about scripting: I am creating a .NET program that is supposed to Open codesys, open template project, export a bunch of pou, then exit codesys.

    Launch works, Open project works, Export works, But how do i tell codesys to close itself? I can tell windows to terminate codesys, but i would prefer to do it properly.

    from __future__ import print_function
    import sys
    import System
    proj = projects.primary
    # We're interested in POU nodes:
    POUGuid = Guid("6f9dac99-8de1-4efc-8465-68ac443b7d08")
    # We collect all POU nodes in that list.
    pous = []
    # From the parent node on, we recursively add POU nodes:
    def CollectPous(node):
        if node.type == POUGuid:
            pous.append(node)
        else:
            for child in node.get_children():
                CollectPous(child)
    # Now we collect all the leaf nodes.
    for node in proj.get_children():
        CollectPous(node)
    # We print everything just to know what's going on.
    for i in pous:
        print("found: ", i.type, i.guid, i.get_name())
    # And now we export the files.
    for candidate in pous:
        # We create a list of objects to export:
        # The object itsself
        objects = [candidate]
    
        # And sub-objects (POUs can have actions, properties, ...)
        objects.extend(candidate.get_children())
    
        # And the parent folders.
        parent = candidate.parent
        while ((not parent.is_root) and parent.is_folder):
            objects.append(parent)
            parent = parent.parent
    
        # Create an unique file name:
        if len(sys.argv) == 1:
            filename = "parent\\%s.export" % (candidate.get_name())
        else:
            filename = "%s\\%s.export" % (sys.argv[1],candidate.get_name())
    
        # print some user information
        print("exporting ", len(objects), " objects to: ", filename)
    
        # and actually export the project.
        proj.export_xml(objects, filename)
    
    proj.close()
    print ("script finished.")
    System.exit(0) // Dont work
    

    .NET:

            public static void Export(string path,string proj)
            {
                if (checkSettings())
                {
                    var p = new System.Diagnostics.Process();
                    p.StartInfo.FileName = Properties.Settings.Default.CSVersion +"\\CODESYS\\Common\\CODESYS.exe";
                    p.StartInfo.Arguments =
                        " --Profile="  + qoute(Properties.Settings.Default.CSProfile) + 
                        " --culture=en" +
                        " --project="  + qoute(path + "\\" + proj)  +
                        " --runscript=" + Properties.Settings.Default.LastOpenProjectPath + "\\INPUT_DATA\\SCRIPT\\Export.py" +
                        " --scriptargs:" + qoute(path)
    
    
                        ;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.CreateNoWindow = false;
                    p.Start();
                    p.StandardOutput.ReadToEnd();
                    p.CloseMainWindow();
                    p.Close();
                }
    
            }
    
     
  • mase - 2024-08-06

    Hi,

    sorry I canΒ΄t help you with your issue, but I hope you can give me a hint on getting GUID for other objects than POUs.

    Your code states "Guid("6f9dac99-8de1-4efc-8465-68ac443b7d08")". We can I find the GUID / ObjectIds`?

    I wasnΒ΄t able to get details about the GUID of DataTypes (STRUCT/ENUM,..)m, Functions (FUN), GLOBALS and TYPES.
    I want to create markdown of all available project elements.

    Thanks for your help

     
  • lyngaansns - 2024-09-06

    Hello

    I am looking for the same thing, so i don't have an answer, but in your case you could possibly use the --noUI flag when running codesys, if you don't care about showing what's happening.

     

Log in to post a comment.