Setting up (C#)

To start working with openDAQ™, the requisite binaries are required. They can be obtained from http://nuget.org (the recommended way) or from https://docs.opendaq.com (https://docs-dev.opendaq.com for the development version).

The .NET binaries are available from the Windows™ SDK packages, but they only contain the Windows™ libraries.
For both, Windows™ and Linux™ all binaries are available in the NuGet package, which is the recommended consumption method for .NET developers.

All commands in this guide use the .NET command-line interface (dotnet CLI) which is a cross-platform toolchain for developing, building, running, and publishing .NET applications.

Creating an openDAQ™ project

Below, we start a Console Project in Visual Studio™ from scratch but there are also example files which can be found in the examples archive from https://docs.opendaq.com.

Requirements

  • Windows

  • Linux

  • .NET compiler, e.g.

    • .NET SDK (used in this guide).

    • Visual Studio 2022™.

    • Visual Studio Code with extension "C# Dev Kit".

On Windows there is the Windows Subsystem for Linux (WSL),
where .NET projects can also be created (use an Ubuntu 22.04 distribution).

In general everything around .NET can be found here:
https://dotnet.microsoft.com/en-us/download/dotnet/8.0

In this guide we use a Developer PowerShell for VS2022 prompt on Windows™ for creating and building projects.
To use Linux™ as a Windows™ user, the Windows Subsystem for Linux (WSL) can be used.

Creating and building the project

With the following commands in PowerShell™ or WSL/Linux™ Bash:

  • We create a csharp/quick_start directory and navigate to it.

  • Then we create an empty (.NET8.0) console application project (named by its folder).

  • Then we add the openDAQ.Net Bindings NuGet package reference (from nuget.org) to the project, which contains all the needed binaries for Windows(TM ) and Linux™.

mkdir ./csharp/quick_start
cd ./csharp/quick_start
dotnet new console
dotnet add package openDAQ.Net
# dotnet add package openDAQ.Net --source path/to/folder --prerelease
Alternatively, the latest version of the .NET Bindings is available at https://docs-dev.opendaq.com/ and can be used via:
dotnet add package openDAQ.Net --source path/to/folder --prerelease
See also Learn NET - dotnet add package

With this, we are ready to start developing:
We fill in our Program.cs executable code as follows (replacing the default content):

using Daq.Core.Types;
using Daq.Core.Objects;
using Daq.Core.OpenDAQ;

// Create a fresh openDAQ(TM) instance, which acts as the entry point into our application
Console.WriteLine("create instance...");
var instance = OpenDAQFactory.Instance();

Console.WriteLine();
Console.WriteLine($"successfully created the instance '{instance.Name}'");

Console.WriteLine();
Console.Write("Press a key to exit the application ...");
Console.ReadKey(intercept: true);
When creating the openDAQ™ instance, we don’t need to specify the root directory of our openDAQ™ Modules as they are conveniently being deployed by the NuGet package and found where they are.

Now we can compile and run our program in our PowerShell™ or WSL instance from above.

dotnet run --no-restore
--no-restore tells the compiler not to restore (NuGet) packages. They have been restored/installed already by the dotnet add package command above.
This is particularly useful if you are using a local package, as it cannot be found without specifying its location.

With the run command the project is being built (when there was a change after the last run) and the resulting quick_start.exe is executed.
All binaries from the Nuget package will be copied to the target directory (which defaults to ./bin/Debug/net8.0). These dependencies are also entered in the file quick_start.deps.json so that the executable can find the openDAQ™ SDK binaries during execution.

In case a new version of the openDAQ™ NuGet package is available, you need to update the version in your project file by calling the add-package command again (one of the two, depending on the package source):

dotnet add package openDAQ.Net
# dotnet add package openDAQ.Net --source path/to/folder --prerelease

Congratulations, if you have managed to reach this point, you have successfully created your first openDAQ™ project!
To find out more about using openDAQ™ in applications to connect to, configure, and read data of Devices, continue with the openDAQ™ application quick start guide.