Transfer binary data from DCA-X to PC

This example script demonstrates using the :DISK:FILE:READ? query to transfer binary data from a file on a DCA-X (FlexDCA) to the script's folder. The program transfers the file's data and not the file. The program uses the same filename for the saved file on the PC, but any filename could have been used. To confirm that the data has been correctly transferred, simply double-click on the resulting file to view it. This example script uses the PyVisa method query_binary_values(), to return the binary data from the captured screen image file. This method automatically removes the binary data's IEEE header. Leaving this header in the data would result in an invalid graphics file.

Example Script

Copy

DISK_FILE_READ.py

""" Example of FlexDCA's ":DISK:FILE:READ?" command.
Transfer a binary file from an DCA-X or N1010A FlexDCA to file on PC.
This program starts by saving an DCA-X screen image (a binary file).
Next, the screen image file is transferred to the script's folder.
"""

import pyvisa as visa  # import VISA library

ADDRESS = 'TCPIP0::localhost::hislip0,4880::INSTR'
IOTIMEOUT = 30000
# name of image file on DCA-X
screenimage = '%USER_DATA_DIR%\\Screen Images\\test_image.jpg'
fileout = 'test_image.jpg'  # destination file on PC


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
        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 save_screen_image(FlexDCA, screenimage):
    """ Commands 86100D to save a screen image for us to use. The file
    is saved in the default \Screen Images folder.
    """
    FlexDCA.write(':DISK:SIMage:FNAMe "' + screenimage + '"')
    filepath = FlexDCA.query(':DISK:SIMage:FNAMe?')
    print('\nSaving ' + filepath + ' screen image on DCA-X.', flush=True)
    FlexDCA.query(':DISK:SIMage:SAVE;*OPC?')
    return


def get_binary_file_data(FlexDCA, screenimage):
    """ Returns binary data from saved image file """
    print('\nReturning ' + screenimage + ' data from DCA-X.', flush=True)
    FlexDCA.timeout = IOTIMEOUT
    data = b''
    message = ':DISK:FILE:READ? "' + screenimage + '"'
    data = FlexDCA.query_binary_values(message,
                                       datatype='B',
                                       header_fmt='ieee',
                                       container=bytes)
    return data


def save_binary_data_to_file(FlexDCA, fileout, data):
    """ Places output file in same folder as script. """
    with open(fileout, 'wb') as fout:
        fout.write(data)
    print('\nReturned data saved on PC: ', fileout, flush=True)


FlexDCA = open_flexdca_connection(ADDRESS)
save_screen_image(FlexDCA, screenimage)
data = b''
data = get_binary_file_data(FlexDCA, screenimage)
save_binary_data_to_file(FlexDCA, fileout, data)
FlexDCA.write(':SYSTem:GTLocal')
FlexDCA.close()

For more information, read the information on the :DISK:FILE:READ? query