Transfer CGGS data to/from oscilloscope

This script demonstrates Infiniium's :WAVeform:CGRade:XML:READ? and :WAVeform:CGRade:XML:WRITe? queries. The script first configures Infiniium so that a color-grade gray-scale waveform is displayed. Then, the internal format waveform data is:

  1. Returned from Infiniium's ECH2_8.
  2. Written into Infiniium's color-grade gray-scale memory 2.
  3. Saved to a color-grade gray-scale file (.cgsi) in the script folder. .

Example Script

waveform-cgrade-xml-read-write.py

#!python3

"""This program illustrates, in the Infiniium XR8 oscilloscope,
the use of the ":WAVeform:CGRade:XML:READ?" and
":WAVeform:CGRade:XML:WRITe?" queries. Waveform data is
returned from a real-time eye source, written into a
color-grade gray-scale memory, and saved to a waveform file.
"""

# Import modules.
import pyvisa
import sys
import time
import platformdirs

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

# Save Locations
base_file_name = "my_cggs"
local_base_directory = f"{platformdirs.user_documents_dir()}\\"


def get_cggs_binary_waveform_data(cggs_source):
    """Get color-grade gray scale data from a mulit-valued
    waveform source, for example, a real-time eye or other
    waveform with color-grade or gray-scale persistence.
    """

    message = f":WAVeform:CGRade:XML:READ? {cggs_source}"
    data_bytes = Infiniium.query_binary_values(message, datatype="B", container=bytes)
    return data_bytes


def write_cggs_data_to_cggs_memory(data_bytes, cggs_memory):
    """Write color-grade/gray scale data to a CGGS memory."""

    message = f":WAVeform:CGRade:XML:WRITe? {cggs_memory},"
    Infiniium.write_binary_values(message, data_bytes, datatype="B")
    data = Infiniium.read_raw()


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?")

# Stop acquisitions and clear the display.
Infiniium.write(":ACQuire:STOP")
Infiniium.write(":ACQuire:CDISplay")  # Clear display.

# Turn on CHANnel1 real-time eye.
source = "CHANnel1"
Infiniium.query(f":{source}:EYE:DISPlay ON;*OPC?")

# Make 100 acquisitions.
Infiniium.write(":LTESt:ACQuire:CTYPe:WAVeforms 100")
Infiniium.write(":LTESt:ACQuire:STATe ON")
Infiniium.query(":ACQuire:RUN;*OPC?")  # *OPC? required when limit testing.
Infiniium.write(":LTESt:ACQuire:STATe OFF")

# Get color-grade gray scale data from a real-time eye.
eye_source = "ECHannel1"
cggs_data = get_cggs_binary_waveform_data(eye_source)
print(f"Color grade/gray scale data read from: {eye_source}")

# Write color-grade/gray scale data to a memory.
cggs_memory = "CGMemory1"
write_cggs_data_to_cggs_memory(cggs_data, cggs_memory)
print(f"{cggs_memory} loaded from: {eye_source}")

# Write color-grade/gray scale data to a file.
cggs_data_file = f"{local_base_directory}eye_data.cgsi"
with open(cggs_data_file, "wb") as fout:
    fout.write(cggs_data)
print(f"Color grade/gray scale data written to: {cggs_data_file}")

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

# Exit program.
exit_program()