AnsweredAssumed Answered

Matlab less than 2010 binblockwrite issues 33522A

Question asked by lhornburg Employee on Apr 13, 2012
Some of you may have noticed that the binblockwrite command is buggy and often doesn't work in matlab revisions less than 2010.  This is especially frustrating if your trying to download a binary waveform to an Agilent function generator like the 33522A.  Below is a matlab program that shows you how to create and send an binary arb without needing to use the binblockwrite command:

% clears all variables, closes all open files

clear all; close all; clc;

%opens and creates a visa session for communication with function generator

fgen = visa('AGILENT','TCPIP0::');
fgen.Timeout = 10;
set (fgen,'OutputBufferSize',2000000000);


%Query Idendity string and report
fprintf (fgen, '*IDN?');
idn = fscanf (fgen);
fprintf (idn)
fprintf ('\n\n')

%Clear and reset instrument
fprintf (fgen, '*RST');
fprintf (fgen, '*CLS');

%Clear volatile memory
fprintf(fgen, 'SOURce1:DATA:VOLatile:CLEar');
fprintf(fgen, 'FORM:BORD SWAP');  %configure the box to correctly accept the binary arb points

%create 800Kpoint arb named z
numDataPoints = 800000;
z = zeros(1, numDataPoints, 'single');
i = single(1:1:numDataPoints);      
z = (i-1)/numDataPoints;  %creates a simple rising waveform 800Kpoints long

bytes=num2str(length(z) * 4); %# of bytes
header= ['SOURce1:DATA:ARBitrary testrise, #' num2str(length(bytes)) bytes]; %create header
binblockBytes = typecast(z, 'uint8');  %convert datapoints to binary before sending
fwrite(fgen, [header binblockBytes], 'uint8'); %combine header and datapoints then send to instrument

fprintf(fgen, '*WAI');   %Make sure no other commands are exectued until arb is done downloading
fprintf('Download Complete\n\n')

%Set desired configuration for channel 1
fprintf(fgen,'SOURce1:FUNCtion:ARBitrary testrise'); % set current arb waveform to defined arb testrise
fprintf(fgen,'SOURCE1:VOLT 2'); % set max waveform amplitude to 2 Vpp
fprintf(fgen,'SOURCE1:VOLT:OFFSET 0'); % set offset to 0 V
fprintf(fgen,'OUTPUT1:LOAD 50'); % set output load to 50 ohms
fprintf(fgen,'SOURCE1:FUNCtion:ARB:SRATe 80000000'); % set sample rate
fprintf(fgen,'TRIGGER1:SOURCE EXT'); %set trigger source for channel 1 to external
fprintf(fgen,'SOURCE1:BURST:STATE ON');                 % !!! must be BURST (not CW), unclear Manual !!!
fprintf(fgen,'SOURCE1:BURST:NCYCLES 1');                % 1 / INF
fprintf(fgen,'SOURCE1:BURST:MODE TRIG');                % Burst mode to TRIG or GATed
fprintf(fgen,'SOURce1:FUNCtion ARB'); % turn on arb function

%Enable Output for channel 1
fprintf(fgen,'OUTPUT1 ON');

%set desired configuration for channel 2
fprintf(fgen,'SOURCE2:FUNCTION:SHAPE SQUARE');      %  %%% No ":" before SQUare!
fprintf(fgen,'SOURCE2:FREQ 100');
fprintf(fgen,'SOURCE2:VOLTAGE:AMPLITUDE 5.0');      
fprintf(fgen,'SOURCE2:VOLTAGE:OFFSET 2.5');
fprintf(fgen,'OUTPUT2:LOAD 50'); % set output load to 50 ohms
fprintf(fgen,'OUTPut:SYNC:SOURce CH2');

%Enable output for channel 2
fprintf(fgen,'OUTPUT2 ON');

% Read Error
fprintf(fgen, 'SYST:ERR?');
errorstr = fscanf (fgen);

% error checking
if strncmp (errorstr, '+0,"No error"',13)
   errorcheck = 'Arbitrary waveform generated without any error';
   fprintf (errorcheck)
   errorcheck = ['Error reported: ', errorstr];
   fprintf (errorcheck)

%Save Arb to USB stick, titled test arb

% fprintf(fgen, 'MMEM:STOR:DATA "USB:\RAMP100_40M.arb"');

%closes the visa session with the function generator