Code covered by the BSD License  

Highlights from
Capture a Screenshot of an Agilent X-Series Signal Analyzer and Transfer it to MATLAB(R)

image thumbnail

Capture a Screenshot of an Agilent X-Series Signal Analyzer and Transfer it to MATLAB(R)



24 Apr 2013 (Updated )

MATLAB(R) example to save a screenshot of an Agilent X-Series Signal Analyzer using VISA interface.

%% Example to connect to and grab a screenshot of an Agilent X-Series Signal Analyzer
% This example connects to an X-Series Agilent Signal Analyzer using VISA and
% sends SCPI commands to capture a screenshot of the signal analyzer screen and
% displays it in MATLAB(R).
% Note that this demo requires you to have Agilent IO Libraries installed.
% The VISA resource string to the signal analyzer is to be obtained from
% Agilent connection expert.

% Copyright 2013-2014 The MathWorks, Inc.

%% Interface configuration and instrument connection

% The second argument to the VISA function is the resource string for your
% instrument
visaObj = visa('agilent','TCPIP0::');

% Set the timeout value
visaObj.Timeout = 10;
% Set the Byte order
visaObj.ByteOrder = 'littleEndian';
% Set the buffer size to a large value sinze the BMP could be large
visaObj.InputBufferSize = 10000000;
% Open the connection

%% Get the screenshot of the instrument and display it in MATLAB(R)

% Send command to clear the event registers in all register groups. Also clear the error queue.

% Send command to choose the theme to be used when saving the screen image
fprintf(visaObj, 'MMEM:STOR:SCR:THEM TDC');

% Store the current screen image in the file "screenCap.png" to the default
% directory (D:\User_My_Documents\Instrument\My Documents\SA\screen\)
fprintf(visaObj, 'MMEM:STOR:SCR "screenCap.png"');

% Transfer the image to MATLAB
fprintf(visaObj, 'MMEM:DATA? "D:\User_My_Documents\Instrument\My Documents\SA\screen\screenCap.png"');
screenPNG = binblockread(visaObj,'uint8'); fread(visaObj,1);

% save as a PNG  file
fid = fopen('screenCap.png','w');

%% Display errors, if any, by reading back from the instrument error queue
instrumentError = query(visaObj,':SYSTEM:ERR?');
while ~isequal(instrumentError,['+0,"No error"' char(10)])
    disp(['Instrument Error: ' instrumentError]);
    instrumentError = query(visaObj,':SYSTEM:ERR?');

% Read the PNG and display image
imageMatrix = imread('screenCap.png','png');
% Adjust the figure so it shows accurately
sizeImg = size(imageMatrix);
set(gca,'Position',[0 0 1 1],'XTick' ,[],'YTick',[]); set(gcf,'Position',[50 50 sizeImg(2) sizeImg(1)]);
axis off; axis image;

% Delete objects and clear them.
delete(visaObj); clear visaObj;

Contact us