Search code examples
gitplctwincatcodesys

Using Version Control Software (Git) with a CoDeSys 2.3 PLC project


I'm looking to use Git with a CoDeSys 2.3 PLC project. By default, the project is stored in one large binary file. Git can detect that changes have been made to this file, but it can't see into file to know what has changed.

I'm looking for a solution that stores the code in a text file or XML file that Git can read and therefore is able to show changes. It would be beneficial if the solution required a minimum amount of manual intervention to work.

In my research I have found that the source code can be exported into plain text using the Export function in the menu (Project -> Export...). There is also an import function for pulling the source code back into the PLC project.

I think these two tools have the functionality that I'm looking for, but I would like to find a way to make the process a little more automated. Ideally this would be a script that is called automatically as part of the build process, after saving the project or perhaps when calling git-status.


Solution

  • I have found a partial answer to my own question here: https://forge.codesys.com/forge/talk/CODESYS-V2/thread/d1685e2948/

    CoDeSys 2.3 has support for running a list of commands from a command file. This file is passed to the program as an argument in when called in command line.

    A powershell script that exports the PLC code to a specified location:

    #This PowerShell script opens CoDeSys 2.3 (without GUI) and asks it to export the source code plain text into a specified location. 
    
    #Path to the CoDeSys 2.3 .exe
    $EXE_PATH = 'C:\Program Files (x86)\ifm electronic\CoDeSys V2.3\Codesys.exe'
    
    #Path to the CoDeSys PLC project file (.pro) relative or absolute
    $PROG_PATH = '.\PLC PROGRAM.pro'
    
    #Path to export folder relative or absolute.
    $OUT_PATH = '.\SOURCECODE EXPORT'
    
    #Let the user know what is happening and give them a chance to abort
    Write-Output "Removing exisiting files from and exporting CoDeSys Files to: $OUT_PATH"
    pause
    
    #Check the program exists
    if (-NOT(Test-Path "$PROG_PATH"))
    {
        Write-Output "Specified program file doesn't exist"
        pause
        exit
    }
    #Remove exisiting export files
    if (Test-Path "$OUT_PATH")
    {
        Remove-Item ".\$OUT_PATH\*.EXP"
    }
    
    #Create a .txt file with the commands for CoDeSys.
    Write-Output "Creating command file..."
    Out-File -FilePath .\CoDeSys_CMD.txt -InputObject "project expmul `"$OUT_PATH`"" -Encoding ASCII
    
    #open CoDeSys 2.3 project as specified. Passes command file to the opening program. /batch causes codesys to open without GUI. Swap with /cmd for GUI. 
    Write-Output "Exporting files..."
    start-process -Wait -FilePath $EXE_PATH -ArgumentList "`"$PROG_PATH`"" ,"/batch `".\CoDeSys_CMD.txt`""
    
    #Remove command file once finished.
    Write-Output "Removing command file..."
    Remove-Item CoDeSys_CMD.txt
    Write-Output "Done."
    

    I'm not sure if the export files fully describe the project. There may be important data that remains in the .pro file. So, the .pro file will also remain in the git repo. Given that, I decided not to automate the import function as it will only occasionally be required.