%% 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.
%
% 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 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::172.28.16.222::inst0::INSTR');
% 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
fopen(visaObj);
%% Get the screenshot of the instrument and display it in MATLAB
% Send command to clear the event registers in all register groups. Also clear the error queue.
fprintf(visaObj,'*CLS');
% 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');
fwrite(fid,screenPNG,'uint8');
fclose(fid);
%% 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?');
end
% Read the PNG and display image
figure;
imageMatrix = imread('screenCap.png','png');
image(imageMatrix);
% 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;