# Tracking Markers

This Python example, tracking-markers.py, demonstrates using tracking markers. A simulated instrument and waveform is used instead of an oscilloscope. Python 3 supports unicode, so a Greek symbol is shown in output from a print statement.

## Example Script

markers-tracking.py

    #!python3

    """This program illustrates, in the Infiniium XR8
    oscilloscope, using tracking markers.
    """

    # Import modules.
    import pyvisa
    import sys
    from matplotlib.ticker import EngFormatter

    # Global variables.
    oscilloscope_visa_address = "TCPIP0::localhost::hislip0,4880::INSTR"
    timeout = 20000  # 20 seconds.
    source = "CHANnel1"
    eng_s = EngFormatter(unit="s", places=2, sep=" ")
    eng_V = EngFormatter(unit="V", places=2, sep=" ")

    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.

    # Default Setup.
    print("Default Setup.")
    Infiniium.write(":SYSTem:DEFault")

    # # Use auto-scale to automatically set up oscilloscope.
    # print("Autoscale.")
    # Infiniium.query(":SYSTem:AUToscale;*OPC?")

    # Make a Single acquisition.
    Infiniium.query(":ACQuire:SINGle;*OPC?")

    # Get times of interest using "time at" measurements.
    Infiniium.write(f":MEASure:HORizontal:TMAXimum:SOURce {source}")
    qresult = Infiniium.query(":MEASure:HORizontal:TMAXimum:SOURce?").strip()
    print(f'Measure horizontal "time at max" source: {qresult}')

    Infiniium.write(":MEASure:HORizontal:TMAXimum")
    time_at_max = float(Infiniium.query(":MEASure:HORizontal:TMAXimum?"))
    print(f'Measured "time at max" on {source}: {eng_s(time_at_max)}')

    Infiniium.write(f":MEASure:HORizontal:TMINimum:SOURce {source}")
    qresult = Infiniium.query(":MEASure:HORizontal:TMINimum:SOURce?").strip()
    print(f'Measure horizontal "time at min" source: {qresult}')

    Infiniium.write(":MEASure:HORizontal:TMINimum")
    time_at_min = float(Infiniium.query(":MEASure:HORizontal:TMINimum?"))
    print(f'Measured "time at min" on {source}: {eng_s(time_at_min)}')

    # Place tracking markers at those times.
    Infiniium.write(":MARKer:Y1A:STATe TRACk")
    Infiniium.write(f":MARKer:X1:SOURce {source}")
    Infiniium.write(f":MARKer:X1:POSition {time_at_max}")

    Infiniium.write(":MARKer:Y2A:STATe TRACk")
    Infiniium.write(f":MARKer:X2:SOURce {source}")
    Infiniium.write(f":MARKer:X2:POSition {time_at_min}")

    # Get vertical values from tracking markers.
    V_at_max = float(Infiniium.query(f":MEASure:MARKer:Y1A?"))
    print(f'Tracking marker voltage at "time at max" on {source}: {eng_V(V_at_max)}')
    V_at_min = float(Infiniium.query(f":MEASure:MARKer:Y2A?"))
    print(f'Tracking marker voltage at "time at min" on {source}: {eng_V(V_at_min)}')
    Infiniium.write(":MARKer:REFerence X2")
    delta_V = float(Infiniium.query(f":MEASure:MARKer:DY1A?"))
    print(f"Voltage delta from tracking markers on {source}: {eng_V(delta_V)}")

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

    # Exit program.
    exit_program()