Pattern acquisition saved to files

This Python example, pattern_acquisition_with_save.py, installs a simulated module and acquires 32 patterns waveforms in oscilloscope mode. The resulting pattern waveform is saved in internal format and VSA waveform format files. A screen capture is also saved.

Script

Copy

pattern-acq-with-save.py

# -*- coding: utf-8 -*-
""" Demonstrates pattern acquisition in oscilloscope mode. Saves a pattern
waveform file (internal format) and a screen capture as a result
of an acquisition limit test. Also separately saves a pattern
waveform file (csv format). """

import pyvisa as visa  # import VISA library

ADDRESS = 'TCPIP0::localhost::hislip0,4880::INSTR'
CHANNEL = '5A'
SCREENFILE = '%USER_DATA_DIR%\\Screen Images\\myscreenimage.png'
PATTERNFILE = '%USER_DATA_DIR%\\Waveforms\\pattern_waveform_internal'
PATTERNFILE_VSA = '%USER_DATA_DIR%\\Waveforms\\waveform_VSA_recording.csv'


def open_flexdca_connection(address):
    """ Opens visa connection to FlexFlexDCA. """
    print('Connecting to Flexdca ...')
    try:
        rm = visa.ResourceManager()
        connection = rm.open_resource(address)
        connection.timeout = 20000  # Set connection timeout to 20s
        connection.read_termination = '\n'
        connection.write_termination = '\n'
        inst_id = connection.query('*IDN?')
        print('\nFlexDCA connection established to:\n' + inst_id, flush=True)
    except (visa.VisaIOError, visa.InvalidSession):
        print('\nVISA ERROR: Cannot open instrument address.\n', flush=True)
        return None
    except Exception as other:
        print('\nVISA ERROR: Cannot connect to instrument:', other, flush=True)
        print('\n')
        return None
    return connection


def install_simulated_module(flexdca, channel, model, signal='NRZ'):
    """ Simplified installation of a simulated FlexDCA module.
    model
        Type of simulated module. "DEM" is a dual-electrical module.
        "QEM" is a quad-electrical module. "DOM" is a dual-optical
        module. "QEM" is a electrical-optical module.
    signal
        Format of signal. NRZ or PAM4.
    """
    slot = channel[0]
    flexdca.write(':EMODules:SLOT' + slot + ':SELection ' + model)
    if signal in 'NRZ':
        flexdca.write(':SOURce' + channel + ':FORMat NRZ')
    else:
        flexdca.write(':SOURce' + channel + ':FORMat PAM4')
    flexdca.write(':SOURce' + channel + ':DRATe 9.95328E+9')
    flexdca.write(':SOURce' + channel + ':WTYPe DATA')
    flexdca.write(':SOURce' + channel + ':PLENgth 127')
    flexdca.write(':SOURce' + channel + ':AMPLitude 90E-3')
    flexdca.write(':SOURce' + channel + ':NOISe:RN 3.0E-6')
    flexdca.write(':SOURce' + channel + ':JITTer:RJ 4.0E-12')
    flexdca.write(':CHANnel' + channel + ':DISPlay ON')


def all_channels_off(flexdca):
    """ Turns all available channels off. """
    for slot in '12345678':
        for letter in 'ABCD':
            channel = slot + letter
            flexdca.write(':CHANnel' + channel + ':DISPlay OFF')


def setup(flexdca, channel):
    flexdca.write('*CLS')
    flexdca.query(':SYSTem:DEFault;*OPC?')
    all_channels_off(flexdca)
    flexdca.write(':CHANnel'+channel+':DISPlay ON;')
    flexdca.query(':SYSTem:MODE OSCilloscope;*OPC?')
    flexdca.query(':TRIGger:PLOCk ON;*OPC?')
    flexdca.query(':SYSTem:AUToscale;*OPC?')
    flexdca.write(':ACQuire:EPATtern ON')  # Acquire entire pattern
    flexdca.write(':ACQuire:SPBit:MODe MANual')  # Select samples/bit
    flexdca.write(':ACQuire:SPBit 31.99212598')  # 32 samples/bit
    flexdca.write(':ACQuire:SMOothing AVERage')  # Average smoothing selected
    patternlength = flexdca.query(':TRIGger:PLENgth?')  # Get pattern length
    flexdca.write(':TIMebase:BRANge ' + patternlength)  # View pattern


def define_limit_test_files(flexdca, channel, screenfilename, patternfilename):
    """ Configures the name and type of files saved upon completion of
    the acquisition limit test.
    """
    # Specifies pattern waveform file (.wfmx internal format)
    flexdca.write(':LTESt:ACQuire:SWAVeform:CHANnel' +
                  channel + ':STATe DISK')
    flexdca.write(':LTESt:ACQuire:SWAVeform:CHANnel' + channel +
                  ':FTYPe WPINternal')
    flexdca.write(':LTESt:ACQuire:SWAVeform:CHANnel' + channel +
                  ':FNAMe "' + patternfilename + '"')
    # Specifies screen image file to save
    flexdca.write(':LTESt:ACQuire:SIMage:FNAMe:USTandard')
    flexdca.write(':LTESt:ACQuire:SIMage:FNAMe "' + screenfilename + '"')
    flexdca.write(':LTESt:ACQuire:SIMage:STATe ON')
    flexdca.write(':LTESt:ACQuire:SIMage:INVert OFF')
    flexdca.write(':LTESt:ACQuire:SIMage:WINDow TIMe1')


def run_acquisition_limit_test(flexdca, channel):
    """ Configures and runs acquisition limit test.
    32 patterns are acquired which results in a saved pattern waveform in
    internal format and a saved screen capture. For pattern smoothing,
    patterns acquired equal patterns smoothed.
    """
    patterncount = '32'
    flexdca.write(':LTESt:ACQuire:CTYPe:PATTerns ' + patterncount)
    if int(patterncount) > 1:
        flexdca.write(':ACQuire:ECOunt ' + patterncount)
    flexdca.write(':ACQuire:STOP')
    flexdca.write(':ACQuire:CDISplay')  # Clear acquired data
    flexdca.query(':LTESt:ACQuire:STATe ON;*OPC?')
    # Acquire waveforms
    flexdca.timeout = 60000  # 60 seconds
    print('Acquiring ' + patterncount + ' patterns. Times out after 60s.',
          flush=True)
    flexdca.query(':ACQuire:RUN;*OPC?')
    flexdca.write(':LTESt:ACQuire:STATe OFF')
    flexdca.timeout = 30000
    print('\nFile saved from limit test:\n',
          flexdca.query(':LTESt:ACQuire:SWAVeform:CHANnel' +
                        channel + ':FNAMe?'))
    print('\nFile saved from limit test:\n',
          flexdca.query(':LTESt:ACQuire:SIMage:FNAMe?'))


def save_pattern_waveform_file(flexdca, channel, patternVSAfilename):
    """ After the limit test has completed, this function
    saves a pattern waveform to a comma-separated-value text file.
    """
    flexdca.write(':DISK:WAVeform:SAVE:SOURce CHAN' + channel)
    flexdca.write(':DISK:WAVeform:SAVE:FTYPe VSACsv')
    flexdca.write(':DISK:WAVeform:FNAMe "' + patternVSAfilename + '"')
    flexdca.query(':DISK:WAVeform:SAVE;*OPC?')
    s = flexdca.query(':DISK:WAVeform:FNAMe?')
    print('\nFile saved:\n', s)


FlexDCA = open_flexdca_connection(ADDRESS)
install_simulated_module(FlexDCA, CHANNEL, 'QEM')
setup(FlexDCA, CHANNEL)
define_limit_test_files(FlexDCA, CHANNEL, SCREENFILE, PATTERNFILE)
run_acquisition_limit_test(FlexDCA, CHANNEL)
save_pattern_waveform_file(FlexDCA, CHANNEL, PATTERNFILE_VSA)
FlexDCA.write(':SYSTem:GTLocal')
FlexDCA.close()