Create a FOM Measurement with Error Handling

The following Python example demonstrates how to create a FOM measurement with the following attributes:

See Also

Python Basics

 

import pyvisa as visa
import sys

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

# Custom Exception class for SCPI Errors
class SCPIError(Exception):
    def __init__(self, ex, *args):
        super().__init__(args)
       
 self.ex = ex
    # Display the Error Messag
    def __str__(self):
        return f'SCPI errors found in the system: {self.ex}'

# Helper function to check for errors
def catchErr():
    numErrors = session.query(
"SYST:ERR:COUN?")
   
 if int(numErrors) != 0:
       
 raise SCPIError(session.query("SYST:ERR?"))

try:
   
 # 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")
    catchErr()


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


    # Create a measurement with parameter
    session.write("CALC1:MEAS1:DEF 'S21'")
    catchErr()


    # Displays measurement 1 in window 1 and assigns the next available trace number to the measurement
    session.write("DISP:MEAS1:FEED 1")
    catchErr()


    # Set the start and stop frequencies
    session.write("SENS1:FREQ:START 1e9")
    catchErr()


    session.write("SENS1:FREQ:STOP 2e9")
    catchErr()


    # Set the receivers to be 2e9 -> 3e9
    # See SENS:FOM:RNUM? to find the range number for a specific name
    session.write("SENS1:FOM:RANG3:FREQ:OFFS 1e9")

    catchErr()    

    # Turns frequency offset on and enable the freq offset settings
    session.write("SENS1:FOM ON")
    catchErr()


except visa.Error as ex:

    # Failed to communicate with the instrument
    print('Couldn\'t connect to \'%s\', exiting now...' % VISA_ADDRESS)
    sys.exit()


except SCPIError as e:
   
 # SCPI Errors in the system
    print(e)
    sys.exit()


except Exception as e:
   
 # Catches generic errors
    print(f"Error: {e}")

finally:
   
 # No errors found
    print("Done")