说明
本文介绍了 Keysight 34465A 的基本使用和 SCPI 指令设置,演示了使用 Python 的 PyVISA 库控制两台 34465A 同时采集数据的完整流程,包括设置采样参数、触发测量、读取数据、使用 OpenPyXL 保存到 Excel 并生成折线图。整个过程采用模块化代码并加入详细注释,每一步都以教程形式说明,并增加了异常处理逻辑来提高健壮性。
使用的库
openpyxl
- https://openpyxl.readthedocs.io/
- OpenPyXL 用于生成 Excel 文件和绘制图表。
pyvisa
- https://pyvisa.readthedocs.io/en/latest/
- 在 Python 环境中,通过
pip install pyvisa pyvisa-py openpyxl
安装依赖库。PyVISA 用于与万用表通讯,PyVISA-Py(或 NI-VISA)提供 VISA 后端。
代码说明
- 生成Excel文件每次采集后直接保存;
- 使用Excel折线图展示数据;
- 可设定采集参数(采集点数、采集时长、NPLC);
- 代码包含异常处理(比如设备未连接、超时等);
效果展示
参考
- https://pyvisa.readthedocs.io/en/latest/
- https://docs.keysight.com/
- https://www.keysight.com/
- https://openpyxl.readthedocs.io/
- https://blog.csdn.net/Jzy140222010108/article/details/125111682
- https://blog.csdn.net/palzhj/article/details/129339224
- https://bloghome.com.cn/post/ru-he-li-yong-pyvisaku-yu-gpibjie-kou-de-ce-liang-she-bei-jin-xing-tong-xin-shi-xian-shu-ju-de-cai-ji-he-fa-song-kong-zhi-ming-ling-csdnwen-ku.html
代码
import pyvisa
from time import sleep
from openpyxl import Workbook
from openpyxl.chart import LineChart, Reference
from datetime import datetime
import logging
# 查看详细通信过程
#logging.basicConfig(level=logging.DEBUG)
# 获取当前时间并格式化
current_time = datetime.now().strftime("%Y%m%d-%H%M%S")
# 配置日志格式和文件
logging.basicConfig(
level=logging.DEBUG, # 设置日志级别
format='%(asctime)s [%(levelname)s] %(message)s', # 含时间戳和日志级别
handlers=[
logging.FileHandler(f'DMMRecorder_{
current_time}.log', encoding='utf-8'), # 输出到文件
logging.StreamHandler() # 同时输出到控制台
]
)
# 示例参数:采样 1000 点,总时间 1秒
sample_points = 1000 # 采样点数
total_time = 1.0