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. 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;
for function_block_type in instance.available_function_block_types:
    print(function_block_type)
// get available function block types
IDictionary<StringObject, FunctionBlockType> functionBlockTypes = instance.GetAvailableFunctionBlockTypes();
foreach (string functionBlockType in functionBlockTypes.Keys)
    Console.WriteLine(functionBlockType);

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");
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#

FunctionBlockTypePtr functionBlockType = functionBlock.getFunctionBlockType();
std::cout << functionBlockType.getId() << std::endl;
std::cout << functionBlockType.getName() << std::endl;
std::cout << functionBlockType.getDescription() << std::endl;
function_block_type = function_block.function_block_type
print(function_block_type.id)
print(function_block_type.name)
print(function_block_type.description)
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;

// Corresponding document: Antora/modules/howto_guides/pages/howto_add_function_block.adoc
int main()
{
    // Create an openDAQ(TM) instance, loading modules from the current directory
    InstancePtr instance = Instance(MODULE_PATH);

    // 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:
    print(function_block_type)

# if there is no statistics function block available, exit with an error
if 'ref_fb_module_statistics' not in function_block_types:
    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;