Transfer binary data from PC to DCA-X

This program demonstrates using the :DISK:FILE:WRITe? query to transfer binary data from a file on the PC to a DCA-X. The binary file is a screen image that is in the script's folder. Any png file will work as long as it is named test_image.png. The program transfers the file's data and not the file. The data is placed in a new file on the CA-X named test_image.jpg.

Example Script

Copy

DISK_FILE_WRITe.py

""" Example of FlexDCA's ":DISK:FILE:WRITE?" query.
Transfer a binary file to an DCA-X from the PC. The binary file is a screen
image that is in the scripts folder. Any png file will work as long
as it is named "test_image.png". The data is placed in a new file on the
DCA-X named "test_image.png". """


import pyvisa as visa  # import VISA library

ADDRESS = 'TCPIP0::localhost::hislip0,4880::INSTR'

def open_flexdca_connection(address):
    """ Opens visa connection to FlexDCA. """
    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


FlexDCA = open_flexdca_connection(ADDRESS)
print('Sending a binary file to DCA-X.', flush=True)
data = b''  # Python 3 byte string (bytes not unicode)
fin = open('test_image.png', 'rb')
data = fin.read()
fin.close()
filename = '"%USER_DATA_DIR%\\Screen Images\\test_image.png"'
message = ':DISK:FILE:WRITe? ' + filename + ','
FlexDCA.write_binary_values(message, data, datatype='B')
print('File is on DCA-X: ', filename)
FlexDCA.write(':SYSTem:GTLocal')
FlexDCA.close()