Transfer waveform data to/from N1000A
This script demonstrates FlexDCA's :WAVeform:XML:READ?
and :WAVeform:XML:WRITe?
queries. The script first configures FlexDCA so that a single-valued waveform is displayed. Then, the internal format waveform data is:
- Returned from FlexDCA's Channel 1A.
- Written into FlexDCA's waveform memory 2.
- Saved to a waveform file (.wfmx) in the script folder.
Example Script
Copy
WAVeform-XML-READ-WRITE.py
""" Example of FlexDCA's ":WAVeform:XML:READ?" and ":WAVeform:XML:WRITe?"
queries. FlexDCA offline is configured with a waveform.
Waveform data is returned from FlexDCA's channel 1A,
written into FlexDCA's waveform memory 2, and saved
to a waveform file without the definite-length block data
preamble).
"""
import pyvisa as visa # import VISA library
ADDRESS = 'TCPIP0::localhost::hislip0,4880::INSTR'
IOTIMEOUT = 20000 # 20 seconds
CHANNEL = '1A'
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 = IOTIMEOUT # 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 waveform. 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 setup(flexdca, channel):
flexdca.write('*CLS')
flexdca.query(':SYSTem:DEFault;*OPC?')
install_simulated_module(flexdca, channel, 'DEM')
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
patternlength = flexdca.query(':TRIGger:PLENgth?') # Get pattern length
flexdca.write(':TIMebase:UIRANge ' + patternlength) # View pattern
flexdca.write(':ACQuire:STOP')
def get_binary_waveform_data(flexdca):
""" Sends a scpi query to FlexDCA that returns SCPI binary data
instead of an ASCII string. For example, ':WAVeform:XML:READ? CHAN2A'
returns binary data.
datatype
Set to unsigned bytes for internal format PyVisa's default is
float. You can change datatype to whatever you need that is
supported by PyVisa.
is_big_endian
FlexDCA's byte order is little-endian by default.
ieee
IEEE definite-length block header is automatically stripped
from returned data.
"""
global CHANNEL
flexdca.read_termination = ''
flexdca.write_termination = ''
message = ':WAVeform:XML:READ? CHAN' + CHANNEL
data = flexdca.query_binary_values(message, datatype='B', is_big_endian=False, container=bytes, delay=None, header_fmt='ieee')
flexdca.read_termination = '\n'
flexdca.write_termination = '\n'
return data
def write_data_to_waveform_memory(flexdca, values):
data = b'' # Python 3 byte string (bytes not unicode)
flexdca.read_termination = ''
flexdca.write_termination = ''
message = ':WAVeform:XML:WRITe? WMEMory2,'
flexdca.write_binary_values(message, values, datatype='B', is_big_endian=False, termination=None, encoding=None)
# to satisfy ':WAVeform:XML:WRITe? WMEMory2,' as a query
data = flexdca.read_raw()
flexdca.read_termination = '\n'
flexdca.write_termination = '\n'
return
FlexDCA = open_flexdca_connection(ADDRESS)
setup(FlexDCA, CHANNEL)
data = b'' # Python 3 byte string (bytes not unicode)
data = get_binary_waveform_data(FlexDCA)
write_data_to_waveform_memory(FlexDCA, data)
with open('scope_waveform.wfmx', 'wb') as fout:
fout.write(data)
FlexDCA.write('DISPlay:WINDow:TIME1:DMODe STILed') # display channel and CGGS waveforms
FlexDCA.write(':SYSTem:GTLocal')
FlexDCA.close()