Getting and Putting Data with ASCII

This Python Program does the following:

See Also

Python Basics

 

import pyvisa as visa
import numpy as np

# Change this variable to the address of your instrument
VISA_ADDRESS = 'TCPIP0::localhost::inst0::INSTR'

# Create a connection (session) to the instrument
resourceManager = visa.ResourceManager()
session = resourceManager.open_resource(VISA_ADDRESS)


# Command to preset the instrument and deletes the default trace, measurement, and window
session.write("SYST:FPR")

# Create and turn on window 1
session.write("DISP:WIND1:STAT ON")

# ======================== Getting data ========================

# Select the measurement
session.write("CALC1:MEAS1:DEF 'S21'")
# Take a sweep
session.write("SENS1:SWE:MODE SING")
# Keep the controller and the VNA "synched"
session.query("*OPC?")
# Default Data Format is ASCII

session.write("FORM:DATA ASCII,0")

# Ask for the data from the sweep, pick one of the locations to read

myMeas = session.query_ascii_values("CALC1:MEAS1:DATA:FDATA?", container=np.array)       # Formatted Meas

# myMeas = session.query_ascii_values("CALC:MEAS1:DATA:SDATA?", container=np.array)     # Corrected, Complex Meas
# session.write("CALC:MEAS:MATH:MEM")                                                   # Stores a trace into memory, required if querying with FMEM or SMEM
# myMeas = session.query_ascii_values("CALC:MEAS1:DATA:FMEM?", container=np.array)      # Formatted Memory
# myMeas = session.query_ascii_values("CALC:MEAS1:DATA:SMEM?", container=np.array)      # Corrected, Complex Memory
# myMeas = session.query_ascii_values("SENS1:CORR:CSET:ETER:DATA? 'Directivity(1,1)'", container=np.array)     # Error-Term Directivity


print(myMeas)
# Measurements are stored as an array, allowing quick and simple Python commands
maxValue = np.max(myMeas)

print(f"Max Value: {maxValue}")


# # ======================== Putting data ========================


# # Preset the instrument and deletes existing traces, measurements, and windows
# # Creates a S11 measurement named "CH1_S11_1"

session.write("SYST:PRES")

# Put data back in
session.write_ascii_values("CALC1:MEAS1:DATA:FDATA ",myMeas)         # Formatted Meas
# session.write_ascii_values("CALC:MEAS1:DATA:FMEM ",myMeas)      # Formatted Memory

# session.write_ascii_values("CALC:MEAS1:DATA:SDATA ",myMeas)     # Corrected, Complex Meas
# session.write_ascii_values("CALC:MEAS1:DATA:SMEM ",myMeas)      # Corrected, Complex Memory
# session.write_ascii_values("SENS1:CORR:CSET:ETER:DATA 'Directivity(1,1)',",myMeas)     # Error-Term Directivity'