Problem C++ threads
Dear community,
I am trying to implement a program in C++ to later upload it to PLCnext, the program in question must create two threads, to test it I made the code that creates and destroys some directories.
When loading it for the first time in the AXC F2152 it gave me an error and the driver was blocked.
Reading I found that this error could be due to the watchdog, so I created a new cyclic task, set the watchdog to zero and in this task I execute the program in C++, now it works without problems.
Could someone tell me if I'm correct that the failure was due to the watchdog?
Is executing the program setting the watchdog to 0 correct?
Could it give me problems that I am not seeing now?
Thank you very much.
Comments
Could someone tell me if I'm correct that the failure was due to the watchdog?
Yes.
Is executing the program setting the watchdog to 0 correct?
It's not advisable to disable the task watchdog by setting the value to 0, except while debugging/troubleshooting, or to set the value to greater than the task cycle time.
The purpose of an IEC 61131 cyclic task is to execute all the program instances in that task, one after the other, once every cycle period. The sum of the execution times of all the program instances in a task should never be more than the task period, and the Task Watchdog is a way of guarding against bugs in the application that might cause programs to execute for longer than the task period. For example, if there are any loops in a program that take a "long" time to execute, then the execution time of the program instance might exceed the task period, which is something that should be avoided in IEC 61131 cyclic tasks.
In your case, if I am reading the code correctly, the C++ program instance will block the task execution forever, because the Execute method joins two threads that will never terminate.
In this case, if a deterministic cyclic task is not a requirement of the application, then it might be better to implement the solution in a Component rather than in a Program.
Could it give me problems that I am not seeing now?
Yes. If an IEC task is blocked forever with the watchdog disabled, this is likely to cause problems.
First thank you very much for answering, I’m new to PLCnext and I’m pretty lost.
From what you have explained I would be interested to implement the solution in a Component.
Now the question is how to achieve this, reading I understand that right now in my program I have a PLM component that should convert to an ACF Component.
Is this right?
If it’s right, what’s the best way to do it?
As in the videos of Coding Diary Day 03 add 04?
Thank you very much.
I understand that right now in my program I have a PLM component that should convert to an ACF Component.
Yes, if you have a project with a C++ program then that project will also include a C++ PLM Component.
You could use the PLM Component to implement your solution, since an instance of this component is automatically created when a program instance is created by the PLCnext Engineer project, even if that program has no code in the Execute method.
If you prefer to use a component that is not affected by starting/stopping/reloading the PLCnext Engineer project, then you could implement the solution in an ACF component.
There is some information in the Info Center on programming C++ PLM/ACF Components, including an interactive training course.
There are also some (non-Phoenix Contact) resources that might help get started with component programming, e.g. section 4.8 of this online book builds a simple ACF component:
I have also asked someone from your local Phoenix Contact office to contact you. They will be able to help with any other questions you might have about C++ programming on PLCnext Control devices.
Hello again,
From what you have explained I will use a PLM Component, this component to execute the previous code since it will not have the problems that gave me the watchdog and I will be able to use the program to later add other elements that do need a real-time execution.
Now I was trying to use the default component when creating a project in Eclipse and similar to what I had at the beginning, I created and destroyed a folder (I’m checking it in the simulation). However, I find that it is not working.
From what I saw, should /opt/plcnext/projects/Default/Plc/Plm already have auto-generated files? I ask because Plm is empty, even assigning the program to PLCnext enginner.
Am I missing something else to add to the project or what am I doing wrong?
Also thank you very much for getting me in touch with the staff of the local office, they haven’t contacted me yet and I really need help.
Thank you so much for your help.