在信号发生器编程软件中设置异常中断是确保测试稳定性和设备安全的关键环节。异常中断机制能够在检测到硬件故障、参数越界或通信错误时立即停止测试,避免设备损坏或数据错误。以下是分步骤的详细设置方法,结合代码示例和最佳实践:
FREQ:SET 1GHz
后1秒未收到确认)
大多数信号发生器通过SCPI命令或编程接口提供硬件保护功能。
示例(Keysight E8257D):
python
import
pyvisa
rm = pyvisa.ResourceManager()
device = rm.open_resource('TCPIP0::192.168.1.10::inst0::INSTR')
# 启用过流保护中断
device.write('OUTP:PROT:CURR:STAT ON')# 开启电流保护
device.write('OUTP:PROT:CURR:THR 1.0')# 设置电流阈值为1A
# 启用过热保护中断
device.write('SYST:ERR:HAND:ENAB 1')# 启用系统错误处理
device.write('SYST:ERR:HAND:TYPE FAUL')# 故障时触发中断
R&S SMU200A示例:
pythondevice.write('SOUR:PROT:STAT ON') # 开启输出保护device.write('SOUR:PROT:LEV 25') # 设置保护电平为25dBm
在编程时添加参数合法性验证,避免发送非法指令。
Python示例:
pythondef set_frequency(device, freq_hz):max_freq = 26.5e9 # 设备最大频率(示例值)min_freq = 1e3 # 设备最小频率if not (min_freq <= freq_hz <= max_freq):raise ValueError(f"频率 {freq_hz/1e9:.3f}GHz 超出范围 [{min_freq/1e9:.3f}, {max_freq/1e9:.3f}]GHz")device.write(f'FREQ:CW {freq_hz}')
调用示例:
pythontry:set_frequency(device, 30e9) # 尝试设置30GHz(会触发异常)except ValueError as e:print(f"参数错误: {e}")device.write('ABORT') # 发送中断指令停止输出
设置指令响应超时时间,超时后自动触发中断。
PyVISA超时设置:
python
device.timeout =
2000
# 设置超时为2秒(单位:毫秒)
def
send_command_with_timeout(device, cmd):
try:
response = device.query(cmd)
return
response
except
pyvisa.errors.VisaIOError
as
e:
if
"timeout"
in
str(e):
print("通信超时,触发中断")
device.write('*RST')# 复位设备
raise
调用示例:
pythontry:send_command_with_timeout(device, 'MEAS:POWER?')except Exception as e:print(f"通信失败: {e}")
通过查询设备错误队列实现错误中断。
SCPI错误查询示例:
python
def
check_device_errors(device):
errors = []
while
True:
error = device.query('SYST:ERR?')
code, msg = error.split(',')
code =
int(code.strip())
if
code ==
0:# 无错误
break
errors.append((code, msg.strip('"').strip()))
return
errors
def
safe_operation(device, operation):
try:
operation()
errors = check_device_errors(device)
if
errors:
raise
RuntimeError(f"设备错误:
{errors}")
except
Exception
as
e:
print(f"操作中断:
{e}")
device.write('OUTP OFF')# 关闭输出
调用示例:
python
def
test_operation():
device.write('FREQ:CW 10e6')
device.write('OUTP:AMPL 10')
safe_operation(device, test_operation)
部分高端信号发生器支持外部触发中断(如TTL电平触发)。
R&S SMU200A示例:
python# 配置外部触发中断(当EXT_TRIG输入为高电平时停止)device.write('TRIG:SOUR EXT') # 外部触发源device.write('TRIG:SLOP POS') # 上升沿触发device.write('OUTP:TRIG:ACT STOP') # 触发时停止输出
实现错误日志记录和自定义回调函数。
Python示例:
python
error_log = []
def
log_and_interrupt(error):
error_log.append(error)
print(f"致命错误:
{error}, 执行中断...")
device.write('*RST')# 复位设备
def
set_amplitude(device, amp_dbm):
max_amp =
20
if
amp_dbm > max_amp:
log_and_interrupt(f"幅度
{amp_dbm}dBm 超过最大值
{max_amp}dBm")
else:
device.write(f'SOUR:POW
{amp_dbm}')
python
import
pyvisa
import
time
class
SignalGenerator:
def
__init__(self, address):
self.rm = pyvisa.ResourceManager()
self.device =
self.rm.open_resource(address)
self.device.timeout =
2000
# 2秒超时
self.max_freq =
26.5e9
self.max_amp =
20
self.error_log = []
def
set_frequency(self, freq_hz):
if
not
(1e3
<= freq_hz <=
self.max_freq):
self._log_error(f"频率
{freq_hz/1e9:.3f}GHz 越界")
raise
ValueError("频率越界")
self.device.write(f'FREQ:CW
{freq_hz}')
def
set_amplitude(self, amp_dbm):
if
amp_dbm >
self.max_amp:
self._log_error(f"幅度
{amp_dbm}dBm 越界")
raise
ValueError("幅度越界")
self.device.write(f'SOUR:POW
{amp_dbm}')
def
_log_error(self, msg):
self.error_log.append((time.time(), msg))
print(f"错误:
{msg}")
def
check_errors(self):
errors = []
while
True:
resp =
self.device.query('SYST:ERR?')
code, msg = resp.split(',')
code =
int(code.strip())
if
code ==
0:
break
errors.append((code, msg.strip('"').strip()))
if
errors:
self._log_error(f"设备错误:
{errors}")
raise
RuntimeError("设备错误")
def
safe_operation(self, operation):
try:
operation()
self.check_errors()
except
Exception
as
e:
print(f"中断测试:
{e}")
self.device.write('OUTP OFF')
self.device.write('*RST')
# 使用示例
sg = SignalGenerator('TCPIP0::192.168.1.10::inst0::INSTR')
def
test_case():
sg.set_frequency(10e6)
sg.set_amplitude(15)
sg.device.write('OUTP ON')
try:
sg.safe_operation(test_case)
except
Exception
as
e:
print(f"测试终止:
{e}")
通过上述方法,可实现信号发生器编程软件的可靠异常中断,确保测试过程的安全性和可重复性。