泰克示波器控制scpi,程序读取波形数据并显示

该程序通过Python的pyvisa库连接到泰克示波器,获取并显示多个通道的波形数据。它首先打开资源,设置数据源和编码方式,然后读取每个通道的波形数据,进行转换并归一化,最后用matplotlib绘制波形。程序适用于进行信号分析和调试。
摘要由CSDN通过智能技术生成
泰克示波器控制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()

程序运行如图 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值