MATLAB - Binary Block File Transfer Via VISA

This is a MATLAB example that enables you to control the FieldFox family of Combination Analyzers via a VISA resource string.

- The program first clears the error queue and all status registers via the "*CLS" command.

- The *IDN? identification query is then asserted and the resultant string is read.

- The application stores a PNG file,'Test_Image.PNG',to the internal memory of the targeted FieldFox (FF) analyzer.

- Next the stored PNG image file,'Test_Image.PNG', is transferred from the FieldFox to the controlling PC via the MMEM:DATA? query. This transfer is by default always an IEEE-754 binary bin-block transfer.

- The .PNG file save on the controlling PC is stored as 'C:\Temp\TransferedTestImage.png'

- Lastly, the system error queue is checked at conclusion of the application. If no errors were generated the response to the "SYST:ERR?" then the query will still read "+0, "No Error"".

  

Note:  In the following example:

"%","{%*"  indicates a comment

"*}" indicates the end of a comment

 

%{

Sample MATLAB program for the Keysight Technologies FieldFox (FF) handheld combination analyzers.

The sample program connects to a FF Family handheld combination analyzer thru a VISA resource string.

The program first clears the error queue and all status registers via the

"*CLS" command. The *IDN? identification query is then asserted and the resultant string is read.

The application stores a PNG file,'Test_Image.PNG',to the internal memory of the targeted

FieldFox (FF) analyzer.

Next the stored PNG image file,'Test_Image.PNG', is transferred from the FF to the controlling PC via

the MMEM:DATA? query. This transfer is by default always an IEEE-754 binary bin-block transfer.

The .PNG file save on the controlling PC is stored as 'C:\Temp\TransferedTestImage.png'

As a wrap up the system error queue is checked at conclusion of the

application. If no errors were generated the response to the "SYST:ERR?"

query will still be "+0, "No Error"".

%}

%Remove all interfaces to instrument

instrreset

% find all previously created objects

oldobjs = instrfind;

% If there are any existing objects

if (~isempty(oldobjs))

    % close the connection to the instrument

    fclose(oldobjs);

    % and free up the object resources

    delete(oldobjs);

end

 

% Remove the object list from the workspace.

clear oldobjs;

%{

Define FieldFox (FF) interface, this is the VISA resource string. Replace this VISA

resource string with your controlling PC's FieldFox VISA resource string as appropriate.

For this applicaiton the 'agilent' I/o libraries are utilized.

%}

fieldFox = visa('agilent', 'TCPIP0::156.140.159.126::inst0::INSTR');

% Buffer size must precede open command

set(fieldFox,'InputBufferSize', 640000);

set(fieldFox,'OutputBufferSize', 640000);

% Open session to fieldFox based on VISA resource string

fopen(fieldFox);

% Clear the event status registers and all errors which may be in the FieldFox's queue.

fprintf(fieldFox, '*CLS');

% Check to ensure the error queue is clear. Response is "+0, No Error"

fprintf(fieldFox, 'SYST:ERR?');

[errIdentifyStart,~] = fscanf(fieldFox, '%c');

['Initial error check results: ', errIdentifyStart]

% Query instrument identification string

fprintf(fieldFox, '*IDN?');

[idn,~] = fscanf(fieldFox, '%c');

['Instrument identified as: ', idn]

% Set the FF mass storage to the internal drive

fprintf(fieldFox, 'MMEM:CDIR "[INTERNAL]:"');

% Binary efforts here

% First store an image to the local FF memory.

fprintf(fieldFox, 'MMEM:STOR:IMAG "TestImage.png"');

% Query image via MMEM:DATA? 'yourFileNameHere.mimeExtensionType'

fprintf(fieldFox, 'MMEM:DATA? "TestImage.png"');

 

% Dump return bits to a variable 'screenPNG' via a MATLAB binblockread call.

% MATLAB binblockread supports five 8-bit bin block read types:

%   uchar, schar, int8, unit8, char.

% Of these uint8, uchar, char % all functioned without corrupting the binary bits,

% i.e., the resultant file transfer preserved the data integrity of the original

% file without corruption.

screenPNG = binblockread(fieldFox,'uint8'); fread(fieldFox,1);

% Write bits to file as PNG file save

% From MATLAB help (in command window 'help fid' to view details)

% 'FID = fopen(FILENAME) opens the file FILENAME for read access'.

% FILENAME is the name of the file to be opened. Thus, in this case open

% C:\Temp\TransferedTestImage.png

% The 'w' indicates 'open file for writing; discard existing contents'

fid = fopen('C:\Temp\TransferedTestImage.png','w');

fwrite(fid,screenPNG,'uint8');

fclose(fid);

% As a last step query the fieldFox error queue and ensure no errors have

% occurred since initiation and completion of the program

fprintf(fieldFox, 'SYST:ERR?');

[errIdentifyStop,~] = fscanf(fieldFox, '%c');

['Final error check results: ', errIdentifyStop]

% Close session connection

fclose(fieldFox);

delete(fieldFox);

clear fieldFox;

%Import the saved image into MATLAB workspace

importedImage = imread('C:\Temp\TransferedTestImage.png')

image(importedImage)

['Initial error check results: ', errIdentifyStart]

['Instrument identified as: ', idn]

['Final error check results: ', errIdentifyStop]