% Date: 6/19/14 % Tested on a 64-bit Windows 7 computer % using Ni-VISA 5.4 and Matlab 2013a % with the Matlab Instrument Control Toolbox % Tested on DPO5204B, MSO72004, and DPO7104C % Program: FastFrame Summary Frame Transfer % This program acquires 10 instances of a pulse train that contains a % periodic runt using FastFrame and transfers the summary frame to the computer. %% Configure VISA resource visa_brand = 'ni'; %set VISA brand visa_address = 'TCPIP::tsc-dpo7104c::INSTR'; %send instrument identifier from VISA buffer = 50000; %set buffer length instrument = visa(visa_brand, visa_address, 'InputBuffer', buffer,'OutputBuffer', buffer); %configure instrument as a VISA resource fopen(instrument); %open the instrument as a VISA resource instrument.timeout = 15; %set instrument timeout %% Identify and reset instrument instID = query(instrument,'*idn?'); %identify instrument fprintf('%s\n',instID); %print instrument ID fwrite(instrument,'*rst'); %default the instrument %% Configure horizontal, vertical, and trigger settings %variables for individual settings horizscale = '200e-9'; %sec/div samplerate = '2.5e9'; %S/sec numberofframes = 10; voltsperdiv = .5; position = -3; highthresh = 2; lowthresh = 0.75; fwrite(instrument,'acquire:state 0'); %turn off the acquisition system fwrite(instrument,'horizontal:mode auto'); %set horizontal settings to auto fprintf(instrument,'horizontal:mode:scale %s',horizscale) %set horiztonal scale fprintf(instrument,'horizontal:mode:samplerate %s',samplerate); %set sample rate fwrite(instrument,'acquire:mode sample'); %set acquire mode to sample fwrite(instrument,'horizontal:fastframe:state 1'); %turn on FastFrame fprintf(instrument,'horizontal:fastframe:count %i', numberofframes); %specify number of frames fprintf(instrument,'ch1:scale %f',voltsperdiv); %set vertical scale fprintf(instrument,'ch1:position %f',position); %set vertical position fwrite(instrument,'trigger:a:type pulse'); %set trigger type to pulse fwrite(instrument,'trigger:a:pulse:class runt'); %set pulse class to runt fwrite(instrument,'trigger:a:pulse:runt:qualify occurs') %set trigger upon occurrence of a runt fwrite(instrument,'trigger:a:pulse:runt:polarity:ch1 positive'); %set runt trigger polarity fprintf(instrument,'trigger:a:pulse:runt:threshold:high %f',highthresh);%set the high and low runt thresholds fprintf(instrument,'trigger:a:pulse:runt:threshold:low %f',lowthresh); fprintf('Horizontal, vertical, and trigger settings configured.\n'); %% Configure data transfer settings fwrite(instrument,'header 0'); %turn the header off fwrite(instrument,'horizontal:fastframe:sumframe average'); %tell the scope to create a summary frame that is the average of all frames fwrite(instrument,'data:encdg fastest'); %set encoding type to fast binary fwrite(instrument,'data:source ch1') %set data source fwrite(instrument,'data:stop 100000'); %set end of record fwrite(instrument,'wfmoutpre:byt_n 2'); %set number of bytes per data point fwrite(instrument,'data:framestart 11'); %as long as start/stop frames are greater than the total number of frames, fwrite(instrument,'data:framestop 12'); %the program will only capture the last frame, which is the summary frame, which is what we want fprintf('Data transfer settings configured.\n'); %% Acquire Waveform fprintf('Acquiring waveform.\n'); instrument.Timeout = 60; %increase timeout in order to capture all frames fwrite(instrument,'acquire:stopafter sequence'); %set scope to single acquisition mode fwrite(instrument,'acquire:state 1'); %start acquisition query(instrument,'*opc?'); %wait until scope has finished acquiring waveforms fprintf('Waveform acquired.\n'); instrument.Timeout = 15; %return instrument timeout to original value %% Retrieve vertical and horizontal scaling information %because the returned values are strings, we need to convert to a 16-bit %number in order to use them to scale, thus the str2double() %Vertical %yoffset is unscaled offset data that is set by the ch:offset yoffset = str2double(query(instrument,'wfmoutpre:yoff?')); %ymult is the scaling factor that is set by ch:scale ymult = str2double(query(instrument,'wfmoutpre:ymult?')); %yzero is scaled position data that is set by ch:position yzero = str2double(query(instrument,'wfmoutpre:yzero?')); %Horizontal %number of points in the waveform acquisition numberofpoints = str2double(query(instrument,'wfmoutpre:nr_pt?;')); %amount of time between data points xincrement = str2double(query(instrument,'wfmoutpre:xincr?;')); %absolute time value of the beginning of the waveform record xzero = str2double(query(instrument,'wfmoutpre:xzero?;')); fwrite(instrument,'curve?'); %send curve? query fprintf('Curve query sent.\n'); %% Transfer waveform data fread(instrument,1); %read and discard # character numchars = char(fread(instrument,1)); %read single character that gives the number of chars in header bytes = char(fread(instrument,str2double(numchars))); %use 'numchars' to read/discard the correct amount of characters in header %Since we specified that the number of bytes in each data point is 2, %we'll use a 16-bit (or 2-byte) unsigned integer to represent the waveform %data accurately in Matlab [data, readsize, msg] = fread(instrument, numberofpoints, 'int16'); fprintf('Curve query received.\n'); %% Using the scaling information, rescale the binary data %Subtract the vertical offset (modified by the vertical offset) from the binary data, multiply the resulting %data by the ymultiplier, and then add the yzero (modified by the vertical position) %order of operations is important because yoffset is raw, unscaled binary %data and yzero has been scaled scaleddata = (data-yoffset).*ymult+yzero; %Create a time vector starting at xzero, ending at the end of the record, %with the same number of points as the record length scaledtime = linspace(xzero,xzero+(xincrement*numberofpoints),numberofpoints); %plot the figure with correct scaling figure(1); plot(scaledtime,scaleddata); xlabel('Time (s)'); ylabel('Voltage (V)'); xlim([0 (xzero+(xincrement*numberofpoints))]) fprintf('Plot generated.\n'); %% Close instrument fclose(instrument); delete(instrument); clear instrument;