C to IEC 61131-3 IL compiler

12,194

Solution 1

Maybe this answer comes too late but it is possible to call C code from CoDeSys thanks to an external library.

You can find documentation on the CoDeSys forum at http://forum-en.3s-software.com/viewtopic.php?t=620

That would give you to use your C code into the PLC with minor modifcations. You'll just have to define the functions or function blocks interfaces.

Solution 2

My guess is that a C to Pascal translator will not get you near enough for being worth the trouble. Structured text looks a lot like Pascal, but there are differences that you will need to fix everywhere.

Not a bug issue, but don't forget that PLCs runtime enviroment is a bit different. A C applications starts at main() and ends when main() returns. A PLC calls it main() over and over again, 100:s of times per second and it never ends. Usally lengthy calculations and I/O needs to be coded in diffent fashion than a C appliation would use.

Unless your C source is many many thousands lines of code - Rewrite it.

Solution 3

It is impossible. To be short: the IL language is a 4GL (i.e. limited to the domain, as well as other IEC 61131-3 languages -- ST, FBD, LD, SFC). The C language is a 3GL.

To understand the problem, try to answer the question, which way to express in IL manipulations with a pointer? for example, to express call a function by a pointer. What about interrupts? Low level access to the peripherial devices?

(really, there are more problems)

BTW, there is the Reflex language, aka "C with processes". Reflex is a 4GL for the control domain with C-like syntax. But the known translators produce C-code and Python-code.

Solution 4

If the amount of code to convert is a few thousand lines, recoding by hand is probably your best bet.

If you have lots of code to convert, then an automated tool might be very effective. Using the DMS Software Reengineering Toolkit we've built translators to map mechanical motion diagrams into RLL (PLC) code. DMS also has full C parser/analyzers/front ends. The pieces are there to build a C to RLL code.

This isn't an easy task. It likely takes 6-12 man-months to configure DMS to something resembling what you want. If that's less than what it takes to do by hand, then its the right way to do it.

Solution 5

There are a few IEC development environments and target hardware that can use C blocks... I would also take a look at the reasons why it HAS to be an IEC-61131 complaint target. I have written extensively on compliance and why it doesn't mean squat. SOFTplc corp can help I'm sure with user defined loadable modules... and they can be in C..

Schneider also supports C function blocks...

Labview too!! not sure why IEC is important that's all!! the compiler if existed would create bad code for sure:)

Share:
12,194
Peter M
Author by

Peter M

Updated on June 04, 2022

Comments

  • Peter M
    Peter M almost 2 years

    I have a requirement for porting some existing C code to a IEC 61131-3 compliant PLC.

    I have some options of splitting the code into discrete function blocks and weaving those blocks into a standard solution (Ladder, FB, Structured Text etc). But this would require carving up the C code in order to build each function block.

    When looking at the IEC spec I realsied that the IEC Instruction List form could be a target language for a compiler. The wikepedia article lists two development tools:

    1. CoDeSys
    2. Beremiz

    But these seem to be targeted compiling IEC languages to C, not C to IEC.

    Another possible solution is to push the C code through a C to Pascal translator and use that as a starting point for a Structured Text solution.

    If not any of these I will go down the route of splitting the code up into function blocks.

    Edit

    As prompted by mlieson's reply I should have mentioned that the C code is an existing real-time control system. So the programs algorithms should already suit a PLC environment.

  • Peter M
    Peter M about 15 years
    I'm well versed in PLC and PC programming so I know all of the architectural differences and issues. But the C code is for a real time system of some sort (haven't seen the code yet - that is next week)