Programmatically Discover and Access EtherCAT I/O Items

Updated Jan 17, 2023

Environment

Software

  • LabVIEW Real-Time Module

Driver

  • NI-Industrial Communications for EtherCAT

I am using the NI-Industrial Communications for EtherCAT driver to access EtherCAT slaves from my National Instruments EtherCAT master, but I would like to programmatically access the I/O data rather than using the static nodes available in the project. How can I do this?

In order to access modules at run-time, the programmatic shared variable API must be used. This API is available in the Data Communication»Shared Variable sub-palettes of the Functions Palette.
 
  1. Refresh I/O Modules
In order to programmatically discover slaves, you must first refresh the modules.
This can be done manually through the project by right-clicking on your EtherCAT Master and selecting New»Targets and Devices. From the dialog, select the existing target or device bullet and navigate to the appropriate device. If you do not see your device, you may need to import the device profile as outlined in 
Adding Third-Party EtherCAT® Slaves in LabVIEW .
 
You can also perform this operation programmatically using the Refresh Modules VI located on the Functions Palette at Measurement I/O»NI Scan Engine. If you get an error when running this VI, you may need to switch the NI Scan Engine to configuration mode using the Set Mode VI, available in the same above location. An example of this sequence can be seen here:
Figure 1. Refresh Modules
 
Note: If you are using third-party slaves, this solution will not discover your slaves unless you manually move the XML file over to the master, as discussed in Programmatic Discovery of Third-Party EtherCAT Slaves Fails. Additionally, please note that this will attempt to refresh any slaves set in the project if specified, rather than query the connected slaves for configurations.
  1. Enumerate EtherCAT Masters
To enumerate EtherCAT masters, you must locate all children of the NI I/O Engine and then determine which child is an EtherCAT master. To do this, you must use the Children[] property of the I/O Variable Container class.  The input to the property node should be a variable constant with the path ni.var.io:/. Once children are enumerated, you must cast the variable refnums output by the property node to the IndComECAT Master class using To More Specific Class VI. If the refnum is not referencing an EtherCAT Master, the function will error and that refnum should be ignored. An example of this can be seen here:
Figure 2. Enumerate EtherCAT Masters
 
  1. Enumerate EtherCAT Slaves
To perform this operation, you must locate all children of the masters located above. Similarly to the process outlined above, the children of the master can be located using property nodes, and then the To More Specific Class VI should be used to convert those refnums into the IndComECAT Slave class. Once again, if the To More Specific Class VI fails, that refnum should be ignored. An example of this process can be seen below:
 

Figure 3. Enumerate EtherCAT Slaves

If you are only interested in retrieving variables associated with the slave, or if you are using a third-party EtherCAT slave, the I/O Variables associated with that slave can be discovered in Step Five, below.
 
  1. Enumerate RSI Modules
National Instruments EtherCAT slaves can be further manipulated using property nodes. The C Series modules installed in NI EtherCAT slaves are referred to as RSI modules and can be accessed individually if needed. Enumerating these modules follows the same pattern as above, as can be seen here:

Figure 4. Enumerate RSI Modules

We also have the ability to restrict our selection to specific modules. To do so, simply change the class input to the To More Specific Class VI from RSI Module to one of the RSI Modules seen here:
 

Figure 5. RSI Modules
 
  1. Enumerate I/O Items
Now that you have located the module or third-party slave you wish to locate variables for, you can use the same method to enumerate these I/O Variables. In this case, we use the AllVariables[] property, which provides us with references to the I/O Variables directly, without having to cast those references into the Variable class. This method is seen here, along with a For Loop to open those references and establish a connection with each variable: 
 

Figure 6. Enumerate I/O Items

Additional Information

Using the Search Variable Container VI:
The above steps can also be accomplished using the Search Variable Container VI, although the process provides less control. An example of this can be found in the NI Example Finder by navigating to Hardware Input and Output/NI-Industrial Communications/EtherCAT/Advanced/Startup Application.lvproj.