Skip to content

A simple blink program in C++ to kickstart learning

Hi,

 

I'm just getting started with PLCnext C++ and was wondering if you had a simple "blink" program for which I can just blink one of my outputs on one of the Digital Output modules for my AXCF 2152. I couldn't find anything related to this on the blog and everything there seemed esoteric and perhaps for advanced Linux users. Can you send an example program so I can at least easily see how it is structured with the main() and whatnot? I've already worked with the PLCnext C++ "myfirstint" example found on youtube but there's no real sample program behind that that does anything tangible. Great start though.

 

Thanks,

 

Brendan

Comments

  • Hi Brendan,

    If you've got the example on YouTube working, then there are only a few more steps to do a C++ program like the one you describe.

    The example below includes these steps:

    • In Eclipse (C++):
      • Define the program OUT port that will contain the "blink" value (ON or OFF).
      • Write a program to toggle the blink value
    • In PLCnext Engineer:
      • Define the physical I/O.
      • Connect the blink port from the C++ program to a physical output.

     

    1. In Eclipse, define the OUT port

    You do this in exactly the same way as shown in the video, but the type of the variable must be boolean instead of int16. Valid C++ port data types are shown in this table.

    Here is an example in a Program.hpp file:

    //#port
    //#attributes(Output)
    boolean blink;

     

    2. In Eclipse, write the program

    The program to toggle the blink value will written in the Program's Execute method, which will be called every time the program runs. There are as many different ways to write this as there are programmers, but here is one example:

    void MyProgram::Execute()
    {
    this->count++;

    if (this->count > 10)
    {
    this->blink = !this->blink;
    this->count = 0;
    }
    }

    The above code snippet uses a private integer called count, which is also declared in the Program.hpp file.

     

    3. Create an instance of the program in PLCnext Engineer

    This follows the same steps as in the video:

    • Build the project in Eclipse.
    • Import the resulting .pcwlx file into PLCnext Engineer.
    • Create an instance of the C++ program in a cyclic task.

     

    4. Define the physical I/O

    In PLCnext Engineer, define the physical I/O like you normally would for a PLCnext Engineer project. Here is an example showing an 8 channel DI/DO module:

    2020 02 16 130026

     

    5. Connect the C++ port to a digital output

    In the PLCnext -> Port List window, connect the OUT port from the C++ program to a physical digital output.

    Here are before and after pictures of a connection between the C++ port and the first digital output (OUT00) on the I/O module:

    2020 02 16 130437

     

    2020 02 16 1307

     

    6. Download and run the project

    After downloading the project to the PLC, you should see the digital output blinking.

     

    Let us know if you have any questions about any of these steps.

    ~ Martin.

     


  • Hey Martin,

     

    Thanks for your response, I followed the directions given but unfortunately still couldn't get it to blink. The program seemed to compile fine and got no errors when uploading to the controller.


    On the AXCF 2152 controller, I noticed that the 'D' LED is flashing red at ~2Hz. Attached are screenshots of what I have for the PLCnext project and the pdf versions of the BlinkProgram cpp and hpp files in pdf format. Also, I didn't add a 'main' instance or do anything with the main as per the youtube video, doesn't really seem like this would be an issue, but worth mentioning.

     

    One last thing, I could have sworn there were a bunch of tutorials on how to use PLCnext somewhere in the software itself (not online). Where might I find this? I saw it once, then couldn't track it down.

     

    Hope to be up and running with this soon, thanks for your help,

     

    Brendan

     

  • OK, if the D LED is flashing red then here is what that indicates (from the manual). I have added a red box showing the most common cause:

    2020 02 17 8 22 29

    In your case I am guessing that the actual hardware configuration does not match the hardware configuration you have specified in PLCnext Engineer. This will need to be fixed first, so please check:

    • The 7-digit part numbers for all I/O modules actually connected to the PLC.
    • That these match the 7-digit part numbers of the I/O modules configured in PLCnext Engineer, and that the I/O modules in PLCnext Engineer appear in the same order as the physical I/O modules.
    • The I/O modules are connected to the CPU properly via the DIN rail connectors.
    • The I/O modules are powered with a 24 VDC supply.

    After this is fixed, the D-LED on the CPU should be solid green. Then, hopefully the digital output will reflect the value of the C++ variable connected to it.

    =========================

    tutorials on how to use PLCnext somewhere in the software itself (not online).

    If by "PLCnext" you mean PLCnext Engineer software, then there is an offline Help system that comes with this software, which you can reach by selecting "View Help" from the Help menu. There is a "Quick Start" guide in this help system, but not any other tutorials - these tutorials have always been online, here:

    Getting started with PLCnext Engineer which is about programming in IEC 61131-3 languages

    ~ Martin.

     

     

     

  • I actually found that I needed to change the order of the modules. I attached a picture of where that is. Thanks again for your help!

Sign In or Register to comment.