The Beckhoff TwinCAT platform is a powerful alternative to LabVIEW Realtime and National Instruments hardware. While the Beckhoff drivers provide a good basis for interfacing between a LabVIEW project and a TwinCAT project, they make heavy use of variants for data typing when both LabVIEW and TwinCAT are strongly typed environments. This package hides the variant intermediates, and most importantly, includes a script that automatically builds a full strictly typed interface to an existing TwinCAT project.
Contents
What is TwinCAT Strict?
The TwinCAT Strict GPM package installs into a project and provides a bunch of classes that wrap Beckhoff’s LabVIEW driver for TwinCAT, plus it provides a script you can run to build a complete interface to a TwinCAT project automatically. Before digging into the details of installation, usage, etc., let’s look at what the end product is.
A Finished Example
To use TwinCAT Strict, you will first need to create your TwinCAT project and include in it a set of global variables that will serve as the inputs from LabVIEW and the outputs to LabVIEW. A sample write operation might look like this
and a sample read operation might look like this
At the start of the program, a constructor has opened a connection to the TwinCAT controller and initialized connections for sending data to the controller (LVI.*) and receiving data from the controller (LVO.*). VI’s can be used for single read or write operations (e.g. Write Execute.vi in the writing example above.) or multiple reads or writes can be combined using property nodes. At the end of the program a destructor performs cleanup. The project looks like this
Digging Inside
Now that you’ve seen the full picture, you have the context for looking inside each of the pieces. As a user, you won’t have to write any of these pieces initially since the script will take care of that, but you might want to modify them later if you make changes to your interface.
Constructor
Here’s the block diagram of the Constructor
The Symbol Interface String is built using one of the VI’s provided in the Beckhoff LabVIEW driver, but the string has been modifed to make the AMSADDRESS effectively a variable instead of a constant. I’ve also cheated a little and run the Symbol Interface String through an online pretty print function to give it nice indenting. The version produced by the script doesn’t have nice whitespace. If you make changes later to the globals in your TwinCAT project there’s not a script for modifying so you’ll need to choose between regenerating the interface class from scratch or manually modifying just the parts you want to change.
Most of the work is done in the two private subVIs, Construct Readers and Construct Writers. To undertand them, it’s useful to look at the class variable we’re trying to initialize.
It contains clusters of reader and writer interface classes that determine each data type. These are the classes that are constructed in the two subVIs. Here’s the block diagram for Construct Readers
The for loop goes through all of the variables and constructs each one in its corresponding case structure. When a variable is a multi-dimensional array, the constructor has inputs for the array dimensions. The script automatically creates block diagram constants for those based on the contents of the Symbol Interface String.
Readers and Writers
The block diagram of each reader and writer is pretty dull since it’s just selecting one of the classes and calling its method. The only part that’s noteworthy is that typical properties for LabVIEW classes just extract data from the class wire while these are actually calling communication functions. If you dig down one layer deeper things are slightly more interesting. Here’s the block diagram for the Boolean read function
The parent of all the read classes has a Read Byte Array method which underlies every data type. Conversion functions specific to each data type convert it from a byte array to the type given by the class. Because MGI has not written classes for every potential data type, users may have to add odd types that they are using. Hopefully they’ll send copies back to MGI for inclusion in the package.
Script
After you install TwinCAT Strict, you’ll see the script inside your project.
However, because of file locking, the script must be run from a separate project so one for that purpose is included in the install: OpenAndRunScriptFromHere.lvproj. You can examine the script and see that it’s built using the VI Scripting Pattern.
How to Use it
Here’s what you need to install in order to make use of twincat-strict.
- LabVIEW version 2017 or higher.
- TwinCAT 3.
- The TwinCAT LabVIEW driver.
- G Package Manager since twincat-strict is distributed as a GPM package.
Now here are the steps to install twincat-strict itself and run the script to create your interface.
- In TwinCAT, create a project with two sets of global variables, one for inputs and one for outputs. I use LVI and LVO for them which I think is a good convention but not a requirement. Here’s a sample
- Activate your TwinCAT project.
- Create a LabVIEW project.
- Run GPM, select your project, click the Browse button, type in twincat in the search string, select twincat-strict, and click the Install Package(s) button at the lower right.
- After GPM completes the installation, navigate in your project to My Computer\GPM Packages\@mgi\twincat-strict\Scripting\OpenAndRunScriptFromHere.lvproj. Double click to open the project.
- In the newly opened project, double-click on Script.vi to open it. Run it.
- The TwinCAT Symbol Interface Configurator will open.
- Expand your controller and click on the 851 PLC port item.
- Drag your inputs global onto the ADS Write pane at the left and drag your outputs global onto the ADS Read pane at the right. Your window should now look like this
- Click OK and wait as the script performs the rest of the steps. This is faster or slower depending on how many global variables you have. The script displays which step it is currently performing.
- Your project will now contain a new class named after the project with all of the methods described earlier.
Open Source
The TwinCAT Strict package is an open source tool that we’ve developed at MGI. This means that we want your input (both code and ideas). All of the source code is hosted on a public accessible git repository. So if you find a bug or write the code to add a new data type, you can push it back up to us, so the community can benefit from it. If you’re not into fixing the code yourself, feel free to create an issue in the issue tracker and we’ll see about fixing it for you.
Report a Problem
Have you found an issue with TwinCAT Strict? Would you like to make a suggetion for improvement? Submit an issue via gitlab or email us directly at support@mooregoodideas.com