Problem how to link c++ app with twincat 3

mhismail

Member
Join Date
Mar 2016
Location
egypt
Posts
30
Hello guys , i have the following problem :

i have a motor that runs a semi-vehicle on rails (AM series from beckhoff ) & a distance sensor ... and i have a c++ app written in vc++ 2010 (NOT twincat 3 c++ module ) , here is my problem in detail:

1) The c++ app works as the brain of the motor , it has functions that are able to do the following :

- func1: Calculate the motor required velocity & acceleration at different time steps , and after finishing write them to txt file on the desktop.

- func2: Based on the reading of the sensor , i go through some algorithms to make a decision for the next action of the motor

2) In twincat 3 , i made a plc program that can run the motor with the required velo & acc by first reading the txt file (when plc starts) and storing the values into an array & later using MC function blocks the motor would move with the required velo & acc

3) Now i faced the problem of func2 , the sensor role comes into action after the motor has already moved ( so c++ app is closed & txt file is already read by twincat plc ) ... how to go back to c++ and tell him " take this sensor reading and give me your analysis results " ?? and if i need to go back and forth between Twincat & vc++2010 (sounds crazy to me during runtime) how to do this ??

a) should i forget about my HUGE c++ code (which is OOP based) and try to make every thing written inside twincat with plc ST !!

b) should i forget about using twincat , and go for some library that makes the vC++ 2010 able to communicate directly with the motor driver ( or beckhoff cx embedded pc) , so now the c++ app will "think" & "act" ... if this the solution , what would substitute the twincat MC library that directs the motor motion ??and what about other I/O like the sensor reading itself ??

c) Is there a method that would make vc++ (brain) talk to twincat plc (action) and vise versa when needed just like two computers or two micro-controllers can talk to each others on demand ? what will happen in the Runtime ? who will have the "focus" of the time tick ? Does using "multiple virtual plcs based on multi core processor " help ?

I am sorry if typed a lot , but i really need help guys and beckhoff documentation is helpless !!

Thank you in advance
 
I suggest "A".
And this is one reason some projects are better done by PLC's than by custom high level coding.
 
The problem is how to convert everything into ST ?? i know you are right , i myself will trust twincat plc for real time .. but still i need to communicate with the c++ app (it is win32 by the way) because somethings can't be converted into ST like classes , inheritance , vectors ,pointers and so on
 
In TwinCAT 2 there was a method of turning a C++ program into a function block for use in the PLC program. This was so sparsely documented that I think very few people ever figured out how to make it work. Maybe in TwinCAT 3 it still is possible. You can look around the CodeSys documentation and may be able to find something.
 
@Archie

I made some search in the past 3 days & found out a library called "tcADS.dll"
TcADS.dll is part of the ADS Communication Library which allows an external program to read/write variable values in a TwinCAT program. It will not give you real time integration to a TwinCAT program such as a function block would.
 
@Archie

ok , i don't want the c++ to do everything , let me describe how i want c++ to work with twincat :

During RUN time ( i don't need real time for c++, but run time communication , i.e i don't want to drive the motor using c++ ) :
1) c++ generates file for motor velocity & acc for different time steps

2) twincat read the file generated by c++ and start moving the motor

3) c++ get input from the sensor & do some analysis then write another file with new instructions for the motor

4) twincat read the new file & move the motor according to the new instructions.

I can code each part of those alone , but how to link them together DURING RUNTIME and if one of them (c++ & twincat PLC) is working what is the other one doing ? for example :

let's assume i will use ads.dll ,

a) Is TCads.dll going to work online (run time) ?

b) at step (1) above , what is twincat doing while c++ is writing the first file ( it can do nothing like a blank if statement )?

c) how will twincat knows that c++ finished the file ( maybe c++ through ads would write a value in a digital input & twincat read that value ) ?

d) if step (2) succeeded what will the c++ do while twincat is moving the motor ( i can search for something to make it go sleep until (3) )

e) for step (3) , how will c++ knows the sensor has made a reading ( may be again like before , twincat writes a value in digital output & c++ reads it )

sorry if i have many questions
 
This is how I would go about this:

- Use the NT_StartProcess function block in TwinCAT to launch the C++ program.
- After the C++ completes, use the ADS library to set a BOOL value in TwinCAT to let it know it is complete.
- When TwinCAT sees the BOOL value go true, it can then read the file
- The C++ will then continue to monitor BOOL values in TwinCAT using ADS to know when to write another file

Plan for up to 100ms for worst case scenario of the C++ to use the ADS library to read/write values with TwinCAT
 
@Archi

OOOH Oh my God !! i spent A LOT OF TIME googling "twincat run external application" but never found this "NT_StartProcess"

Archi , that is exactly what i was wishing for .. I can't tell how happy i am right now .. I couldn't imagine a better solution than this (Twincat PLC will master everything )

about the "100ms" is this the value of "TimeOut" in NT_StartProcess ?

If you mean by "100 ms" the timeout of the ads functions used inside the c++ code , well i checked ads.dll on beckhoff website and found that the standard timeout is 5000 ms
 
Last edited:
about the "100ms" is this the value of "TimeOut" in NT_StartProcess ?

If you mean by "100 ms" the timeout of the ads functions used inside the c++ code , well i checked ads.dll on beckhoff website and found that the standard timeout is 5000 ms
The 100ms is just a general rule I use when using ADS communication library. Since it is not real-time, it has to wait for Windows to decide when it gets priority to send the value to TwinCAT. Most of the time it will be about 10ms, but once in a while Windows goes off and does another task.

In other words, if you are hoping to read something like the status of a limit switch that will only stay on for 50ms, do not rely on ADS communications with TwinCAT to always catch that event 100% of the time.
 
oh , ok i see what i mean ... i will do my best not to go less than 100 ms ( i trust your experience :D )

I downloaded some pdfs on ADS communication , i need to check them to learn more about ADS .. but in general , you answered my questions in a very simple and very helpful way .

Truly Thank you a lot Archi , you really helped me ... Thank you again for your fast & helpful answers .
 

Similar Topics

Hi forum! I am using a unit called IBH Link UA which have a built-in soft Siemens S7-PLC, OPC server and OPC client function. I have mostly used...
Replies
2
Views
2,012
Hi All,I had done up a PLC program which requires CC-Link. Program has no problem but when i try to write into the HMI (Mitsubishi GT15) the lamp...
Replies
0
Views
2,386
Hello, I have the NETlink USB compact. It works perfectly fine when i use it on a Siemens 300 PLC. But it won’t work with the Beckhoff 315 PLC. I...
Replies
3
Views
2,089
I have a problem when I use RSLogix5000 to read value from PowerFlex 700s by datalink. The returned value is not same with the value on PowerFlex...
Replies
4
Views
3,396
hi everyone! I have a problem when I use RSLogix5000 to read value from PowerFlex 700s by datalink. The returned value is not same with the value...
Replies
0
Views
2,518
Back
Top Bottom