Hello,
I am using the script below to read data from an Agilent oscilloscope. Data transfer in ASCII format this works fine (but slow), but in BYTE or WORD format, I get only a few of the expected thousands of data points (the actual number of data points varies between script executions).
For instance, using BYTE format, I get waveform data like: "#564000ÿÿþÿþþþ", indicating that there are 64000 data points to follow, but there are only 7 present. Where are the other 63993 data points?
Am I forgetting something?
Thanks for your help, Robert
% script agi1.m
%
% Reads waveforms from an Agilent Infiniium 54830B oscilloscope, used with
% Agilent IO-Library Suite 16 (NetFx_IOLibSuite_16_1_14931.exe), Agilent
% 82357A USB/GPIB interface and Matlab R2011a.
%% Reading data in ASCII format works OK (but slow), reading in BYTE or WORD
% format yields much less data points than expected (0 to a few hundred,
% where 64000 or more are expected)
%% last update 18-08-2011
% set some defaults
SamplingRate = 1e7; % samples per second
% select data format (uncomment as appropriate)
% DataFormat = 'ASCii';
DataFormat = 'BYTE';% DataFormat = 'WORD';
switch DataFormat case 'ASCii' InputBufferSize = 1e7; % good for sampling rates up to 1e8
case { 'BYTE', 'WORD' } InputBufferSize = 1e6;end% Find a GPIB object.
obj1 = instrfind('Type', 'gpib', 'BoardIndex', 7, 'PrimaryAddress', 7, 'Tag', '' );% Create the GPIB object if it does not exist, otherwise use the object that was found.
if isempty(obj1) obj1 = gpib('AGILENT', 7, 7);else fclose(obj1); obj1 = obj1(1)end% Configure instrument object, obj1.
set( obj1, 'Timeout', 25.0); % long timeout for slow ASCII transfer
set( obj1, 'InputBufferSize', InputBufferSize );% Connect to instrument object, obj1.
fopen( obj1 );% initialize some parameters
fprintf( obj1, ':SYSTem:HEADer OFF' );fprintf( obj1, ':TIMebase:REFerence LEFT' ); % set T = 0
% set sampling rate
fprintf( obj1, [ ':ACQuire:SRATe ' num2str( SamplingRate ) ] );fprintf( obj1, ':TIMebase:RANGe?' ); % full-scale horizontal time in seconds
TimebaseRange = str2num( fscanf( obj1 ) );% set up data acquisition
fprintf( obj1, ':ACQuire:AVERage OFF' );fprintf( obj1, ':ACQuire:MODE RTIMe' );fprintf( obj1, ':ACQuire:POINts AUTO' );% digitize channel 1 for later read-out
fprintf( obj1, ':DIGitize CHANnel1' );fprintf( obj1, ':CHANnel1:DISPlay ON' );% turn channel 1 display back on
fprintf( obj1, ':WAVeform:SOURce CHANnel1' );fprintf( obj1, [ ':WAVeform:FORMat ' DataFormat ] );% # data points to read
fprintf( obj1, ':WAVeform:POINts?' );WaveformPts = str2num( fscanf( obj1 ) );% read waveform
fprintf( obj1, ':WAVeform:DATA?' );WaveformData = fscanf( obj1 );% restart data acquisition by scope
fprintf( obj1, ':RUN' );% Disconnect all objects.
fclose( obj1 );% set input buffer back to small size
set( obj1, 'InputBufferSize', 512 );% Clean up all objects.
delete( obj1 );%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% process data
switch DataFormat case 'ASCii' Volts = str2num( WaveformData ); case { 'BYTE', 'WORD' } % see page 28_10 of Agilent54830-97014ProgrammersReference.pdf
disp( WaveformData ); % start reading after the first '#'
Idx = findstr( WaveformData, '#' ) + 1 ; Idx = Idx( 1 ); NoHdrBytes = str2num( WaveformData( Idx ) ); NoDataBytes = str2num( WaveformData( Idx + 1 : Idx + NoHdrBytes ) ); if WaveformPts ~= NoDataBytes disp( ' WaveformPts ~= NoDataBytes' ); end DataBytes = WaveformData( Idx + NoHdrBytes + 1 : end ); if length( DataBytes ) ~= NoDataBytes disp( ' length( DataBytes ) ~= NoDataBytes' ); disp( [ ' ' num2str( length( DataBytes ) ) ' ~= ' num2str( NoDataBytes ) ] ); end Volts = double( DataBytes ); end
Best Answer