C# 脚本编写
以下功能需要Otii自动化工具箱许可证。Otii内置的TCP服务器,可用于从其他应用程序(例如在持续集成环境中运行的脚本)控制本应用程序。
用户可使用 TCP 服务器 API 编写自定义TCP客户端,或使用Qoitech Github上提供的客户端之一。
本指南将演示如何通过 C#版本 Otii TCP 客户端从 C#应用程序控制 Otii。
安装C#客户端
C# 版 Otii TCP 客户端可从 nuget.org 以 NuGet 包形式获取。
在 Visual Studio 中创建 C#/.NET Framework 项目,右键单击解决方案资源管理器中的“引用(References)”,选择“管理 NuGet 包...(Manage NuGet Packages....)”。
在NuGet包管理器(NuGet Package Manager)中,从nuget.org包源安装OtiiTcpClient。(若未列出nuget.org包源,请参阅此处解决方法)
启动TCP服务器
用户需要先启动Otii TCP服务器再连接。
有关 TCP 服务器入门指南,请参阅以下页面:
操作指南
请确保已启动 Otii 并运行 TCP 服务器,且 Arc 已连接至计算机(其主输出端未连接任何设备)。
首先添加控制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();
}
}
}运行程序,连接至计算机的Arc设备会自动开启电源,并在一秒后自动关闭。
项目处理
开始测量前,用户需要创建新项目(create a project)、连接已打开的项目(connect to an already opened project),或打开先前保存的项目(open a previously saved project)。
此处若存在已打开的项目,那么就连接至该项目,否则即创建新项目。
...
// Get a reference to the first device in the list
var arc = devices[0];
var project = otii.GetActiveProject();
if (project == null) {
project = otii.CreateProject();
}
...项目配置
测量前,用户需设置主输出电压、过流保护,并配置启用UART通信。
随后添加需录制的通道。本示例中启用主电流(mc)、主电压(mv)、UART日志(rx)及数字输入GPI1(i1)。
在Visual Studio中将鼠标悬停于EnableChannel Method上方,可查看可用通道列表。
...
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);
...执行定时记录
将电源开关 Turn on and off the power 部分的代码替换为下方录制 Record部分。同时需在主类中添加定义记录时长的常量。
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();
}用户可在Qoitech Github上找到基础测量示例。
