用 SCPI 读取历史记录数据(循环次数、时间、电压)
自动生成 “循环次数 - 时间” 关系图
我会提供: ✅ SCPI 指令集 ✅ 数据解析规则 ✅ 可直接运行的 Python 绘图代码 ✅ 最终输出:循环次数 vs 时间 曲线图
一、核心 SCPI 命令(获取 “循环次数 + 时间” 数据)
你需要从 DLOG 序列历史记录文件 中读取:
绝对时间戳
采样时间
实测电压
计算出:每条数据属于第几轮循环
1. 必须执行的 SCPI 指令
scpi
*IDN? ! 确认仪器
DLOG:FILE:CAT? ! 列出所有历史日志
DLOG:FILE:INFO?"日志名.csv" ! 获取创建/结束时间
DLOG:FILE:TIMESTAMP? ! 必须 =1(带时间戳才能算循环)
DLOG:FILE:DATA?"日志名.csv" ! 读取全部历史数据(时间+电压+电流)
SOUR:LIST:STEP? ! 序列步数
SOUR:LIST:DWEL? ! 每步时间(计算单轮总时长)
TRIG:COUN? ! 总循环次数
二、DLOG 数据返回格式(必须会解析)
开启时间戳后返回格式固定:
plaintext
时间戳, 电压(V), 电流(A), 功率(W)
示例:
plaintext
2026-06-10 14:30:00.123.12.000.2.500.30.000
2026-06-10 14:30:00.223.12.005.2.501.30.037
计算循环次数公式(关键)
先算 单轮总时长
plaintext
单轮时长 = 所有步骤 DWEL 时间相加
每条数据相对于起始时间的偏移
plaintext
时间偏移 = 当前时间戳 - 第一条时间戳
循环次数 = int (时间偏移 / 单轮时长) + 1
这就是 循环次数 ↔ 时间 的数学关系。
三、可直接运行:Python SCPI 读取 + 绘制 “循环次数 - 时间” 图
这是完整自动化代码,直接复制运行即可出图。
依赖安装
bash
运行
pip install pyvisa pyvisa-py matplotlib pandas
完整代码
python
运行
import visa
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime
# ======================
# 1. 连接仪器
# ======================
rm = visa.ResourceManager()
inst = rm.open_resource("TCPIP0::192.168.1.10::inst0::INSTR") # 按你的地址修改
inst.timeout = 10000
print("仪器识别:", inst.query("*IDN?").strip())
# ======================
# 2. SCPI 获取关键参数
# ======================
filename = inst.query("DLOG:FILE:CAT?").split(",")[0].strip() # 取第一个日志
print("使用日志文件:", filename)
# 读取序列配置(计算单轮时长)
steps = int(inst.query("SOUR:LIST:STEP?"))
dwel = list(map(float, inst.query("SOUR:LIST:DWEL?").split(",")))
t_round = sum(dwel)
total_loops = int(inst.query("TRIG:COUN?"))
print(f"单轮总时长: {t_round:.2f}s")
print(f"预设总循环次数: {total_loops}")
# ======================
# 3. 读取历史数据
# ======================
data = inst.query(f'DLOG:FILE:DATA?"{filename}"')
lines = data.strip().split("n")
# ======================
# 4. 解析时间戳 + 计算循环次数
# ======================
loops = []
times = []
t0 = None
for line in lines:
if not line:
continue
parts = line.split(",")
ts = parts[0]
volt = parts[1]
# 时间转秒偏移
current_t = datetime.strptime(ts, "%Y-%m-%d %H:%M:%S.%f")
if t0 is None:
t0 = current_t
dt = (current_t - t0).total_seconds()
loop_num = int(dt / t_round) + 1
loops.append(loop_num)
times.append(dt)
# ======================
# 5. 绘制 循环次数 - 时间 图
# ======================
plt.figure(figsize=(12. 5))
plt.plot(times, loops, linewidth=2. color="#2E86AB")
plt.title("循环次数 - 时间关系图", fontsize=14)
plt.xlabel("时间 (s)", fontsize=12)
plt.ylabel("循环次数", fontsize=12)
plt.grid(True)
plt.show()
四、图表展示效果(你将得到)
运行后自动弹出一张专业图表:
X 轴:时间(秒)
Y 轴:循环次数
曲线呈阶梯上升,每一步代表完成一轮循环
图表能清晰看到: ✅ 每一轮花了多少时间 ✅ 总执行时间 ✅ 循环是否均匀稳定 ✅ 有无异常延迟
五、如果你只想用 SCPI 得到可绘图的原始数据
执行这 3 条命令即可导出可直接导入 Excel 绘图的数据:
scpi
DLOG:FILE:TIMESTAMP 1
DLOG:FILE:DATA?"你的日志.csv"
SOUR:LIST:DWEL?
把返回数据粘贴到 Excel → 插入 散点图 / 折线图 → X = 时间,Y = 循环次数。