Add Function Block

openDAQ™ provides processing features through Function Blocks. They can process Signals either on a Device or on a host PC where the SDK is running.

The SDK is bundled with a few Function Blocks:

  • Renderer (displays Signal on a desktop window)

  • Statistics (calculates average and RMS from an input Signal)

  • Power (calculates DC power from the input voltage and current Signal)

These Function Blocks serve as an example of what is possible to achieve with openDAQ™ SDK. They are not meant to be used in production code.

The following examples use the Statistics Function Block to show how to add a Function Block. The same methods can be used to add other function blocks.

Get a list of available function blocks

The SDK provides a method to get a list of all available types of Function Blocks. This step is optional if the application uses a fixed configuration of modules and the available Function Blocks are predefined.

The result is a dictionary with Function Block id as a key and Function Block type information as a value of the dictionary.

  • Cpp

  • Python

  • C#

// Get available Function Block types
DictPtr<IString, IFunctionBlockType> functionBlockTypes = instance.getAvailableFunctionBlockTypes();
for (const auto& functionBlockType : functionBlockTypes.getKeys())
    std::cout << functionBlockType << std::endl;
# Get available Function Block types
function_block_types = instance.available_function_block_types
for function_block_type in function_block_types.keys():
    print(function_block_type)
// Get available Function Block types
IDictObject<StringObject, FunctionBlockType> functionBlockTypes = instance.GetAvailableFunctionBlockTypes();
foreach (string functionBlockTypeName in functionBlockTypes.Keys)
    Console.WriteLine(functionBlockTypeName);

Add a Function Block

To create and add a Function Block, specify its id in the add Function Block method. If there is no such Function Block available, the method will fail. The result is the Function Block object.

  • Cpp

  • Python

  • C#

// Add Function Block on the host computer
FunctionBlockPtr functionBlock = instance.addFunctionBlock("ref_fb_module_statistics");
# Add Function Block on the host computer
function_block = instance.add_function_block("ref_fb_module_statistics")
// Add Function Block on the host computer
FunctionBlock functionBlock = instance.AddFunctionBlock("ref_fb_module_statistics");

Get function block type information

It is possible to get additional detailed information about the type of the Function Block:

  • through Function Block instance,

  • through dictionary returned by get available function blocks method.

  • Cpp

  • Python

  • C#

// Print Function Block type info
FunctionBlockTypePtr functionBlockType = functionBlock.getFunctionBlockType();
std::cout << functionBlockType.getId() << std::endl;
std::cout << functionBlockType.getName() << std::endl;
std::cout << functionBlockType.getDescription() << std::endl;
# Print Function Block type info
function_block_type = function_block.function_block_type
print(function_block_type.id)
print(function_block_type.name)
print(function_block_type.description)
// Print Function Block type info
FunctionBlockType functionBlockType = functionBlock.GetFunctionBlockType();
Console.WriteLine(functionBlockType.GetId());
Console.WriteLine(functionBlockType.GetName());
Console.WriteLine(functionBlockType.GetDescription());

Full listing

The following is a fully working example of listing available and adding Function Blocks.

The full example code listing
  • Cpp

  • Python

  • C#

#include <opendaq/opendaq.h>
#include <iostream>

using namespace daq;

int main()
{
    // Create an openDAQ(TM) Instance, loading modules from the current directory
    InstancePtr instance = Instance();

    // Add simulated device
    DevicePtr device = instance.addDevice("daqref://device0");

    // Get available Function Block types
    DictPtr<IString, IFunctionBlockType> functionBlockTypes = instance.getAvailableFunctionBlockTypes();
    for (const auto& functionBlockType : functionBlockTypes.getKeys())
        std::cout << functionBlockType << std::endl;

    // If there is no Statistics Function Block available, exit with an error
    if (!functionBlockTypes.hasKey("ref_fb_module_statistics"))
        return 1;

    // Add Function Block on the host computer
    FunctionBlockPtr functionBlock = instance.addFunctionBlock("ref_fb_module_statistics");

    // Print Function Block type info
    FunctionBlockTypePtr functionBlockType = functionBlock.getFunctionBlockType();
    std::cout << functionBlockType.getId() << std::endl;
    std::cout << functionBlockType.getName() << std::endl;
    std::cout << functionBlockType.getDescription() << std::endl;

    return 0;
}
import opendaq

# Create an openDAQ(TM) Instance, loading modules from the current directory
instance = opendaq.Instance()

# Add simulated device
device = instance.add_device('daqref://device0')

# Get available Function Block types
function_block_types = instance.available_function_block_types
for function_block_type in function_block_types.keys():
    print(function_block_type)

# If there is no Statistics Function Block available, exit with an error
if not "ref_fb_module_statistics" in function_block_types.keys():
    print('Function block not found')
    exit(1)

# Add Function Block on the host computer
function_block = instance.add_function_block("ref_fb_module_statistics")

# Print Function Block type info
function_block_type = function_block.function_block_type
print(function_block_type.id)
print(function_block_type.name)
print(function_block_type.description)
using Daq.Core.Types;
using Daq.Core.Objects;
using Daq.Core.OpenDAQ;

// Create an openDAQ(TM) Instance, loading modules from the current directory
Instance instance = OpenDAQFactory.Instance(MODULE_PATH);

// Add simulated device
Device device = instance.AddDevice("daqref://device0");

// Get available Function Block types
IDictObject<StringObject, FunctionBlockType> functionBlockTypes = instance.GetAvailableFunctionBlockTypes();
foreach (string functionBlockTypeName in functionBlockTypes.Keys)
    Console.WriteLine(functionBlockTypeName);

// If there is no Statistics Function Block available, exit with an error
if (!functionBlockTypes.ContainsKey("ref_fb_module_statistics"))
    return 1;

// Add Function Block on the host computer
FunctionBlock functionBlock = instance.AddFunctionBlock("ref_fb_module_statistics");

// Print Function Block type info
FunctionBlockType functionBlockType = functionBlock.GetFunctionBlockType();
Console.WriteLine(functionBlockType.GetId());
Console.WriteLine(functionBlockType.GetName());
Console.WriteLine(functionBlockType.GetDescription());

return 0;