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:
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 ofint16
. Valid C++ port data types are shown in this table.Here is an example in a Program.hpp file:
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: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:
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:
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:
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:
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:
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.
=========================
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!