Python 脚本编写
以下功能需要自动化工具箱许可证。Otii 的内置 TCP 服务器,可用于从其他应用程序(例如在持续集成环境中运行的脚本)控制本Otii应用程序。 用户可以使用TCP 服务器 API 编写自己的 TCP 客户端, 或者使用Qoitech Github上提供的客户端。
本指南将演示用户可通过Python应用程序,使用 Python版本Otii TCP 客户端 来控制 Otii。
有关如何开始使用 TCP 服务器 的更多信息,请查看以下页面:
安装Python客户端
使用pip安装Python客户端:
python3 -m pip install otii_tcp_client运行TCP 服务器
用户要么通过Otii桌面客户端或者在Otii命令行界面,使Otii TCP服务器处在运行状态。
本示例要求服务器处于登录状态,且已预定Otii自动化工具箱软件许可(Automation Toolbox )。用户可在以下页面阅读更多登录并预定软件许可的信息:
自动化测量
创建Python脚本,如otii_measurement.py。首先需导入Otii Python客户端:
#!/usr/bin/env python3
import time
from otii_tcp_client import otii_client下一步建立与Otii TCP服务器的连接。此处假设TCP服务器运行于同一台计算机且使用默认TCP端口。
client = otii_client.OtiiClient()
otii = client.connect()连接成功后,需验证仅有一台设备连接至Otii,并获取该设备句柄。同时确保设备已添加至当前项目。
devices = otii.get_devices()
if len(devices) != 1:
raise Exception(f'Expected to find exactly 1 device, found {len(devices)} devices')
device = devices[0]
device.add_to_project()本脚本需对连接的Arc设备的主通道进行10秒钟的录制。每次运行都添加录制到当前项目文件中,以便后续进行对比分析。
project = otii.get_active_project()下一步是为项目配置Otii Arc/Ace设备。本示例中的设备通常由纽扣电池供电,因此我们将主电压设置为3.0。我们需要使用高精度测量模式(低量程),并测量主电流(mc)和主电压(mv)。
device.set_main_voltage(3.0)
device.set_max_current(0.5)
device.enable_channel("mc", True)
device.enable_channel("mv", True)现在可以开始录制,打开电源开始录制后等待10秒,随后断电并停止录制。
project.start_recording()
device.set_main(True)
time.sleep(10.0)
device.set_main(False)
project.stop_recording()录制结束,用户可得到最后一条录制的统计信息。用户也可以录制多次,使用以下代码得到最后一条录制的数据信息。
# Get statistics for the recording
recording = project.get_last_recording()
info = recording.get_channel_info(device.id, 'mc')
statistics = recording.get_channel_statistics(device.id, 'mc', info['from'], info['to'])
# Print the statistics
print(f'From: {info["from"]} s')
print(f'To: {info["to"]} s')
print(f'Offset: {info["offset"]} s')
print(f'Sample rate: {info["sample_rate"]}')
print(f'Min: {statistics["min"]:.5} A')
print(f'Max: {statistics["max"]:.5} A')
print(f'Average: {statistics["average"]:.5} A')
print(f'Energy: {statistics["energy"] / 3600:.5} Wh')随后断开与Otii TCP 服务器的连接,
otii.disconnect()至此,一个简短的自动化记录脚本完成。 更多示例请访问 Qoitech Github。
