Running C Code on a PLC

Almost all PLC programming makes use of IEC 61131-3 programming languages such as Ladder Diagram, Structured Text, or Function Block Diagram, which can at times limit the functionality of a control system. Fortunately, GE’s PLCs have the option of using C function blocks thanks to the PACSystems C Programmer’s Toolkit, which can allow complex tasks to be handled much easier, ultimately leading to a more streamlined process with greater functionality.

In this Tech Tip, we’ll show you how to create and run blocks of C code on an Rx3i system using Proficy Machine Edition (PME). 

CREATING AND COMPILING YOUR CODE

First, download and install the C Programmer’s Toolkit for PACSystems here.

The next step is to write some code you’d like to run on your PLC. You can do this in any text editor as long as the file is a .c format and is saved in the toolkit’s directory (C:\GE Software\PACSystemsCToolkit\Projects). Here is an example program which scales a 4-20mA signal to a 0-10,000 range (rescaling).



This program makes use of the Interpolation Formula:


There are a few things to note here:
  • You must include the PACRCPlc.h file;
  • The main program must be called GefMain;
  • Inputs AND outputs must be in the declaration of GefMain;
  • The datatypes of the inputs and outputs have different syntaxes. For example, a T_REAL32 is the same as a float in normal C;
  • Inputs and outputs must be referenced as pointers when called, and
  • GefMain must always return GEF_EXECUTION_OK.

Once your program is ready, run the Toolkit console and navigate to the directory your program is in. Compile the program by running the compileCPACRX <filename> command. After the program has been successfully compiled, a folder called plc will appear in the directory. Within the plc folder will be a file titled <filename>.gefElf.

INCORPORATING CODE INTO PME

Set up the hardware configuration and communication settings so that you can get online to your PLC. In the Navigator window, expand the Target and then Logic menu to show the Program Blocks.


Right click on Program Blocks and click Add C Block. Select the .gefElf file in the plc folder from earlier. Once the C block has been added, we need to set the input and output parameters for it. Do this by right clicking the block, in this case Scaling Example, and click properties. In the Inspector window that opens, click the three dots in the parameters section.



Set the input and output parameters for the block. In this example we have an Input which is a REAL, and an Output which is also a REAL.

C blocks are added to programs via CALL functions. Place a call function in a program by selecting where you want the block to appear and type CALL. Once the call block has been placed, double click it and select the C block you wish to call. Populate the inputs and outputs with variables and we’re done!



Above is the program running on an Rx3i, showing an input of 12.5 being rescaled to an output of 5312.5.

Now you should be able to write more complex code in standard C and incorporate it into your PLC system.

Contributors: