泰克示波器控制scpi,程序读取波形数据并显示,py程序
"""
tektronix MDO3000 series oscilloscope test
"""
import numpy as np
import pyvisa
import sys
from struct import unpack #not needed for newest implementation
import matplotlib.pyplot as plt
channel_num = 4 #please select number of channels to read
v_list=[i for i in range(channel_num)]
time_list=[i for i in range(channel_num)]
try:
rm = pyvisa.ResourceManager()
# r_list = rm.list_resources()
# my_inst=rm.open_resource(r_list[0])
my_inst=rm.open_resource('TCPIP0::192.168.0.65::INSTR')
except Exception as e:
print("Could not create instrument instance; {0}".format(e))
sys.exit()
# 得到此通道的波形数据
def acq_one_wave(channel):
try:
my_inst.write(':DAT:SOU '+channel)
my_inst.write(':DAT:ENCdg RPB')
my_inst.write(':DAT:WIDTH 2')
my_inst.write(':HEADer 1')
print(my_inst.query('WFMO?'))
q1 = my_inst.query(':WFMPRE:YMULT?')
print(q1)
ymult = float( q1[10:])
yzero = float(my_inst.query(':WFMPRE:YZERO?')[10:])
yoff = float(my_inst.query(':WFMPRE:YOFF?')[10:])
xincr = float(my_inst.query(':WFMPRE:XINCR?')[10:])
# ADC_wave = my_inst.query_binary_values(':CURVE?', datatype='H', is_big_endian=True, container = np.array)
##读取波形数据
my_inst.write(':CURVE?')
osc_data=my_inst.read_raw()
print(osc_data)
# headerlen = 2 + int(osc_data[1])
headerlen = 13
print(osc_data[1])
header = osc_data[:headerlen]
ADC_wave = osc_data[headerlen:-1]
# 两个字节组成一个int16,为幅度值,高位在前,低位在后
ADC_wave = np.array(unpack('>%sH' % str(int(len(ADC_wave)/2)),ADC_wave))
Volts = (ADC_wave - yoff) * ymult + yzero
Time = np.arange(0, xincr * len(Volts), xincr)
return Time, Volts
except Exception as e:
print("Error reading waveform from"+channel)
print("Error: {0}".format(e))
my_inst.close()
sys.exit()
if __name__ == "__main__":
try:
print(my_inst.query('*IDN?'))
my_inst.write(':ACQ:STOPAfter SEQ')
# 读取不同通道的波形数据
for i in range(channel_num):
channel_num = 'CH' + str(i + 1)
Time, Volts = acq_one_wave(channel_num)
# normalization
# temp_min = np.min(Volts)
# temp_max=np.max(Volts)
# Volts=(Volts-temp_min)/(temp_max-temp_min)
plt.plot(Time, Volts)
time_list[i] = Time
v_list[i] = Volts
plt.show()
except Exception as e:
print("Error retrieving data: {0}".format(e))
# my_inst.close()
sys.exit()
程序运行如图