# Read Error Queue Using the Count

This Python example, error-count.py, demonstrates how to read Infiniium's errors. This script sends 2 incorrect commands to Infiniium and then reads and print the resulting errors to Python's standard output console. One command does not exist and the other command is valid but is set to a value beyond its maximum.

## Script

error-count.py

    #!python3

    """This program illustrates error count checking in the
    Infiniium XR8 oscilloscopes.
    """

    # Import modules.
    import pyvisa
    import sys

    # Global variables.
    oscilloscope_visa_address = "TCPIP0::localhost::hislip0,4880::INSTR"
    timeout = 20000  # 20 seconds.

    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_count(when):
        """Check error count."""

        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 incorrect commands.
    Infiniium.write(":QUACK:LIKEA DUCK")  # Bad command.
    Infiniium.write(":TIMebase:POSition 2.4E10")  # Out of range error.

    # Check error count.
    check_error_count("End of program")

    # Exit program.
    exit_program()