# Image Saves with Autonumbers

## Example Script

disk-screen-image-capture-autonumber.py

    #!python3

    """This program illustrates saving screen images to the
    Infiniium XR8 oscilloscope disk with autonumbered file
    names, first using custom file names, then using standard
    file names.
    """

    # Import modules.
    import pyvisa
    import sys

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

    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."""

        errors_found = False
        while True:
            # Keep reading errors until "No error".
            error_string = Infiniium.query(":SYSTem:ERRor:NEXT? DIAGnostic")
            if error_string:  # If there is an error string value.

                if error_string.find("0,", 0, 2) == -1:  # Not "No error".
                    errors_found = True
                    print(f"ERROR: {error_string}")

                else:  # "No error"
                    break

            else:  # :SYSTem:ERRor:NEXT? DIAGnostic should always return string.
                errors_found = True
                print("ERROR: :SYSTem:ERRor:NEXT? DIAGnostic returned nothing.")
                break

        if errors_found:
            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.

    # Save image files using custom names. Autonumber starting at 100.
    print("Four custom filenames with autonumbers:")
    filename = f"{scope_base_directory}DUT_1"
    Infiniium.write(f':DISK:SIMage:FNAMe "{filename}.png"')
    for i in range(1, 5):
        Infiniium.query(":DISK:SIMage:SAVE;*OPC?")
        print(f'Saved: {Infiniium.query(":DISK:SIMage:FNAMe?").strip()}')
        Infiniium.write(":DISK:SIMage:FNAMe:AUPDate")

    # Save image files using standard names and autonumber.
    print("Four standard filenames with autonumbers:")
    Infiniium.write(":DISK:SIMage:FNAMe:USTandard")
    for i in range(1, 5):
        Infiniium.query(":DISK:SIMage:SAVE;*OPC?")
        print(f'Saved: {Infiniium.query(":DISK:SIMage:FNAMe?").strip()}')
        Infiniium.write(":DISK:SIMage:FNAMe:AUPDate")

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

    # Exit program.
    exit_program()