概述
通信层是 Quatm 框架的通信骨干。所有进程间通信(设备驱动、分析脚本、GUI 面板之间的数据交互)都通过 ZeroMQ 的 发布/订阅(PUB/SUB) 模式进行,命令与控制则通过 RPC 实现。这确保了:
- 故障隔离:某个相机驱动崩溃不会导致整个实验中断
- 并行处理:图像处理与实验时序可同时运行
- 网络透明:各组件可以运行在不同的机器上
实验脚本
│
▼
[with realtime():] ──→ FPGA ──→ DAC/TTL 输出
│
▼
分析脚本 ◄── 图像/数据流 (ZMQ PUB/SUB)
│
▼
结果发布 ──→ DataManager ──→ HDF5 存储
客户端类型
quatm.servers 提供了四种标准客户端,用于处理不同类型的进程间通信:
| 客户端 | 用途 | 适用场景 |
|---|---|---|
CommandClient | 控制驱动和进程 | 向设备发送指令、启停驱动 |
DataClient | 传输小数据 | 1D 曲线、标量值、元数据 |
ImageClient | 传输大数据块 | 相机图像(2D 数组) |
MessageClient | 日志消息传递 | 错误、警告、信息、调试消息 |
基础客户端 — GenericClient
所有客户端的基类,封装了 ZMQ 连接和数据收发逻辑。
from quatm.servers.clients import GenericClient
client = GenericClient("my_channel")
client.subscribe("data_stream_name")
# 发送数据
client.send({"temperature": 25.0, "timestamp": 12345})
# 接收数据
if client.has_new_data():
data = client.recv()
| 方法 | 说明 |
|---|---|
subscribe(name) | 订阅指定的数据流 |
unsubscribe(name) | 取消订阅 |
send(data) | 发送数据 |
recv() | 接收数据 |
has_new_data() | 检查是否有新数据到达 |
数据客户端 — DataClient
专为小数据集设计的客户端,数据以 JSON 格式序列化,支持 NumPy 数组附件。
from quatm.servers.clients import DataClient
data_client = DataClient("analysis_result")
data_client.send({
"fit_params": {"A0": 1.5, "sigma": 0.3, "pos": 10.2},
"fit_curve": numpy_array
})
图像客户端 — ImageClient
优化用于传输相机图像等大型 2D 数据块。
from quatm.servers.clients import ImageClient
img_client = ImageClient("camera_output")
img_client.send(image_array) # 发送 NumPy 图像数组
命令客户端 — CommandClient
用于向驱动程序发送控制命令,使用独立通道以确保命令不被数据流阻塞。
消息客户端 — MessageClient
标准化的消息传递接口,所有消息带有时间戳和来源信息。
from quatm.servers import send_error, send_warning, send_info, send_debug
send_info("实验开始执行")
send_warning("激光功率偏低,请检查")
send_error("相机连接失败")
分布式属性系统
Quatm 通过分布式属性树管理所有运行时配置参数,支持跨进程实时同步。
配置层级
| 层级 | 说明 | 示例 |
|---|---|---|
| Configuration | 深层、基础系统属性 | 运行哪些硬件、可用驱动列表 |
| Properties | 对象特定参数 | ROI 位置、校准值、拟合参数 |
| Preferences | 不影响数据的次要选择 | 鼠标指针形状、窗口位置 |
PropertyAttribute
将分布式属性映射为 Python 属性,读写操作自动同步到属性树。
from quatm.servers.properties import PropertyAttribute, Properties
class MyComponent:
# 声明分布式属性,默认值为 42.0
_my_param = PropertyAttribute('/MyComponent/param', 42.0)
def __init__(self):
self._props = Properties('MyComponent')
def do_something(self):
# 读取属性值
x = self._my_param.value
# 写入属性值,自动同步到分布式树
self._my_param.value = 99.0
Properties
管理组件内部属性的同步副本,通过后台守护线程使用 ZMQ PUB/SUB 连接到中央属性中心。
from quatm.servers.properties import Properties
props = Properties('MyProcess')
props.set('/path/to/property', value)
current_value = props.get('/path/to/property')
配置读取器
读取静态配置文件 configuration/configfile.json,提供系统级别的运行时配置。
from quatm.servers.configreader import ConfigReader
reader = ConfigReader()
config = reader.getConfiguration()
常用路径常量
quatm.servers 导出以下路径常量,方便定位工作目录:
| 常量 | 说明 |
|---|---|
workpath | 工作根目录 |
driverpath | 驱动文件目录 |
iconpath | 图标资源目录 |
datapath | 数据存储目录 |
experiment_path | 实验脚本目录 |
configpath | 配置文件目录 |