# Oscilloscope Self Tests

This Python example performs the oscilloscope hardware self tests. Self tests are entirely safe to run but should only be needed if the oscilloscope has a possible error and Keysight support requests a self test. You can also run these self tests from Infiniium's GUI. Click Tools > Hardware Self Tests.... The script runs these pass/fail tests using the `*TST?` common command.

oscilloscope-self-test.py

    #!python3

    """This program illustrates performing the oscilloscope hardware
    self-tests on an Infiniium XR8 oscilloscope.
    """

    # Import modules.
    import pyvisa
    import sys
    import time

    # Global variables.
    oscilloscope_visa_address = "TCPIP0::lab-zir-p1z-2.cos.is.keysight.com::inst0::INSTR"
    timeout = 180000  # 3 minutes.

    def exit_program():
        """Exit program."""

        # Close connection to oscilloscope.
        sys.stdout.write("Closing oscilloscope connection.\n")
        Infiniium.write(":SYSTem:GTLocal")  # Unlock GUI.
        Infiniium.clear()  # Clear oscilloscope communications interface.
        Infiniium.close()  # Close communications interface to oscilloscope.
        rm.close()  # Close resource manager.
        print("End of program.")
        sys.exit()

    def check_error_queue(when):
        """Check error queue."""

        num_errors = int(Infiniium.query(":SYSTem:ERRor:COUNt?"))
        if num_errors:
            for _ in range(num_errors):
                error_string = Infiniium.query(":SYSTem:ERRor:NEXT? DIAGnostic").strip()
                print(f"ERROR: {error_string}")

            print(f"Exited because error(s) found when: '{when}'")
            exit_program()

    # ==========================================================
    # Main program:
    # ==========================================================

    # Connect and initialize oscilloscope.
    rm = pyvisa.ResourceManager("C:\\Windows\\System32\\visa64.dll")
    try:
        Infiniium = rm.open_resource(oscilloscope_visa_address)
    except Exception:
        print(
            f"Unable to connect to oscilloscope at {oscilloscope_visa_address}. Aborting program."
        )
        sys.exit()

    Infiniium.timeout = timeout  # Set global timeout.
    Infiniium.clear()  # Clear the instrument bus.
    # Lock GUI happens automatically.

    # Send the *TST? query.
    print("Running hardware self-tests. Please wait...", flush=True)
    start_time = time.time()
    tst_result = Infiniium.query("*TST?").strip()
    print(f"Hardware self-test took {str(time.time() - start_time)} seconds.")
    if tst_result == "0":
        print("Hardware self-tests passed.")
    elif tst_result == "1":
        print("Hardware self-tests failed.")
        syst_test_result = Infiniium.query(":SYSTem:TEST?").strip()
        print(syst_test_result)
    else:
        print(f"Unexpected *TST? result: {tst_result}")

    # Check error queue.
    check_error_queue("End of program")

    # Exit program.
    exit_program()