C# scripting

These features require an Automation Toolbox license.

Otii has a build in TCP Server that can be used to control the application from another application, e.g. from a script running in your C.I. environment.

You can write your own TCP client using the Otii TCP Server API, or use one of the clients available at the Qoitech Github.

In this guide we will show you how to control Otii from a C# application using the Otii TCP Client for C#.

Installing the C# client

The Otii TCP Client for C# is avaliable as a NuGet package from nuget.org.

In Visual Studio, create a C#/.NET Framework project, right click on References in the Solution Explorer, and select Manage NuGet Packages....

In the NuGet Package Manager install OtiiTcpClient from the package source nuget.org. (If nuget.org not is listed as a package source, see here how to fix it.

Start the TCP Server

You need to start the Otii TCP server before connecting to it.

You can find out more about how to get started with the TCP server here:

Getting started

Make sure you have Otii running with the TCP server started, and an Arc connected to the computer without any devices connected to its main output.

We start with adding code to turn on and off the power of the Arc.

using System;
using System.Threading;
using Otii;

namespace Test {
    class Program {
        static void Main(string[] args) {
            // Calling Connect without parameters will connect to a local instance of Otii
            var client = new OtiiClient();
            client.Connect();

            // Create a local reference to the Otii property for convenience
            var otii = client.Otii;

            // Get a list of all Otii devices available
            var devices = otii.GetDevices();
            if (devices.Length == 0) {
                throw new Exception("No available devices");
            }

            // Get a reference to the first device in the list
            var arc = devices[0];

            // Turn on and off the power
            arc.SetMain(true);
            Thread.Sleep(2000);
            arc.SetMain(false);

            // Close the connection
            client.Close();
        }
    }
}

Run the program, and the Arc connected to your computer should turn on the power, and after a second turn it off again.

Handling projects

Before starting a measurement tou need to either create a project, connect to an already opened project, or open a previously saved project.

Here we connect to an open project if it exists, otherwise we create a new one.

            ...

            // Get a reference to the first device in the list
            var arc = devices[0];

            var project = otii.GetActiveProject();
            if (project == null) {
                project = otii.CreateProject();
            }

            ...

Configuring the project

Before measuring, we set the main output voltage, overcurrent protection., and we confiure and enable the UART.

Then we add the channels we want to records. In this case we enable Main Current (mc), Main Voltage (mv), UART logs (rx) and the digital input GPI1 (i1).

If you hover above the EnableChannel method in Visual Studio you can see what channels are available.

            ...

            var project = otii.GetActiveProject();
            if (project == null) {
                project = otii.CreateProject();
            }

            // Configuration
            arc.SetMainVoltage(3.3);
            arc.SetMaxCurrent(0.5);
            arc.EnableUart(true);
            arc.SetUartBaudrate(115200);
            arc.EnableChannel("mc", true);
            arc.EnableChannel("mv", true);
            arc.EnableChannel("rx", true);
            arc.EnableChannel("i1", true);

            ...

Do a timed recording

Replace the code in the section "Turn on and off the power" with the section Record shown below. You also need to add an constant in the main class defining the recording time.

        private const int RecordingTime = 30000;

        static void Main(string[] args)
        {
            ...

            // Record
            project.StartRecording();
            arc.SetMain(true);
            Thread.Sleep(RecordingTime);
            arc.SetMain(false);
            project.StopRecording();

            // Close the connection
            client.Close();
        }

You can find the Basic Measurment example at the Qoitech GitHub.

Last updated