Skip to main content

简体中文 | English

🚀 PyInsts

基于 PyVISA 的仪器自动化控制封装库 (支持 Windows, macOS, Linux)

PyPI Version Supported Python Versions License


pyinsts 是一个轻量级、健壮的 Python 仪器控制封装库。它基于 PyVISA,针对射频和电子测试实验室中常用的频谱分析仪、信号源、网络分析仪、电源、万用表等仪器进行了高层 SCPI 指令封装,同时支持底层的串口与硬件接口通信。

✨ 核心特性与架构

  • 🏗 模块化设计:
    • instrument_drivers/: 存放 SCPI 仪器驱动及基类 baseinstrument.py,驱动类可直接从 pyinsts.instrument_drivers 统一导出使用。
    • drivers/: 存放底层硬件通信协议封装(如 serial_interface.py, ft2232h 等)。
    • instrument/sim/: 存放无实体硬件测试所需的仿真配置文件。
  • 🔄 自动连接重试:内置连接重试机制,自动规避短暂的硬件握手超时。
  • ⏳ 高效同步阻塞:提供 wait_opc() 机制,相比硬编码 time.sleep 更安全,避免截图或扫频时的竞态问题。
  • 🗂 灵活配置加载:支持通过外部 config.yaml 或 config.json 自动解析和分配仪器的物理地址(如 VISA 资源地址)。
  • 📝 开箱即用日志:内置彩色日志流(依赖 loguru),方便调试 SCPI 指令交互,并自动拦截 PyVISA 底层日志。

📦 安装方法

确保你的系统已配置 VISA 后台驱动(如 NI-VISA 或 Keysight IO Libraries Suite)。

pip install pyinsts

⚙️ 仪器地址配置文件示例

本库支持通过配置文件集中管理仪器的 VISA 地址。默认寻找 config.yaml 或 config.json。

在你的项目目录下创建 config.yaml:

instruments_address:
  N9020B: "USB0::0x2A8D::0x1D0B::MY55480186::INSTR"  # Keysight 频谱仪地址
  E5052B: "GPIB0::17::INSTR"                         # Keysight 信号源分析仪地址
  FSWP-26: "TCPIP0::192.168.1.100::inst0::INSTR"     # R&S 信号分析仪地址

🚀 快速开始 (Quick Start)

下面是控制 Keysight N9020B 频谱仪并设置参数的示例:

import logging
from pyinsts.data import setup_logging
from pyinsts.instrument_drivers import FSV3030Sp

# 1. 初始化日志输出(支持控制台彩色显示与 Loguru 记录)
setup_logging(log_level=logging.INFO)

# 2. 建立仪器连接(支持连接实体硬件,也可追加 ;@sim 使用内置仿真测试)
sim_address = "USB::0x0AAD::0x0119::100001::INSTR;@sim"

try:
    with FSV3030Sp(address=sim_address, model="FSV3030") as spec:
        print(f"成功连接仪器,IDN: {spec.idn}")

        # 1. 设置中心频率为 1.0 GHz
        spec.set_freq_center(1.0, "GHz")

        # 2. 设置频跨 (Span) 为 10 MHz
        spec.set_freq_span(10.0, "MHz")

        # 3. 设置 RBW 为自动模式
        spec.set_rbw_auto()

        # 4. 触发 Peak Search 峰值查找
        spec.set_peak_search()

        # 5. 等待命令处理完成
        spec.wait_opc()
        print("仪器控制流程成功执行完毕!")

except Exception as e:
    print(f"仪器操作过程中发生错误: {e}")

📟 目前支持的仪器型号矩阵

支持的驱动类均支持从 pyinsts.instrument_drivers 直接导入:

厂商 (Manufacturer) 仪器类型 (Type) 驱动类名 (Class) 验证型号 (Tested Models)
Keysight / Agilent 信号/频谱分析仪 KeysightN9020B N9020B
Keysight / Agilent 信号/频谱分析仪 KeysightN9030A, KeysightN9030B N9030A, N9030B
Keysight / Agilent 信号源分析仪 KeysightE5052B E5052B
Keysight / Agilent 模拟信号发生器 KeysightE8257D E8257D
Keysight / Agilent 矢量网络分析仪 KeysightN5245B N5245B
Keysight / Agilent 直流电源 KeysightE36312A E36312A
Keysight / Agilent 射频功率计 KeysightN1914A N1914A
Keysight / Agilent 数字万用表 DM34461A 34461A
Rohde & Schwarz 频谱/信号分析仪 FSV3030Sp FSV3030
Rohde & Schwarz 信号/相噪分析仪 FswpSp (频谱模式), FswpPN (相噪模式) FSWP-26 / FSWP
Rohde & Schwarz 矢量网络分析仪 Zna43 ZNA43
Rohde & Schwarz 实时频谱分析仪 Rsa6000Sp RSA6000
同惠 (Tonghui) 矢量网络分析仪 Sna6034a SNA6034A
同惠 (Tonghui) 数字万用表 Th1963 TH1963
其他设备 (Others) 源表 / 测量单元 P2401 P2401
其他设备 (Others) 温控箱单元 Ts760, Ts760Set TS760

🧪 运行测试

项目内置基于 PyVISA @sim 仿真后端的单元测试,无需实体仪器即可验证连接与 SCPI 交互:

pip install -e ".[dev]"
pytest

🏷 更新日志与贡献

📄 开源协议

本项目采用 MIT License 开源协议。

Metadata

Release files for pyinsts 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyinsts 0.1.1
File Size Uploaded
pyinsts-0.1.1.tar.gz 60.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyinsts 0.1.1
File Interpreter ABI Platform
pyinsts-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 142.9 kB

Release files / pyinsts-0.1.1.tar.gz

Download URL pyinsts-0.1.1.tar.gz
Size 60.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ea27645240248abb1a52cdcf556b0b3e273cf4b25ba0311ef45779dfbc665e13
BLAKE2b-256 checksum
How to use checksums
08a069cf1f155bf3cb0b021738c4ae454285f08d4b536a0c19264fa265cd6a1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / pyinsts-0.1.1-py3-none-any.whl

Download URL pyinsts-0.1.1-py3-none-any.whl
Size 82.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
350d41499ba96205b728ee09c23f29219fc44ee4ef146fb0e8418a6b1fb0268b
BLAKE2b-256 checksum
How to use checksums
c8f6065bdd941d2b0b316bc53fa547b29b48dfa05713b181e4d8ad53cc3ef478
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page