moonscpi

    Portable measurement plans and replay with a native SCPI TCP runner.

    Download zip
    Author
    Version
    0.2.0
    License
    MIT
    Last updated
    44 minutes ago
    Downloads
    2

    #MoonSCPI

    MoonSCPI 是用 MoonBit 编写的台架仪器采集工具。把 SCPI 查询、采样次数和允许范围写进 JSON 计划,就能在调试、实验教学和回归测试中复用同一套测量步骤。原生 TCP 采集可在同一次运行中归档计划、原始响应与 JSON/CSV 报告。离线回放使用同一套解析与判定逻辑。

    #构建和复现

    需要 MoonBit 工具链,基准版本为 moonc v1.0.0-rc1+3c0520448。原生构建在 Windows 使用 MSVC 或 clang-cl,在 Linux 使用 GCC 或 Clang。模拟联调脚本使用 Python 3 标准库。

    moonc -v moon update moon build --release --target native cmd/moonscpi python scripts/integration.py

    联调脚本启动本机 TCP 模拟仪器。运行成功后,_build/demo 包含三个通用场景的记录与报告,以及 keysight-e36300/ 下的一次完整模拟会话。

    示例计划运行结果
    示波器读取波形前导和二进制样本,按采样间隔、参考点及幅值系数还原 5 个时间/电压点
    数字万用表连续 3 次 READ?,得到 5.001、4.999、5.000 V,逐次对上下限判定
    直流电源读回 4.98 V 和 0.25 A。异常测试把电流改成 0.80 A,CSV 保留两项读数,进程返回 2
    Keysight E36300 通道读回按型号核验身份,查询指定通道的电压和电流,归档同一次运行的计划、读数、判定和原始响应

    #安装和使用

    通过 Mooncakes 安装固定版本可执行 moon install bzhangui/moonscpi/cmd/moonscpi@0.2.0。在源码目录也可执行 moon install ./cmd/moonscpi --bin ./_build/bin。将安装目录加入当前终端的 PATH 后执行下方命令。源码目录支持使用 moon run --target native cmd/moonscpi -- 后接相同参数。

    moonscpi check examples/scope.json moonscpi replay examples/scope.json _build/demo/scope.trace.json --csv moonscpi replay _build/demo/keysight-e36300/plan.json _build/demo/keysight-e36300/trace.json

    连接实机前,按仪器手册设置通道、触发、测量模式及波形传输格式。复制示例计划,将 expected_idn_prefix 改为实机返回的品牌和型号前缀,并调整查询、单位和上下限。以下命令中的 IP 和端口须替换为仪器的原始 SCPI TCP 端点。

    moonscpi run bench.json 192.0.2.10 5025 --timeout-ms 3000 --output-dir _build/bench-001 moonscpi capture bench.json 192.0.2.10 5025 > bench.trace.json moonscpi replay bench.json bench.trace.json

    通用计划支持 scalar(单个数值,可重复采样)和 waveform(Keysight InfiniiVision 风格十字段前导加定长二进制块),查询使用单条无参数命令。连接后首先发送 *IDN?。E36300 模板额外接受结构化的 channel,由 MoonBit 生成指定通道查询,详见 E36300 使用指南。

    文本支持 LF/CRLF,波形支持 8/16 位有符号或无符号、双字节大小端及三种结束符。默认对连接及每条请求分别设 3 秒超时。计划最多 64 步、256 次查询,响应上限为 16 KiB 文本或 1 MiB 二进制载荷,完整记录上限为 4 MiB,单份报告最多 20 万条数据点。

    run --output-dir 接受父目录已存在的新目录。它写入 plan.json、trace.json、report.json 和 report.csv,最后写出 session.json。会话文件记录版本、起止时间、已核验的仪器身份、计划查询数、已记录响应数与结果。越限和通信失败都可以归档,记录保留已经收到的完整响应。目录已有文件或目录已存在时,运行会在连接仪器前失败。验收时以完整的 session.json 判断归档是否写完。

    退出码 0 表示成功,1 表示计划、归档、连接、协议或解析错误,2 表示有数据越限。错误报告保留已经解析的读数。capture 判断采集是否成功,run 和 replay 还会执行测量值判定。回放检查查询顺序和帧类型,支持读取 v0.1.0 的原始记录,并用调整过的上下限重新判定。

    #测试与实现范围

    moon fmt --check moon check --target all --deny-warn moon test --target all --deny-warn moon build --target all --deny-warn python scripts/package_smoke.py python scripts/package_smoke.py --registry

    默认的安装测试从本地打包产物解压安装。--registry 使用新缓存从 Mooncakes 安装,并在独立 MoonBit 项目中复验公共 API 和命令行。

    根包实现协议成帧、计划校验、型号模板、回放与报告,覆盖 Wasm、Wasm GC、JavaScript 和 Native。native/ 实现 TCP,cmd/moonscpi/ 提供命令行和归档。当前仪器证据来自本机 TCP 仿真。支持范围为原始 SCPI TCP、常用数值响应、IEEE 488.2 定长块与文档中列出的设备模板。

    项目代码按 MIT 发布。协议和波形缩放为 MoonBit 独立实现,参考 Keysight 原始 LAN 连接、IEEE 488.2 响应示例 与 InfiniiVision 编程指南。原生 I/O 依赖 moonbitlang/async 0.22.10(Apache-2.0)。模拟仪器和测试数据随项目编写。

    ScpiError

    pub(all) suberror ScpiError {
    PlanError(String)
    FrameError(String)
    TraceError(String)
    MeasurementError(String)
    } derive(
    Debug
    )

    ScpiError::message

    fn ScpiError::message(self : ScpiError) -> String

    Decoder

    pub struct Decoder {
    // private fields
    }

    Decoder::feed

    fn Decoder::feed(self : Decoder, bytes : BytesView) -> Response? raise ScpiError

    Feed raw TCP bytes until exactly one response is complete.

    Decoder::new

    fn Decoder::new(mode : Mode) -> Decoder

    Decoder::received

    fn Decoder::received(self : Decoder) -> Int

    Decoder::wire

    fn Decoder::wire(self : Decoder) -> Bytes

    Encoding

    pub(all) enum Encoding {
    U8
    I8
    U16LE
    I16LE
    U16BE
    I16BE
    } derive(
    Debug
    )

    Exchange

    pub(all) struct Exchange {
    request : Request
    bytes : Bytes
    }

    Limits

    pub(all) struct Limits {
    lower : Double?
    upper : Double?
    } derive(
    Debug
    )

    Limits::contains

    fn Limits::contains(self : Limits, n : Double) -> Bool

    Mode

    pub(all) enum Mode {
    Text
    Block(Terminator)
    } derive(Eq,
    Debug
    )

    Mode::from_name

    fn Mode::from_name(name : String) -> Mode raise ScpiError

    Mode::name

    fn Mode::name(self : Mode) -> String

    Plan

    pub struct Plan {
    // private fields
    }

    Plan::evaluate

    fn Plan::evaluate(self : Plan, trace : Trace) -> Report raise ScpiError

    Evaluate the trace with no dependency on the instrument or the operating system.

    Plan::parse

    fn Plan::parse(text : StringView) -> Plan raise ScpiError

    Parse and validate the whole plan before attempting instrument I/O.

    Plan::requests

    fn Plan::requests(self : Plan) -> Array[Request]

    Plan::verify_identification

    fn Plan::verify_identification(self : Plan, response : Response) -> String raise ScpiError

    Profile

    type Profile

    Reading

    pub(all) struct Reading {
    step : String
    index : Int
    seconds : Double?
    value : Double
    unit : String
    passed : Bool
    }

    Report

    pub struct Report {
    plan : String
    identity : String?
    readings : Array[Reading]
    failure : String?
    }

    Report::status

    fn Report::status(self : Report) -> Int

    Report::to_csv

    fn Report::to_csv(self : Report) -> String

    Report::to_json

    Request

    pub(all) struct Request {
    query : String
    mode : Mode
    } derive(Eq,
    Debug
    )

    Response

    pub(all) enum Response {
    Line(String)
    Binary(Bytes)
    } derive(Eq,
    Debug
    )

    Step

    pub(all) enum Step {
    Scalar(name~ : String, query~ : String, samples~ : Int, unit~ : String, limits~ : Limits)
    Waveform(name~ : String, preamble~ : String, query~ : String, encoding~ : Encoding, terminator~ : Terminator, unit~ : String, limits~ : Limits)
    }

    Terminator

    pub(all) enum Terminator {
    NoTerminator
    LF
    CRLF
    } derive(Eq,
    Debug
    )

    Trace

    pub(all) struct Trace {
    exchanges : Array[Exchange]
    failure : String?
    }

    Trace::parse

    fn Trace::parse(text : StringView) -> Trace raise ScpiError

    Trace::to_json

    MAX_BLOCK_BYTES

    let MAX_BLOCK_BYTES : Int

    MAX_CAPTURE_BYTES

    let MAX_CAPTURE_BYTES : Int

    MAX_REPORT_POINTS

    let MAX_REPORT_POINTS : Int

    MAX_TEXT_BYTES

    let MAX_TEXT_BYTES : Int

    decode

    fn decode(mode : Mode, bytes : BytesView) -> Response raise ScpiError