Is it possible to capture live running traces using oscilloscope via MATLAB without any delay?

I need to capture live running traces from an oscilloscope via MATLAB without any delay. Is it possible?

Answers (1)

Hi,
You can try the Instrument Control Toolbox in MATLAB, which allows you to communicate with and control external instruments such as oscilloscopes.
Hope it helps!

4 Comments

Yeah I've installed it already but thing is I need to capture the running live traces for a large amount without delay. Since my trace points are larger now and I need only to capture the horizontal traces i need help.
Hi,
Since, you've installed the toolbox, you can connect the oscilloscope and use the follow approach :
  1. Configure the oscilloscope settings to capture only the horizontal traces by setting the timebase, trigger settings, and other relevant parameters.
  2. Send configuration commands to the oscilloscope, refer to the documentation or programming manual of your oscilloscope for the specific commands and syntax required.
  3. Set up a loop in MATLAB to continuously capture the live traces. Within the loop, use the appropriate MATLAB commands to acquire the data from the oscilloscope (for example, read or fread). The specific command will depend on the communication protocol and the functions provided by the Instrument Control Toolbox for your oscilloscope.
  4. Process and store the acquired traces as needed.
visaObj = visa('AGILENT', 'USB0::0x0957::0x1790::MY55190481::0::INSTR');
% Create the TCPIP object if it does not exist
% otherwise use the object that was found.
if isempty(visaObj)
visaObj = tcpip(IPAddress, scopePort);
else
fclose(visaObj);
visaObj = visaObj(1);
end
% Set the buffer size. Change this buffer size to slightly over the number
% of bytes you get back in each read
visaObj.InputBufferSize = 350000;
% Set the timeout value
visaObj.Timeout = 1;
% Set the Byte order
visaObj.ByteOrder = 'littleEndian';
% Open the connection
fopen(visaObj)
%% Instrument Setup
% Now setup the instrument using SCPI commands. refer to the instrument
% programming manual for your instrument for the correct SCPI commands for
% your instrument.
% Set acquisition mode to segmented
fprintf(visaObj, ':ACQUIRE:MODE SEGMENTED');
% Set total number of points per segment
fprintf(visaObj, ':ACQUIRE:POINTS 40000');
% Set sample rate
fprintf(visaObj, ':ACQUIRE:SRATE 40e9');
% Turn interpolation off for faster averaging
fprintf(visaObj, ':ACQUIRE:INTERPOLATE OFF');
% Set total number of segments over which to average
fprintf(visaObj, ':ACQUIRE:SEGMENTED:COUNT 100');
% If needed, set the timebase
fprintf(visaObj, ':TIMEBASE:SCALE 100e-6');
% Force a trigger to capture segments
fprintf(visaObj,'*TRG');
% Depending on how many segments are captured, a pause may be necessary in
% order to account for the time required to capture all of the segments
pause(2);
% Specify data from Channel 1
fprintf(visaObj,':WAVEFORM:SOURCE CHAN1');
% Get the data back as a BYTE (i.e., INT8)
fprintf(visaObj,':WAVEFORM:FORMAT BYTE');
% Set the byte order on the instrument as well
fprintf(visaObj,':WAVEFORM:BYTEORDER LSBFirst');
fprintf(visaObj, 'WAVEFORM:STREAMING OFF');
% Get the preamble block
preambleBlock = query(visaObj,':WAVEFORM:PREAMBLE?');
% The preamble block contains all of the current WAVEFORM settings.
% It is returned in the form <preamble_block><NL> where <preamble_block> is:
% FORMAT : int16 - 0 = BYTE, 1 = WORD, 2 = ASCII.
% TYPE : int16 - 0 = NORMAL, 1 = PEAK DETECT, 2 = AVERAGE
% POINTS : int32 - number of data points transferred.
% COUNT : int32 - 1 and is always 1.
% XINCREMENT : float64 - time difference between data points.
% XORIGIN : float64 - always the first data point in memory.
% XREFERENCE : int32 - specifies the data point associated with
% x-origin.
% YINCREMENT : float32 - voltage diff between data points.
% YORIGIN : float32 - value is the voltage at center screen.
% YREFERENCE : int32 - specifies the data point where y-origin
% occurs.
%preambleBlock
% Maximum value storable in a INT8
maxVal = 2^8;
% split the preambleBlock into individual pieces of info
preambleBlock = regexp(preambleBlock,',','split');
% store all this information into a waveform structure for later use
waveform.Format = str2double(preambleBlock{1}); % This should be 0, since we're specifying INT8 output
waveform.Type = str2double(preambleBlock{2});
waveform.Points = str2double(preambleBlock{3});
waveform.Count = str2double(preambleBlock{4}); % This is always 1
waveform.XIncrement = str2double(preambleBlock{5}); % in seconds
waveform.XOrigin = str2double(preambleBlock{6}); % in seconds
waveform.XReference = str2double(preambleBlock{7});
waveform.YIncrement = str2double(preambleBlock{8}); % V
waveform.YOrigin = str2double(preambleBlock{9});
waveform.YReference = str2double(preambleBlock{10 });
waveform.VoltsPerDiv = (maxVal * waveform.YIncrement / 8); % V
waveform.Offset = ((maxVal/2 - waveform.YReference) * waveform.YIncrement + waveform.YOrigin); % V
waveform.SecPerDiv = waveform.Points * waveform.XIncrement/10 ; % seconds
waveform.Delay = ((waveform.Points/2 - waveform.XReference) * waveform.XIncrement + waveform.XOrigin); % seconds
%% Instrument control and data retreival
% Now control the instrument using SCPI commands. refer to the instrument
% programming manual for your instrument for the correct SCPI commands for
% your instrument.
% An optimization to try and speed up the data transfer
fclose(visaObj);
%visaObj.Terminator = '';
fopen(visaObj);
% Declare variables for use in processing the segments
N = 0;
Total_segments = 100;
Avg_N_segments = zeros(waveform.Points,1);
% This will loop through each of the captured segments and pull the data
% from each segment into MATLAB for processing
while (N<=Total_segments)
% This will place the Nth segment on the screen so it can be pulled into
% MATLAB for processing.
fwrite(visaObj, sprintf(':ACQUIRE:SEGMENTED:INDEX %d\n',N));
% Now send commmand to read data
fwrite(visaObj,sprintf(':WAV:DATA?\n'));
% Read back the BINBLOCK with the data in specified format and store it in
% the waveform structure
waveform.RawData = binblockread(visaObj);
% Generate X & Y Data
waveform.XData = (waveform.XIncrement.*(1:length(waveform.RawData))) - waveform.XIncrement;
waveform.YData = (waveform.RawData - waveform.YReference) .* waveform.YIncrement + waveform.YOrigin;
Measurement_Nth_Segment = waveform.YData;
N = N + 1;
% Average the current segment with the average of all the previously
% captured segments
if N>1
Avg_N_segments = (((N-1) .* Avg_N_segments) + (Measurement_Nth_Segment)).*(1/N);
else
Avg_N_segments = Measurement_Nth_Segment;
end
%Uncomment next two lines to see average plotted as segments are processed
plot(waveform.XData,Avg_N_segments); hold on;
pause(0.01);
end
I used those configurations, inserted this code and got a warning "Unsuccessful read: binblock data not received". Can you please sort this issue?
Hi,
The above error occurs mostly when the oscilloscope does not send the expected data in the specified format. You can once check the the communication settings, data format of oscilloscope, buffer size, oscilloscope setting and if the version of matlab toolbox is compatible or not.

Sign in to comment.

Asked:

on 13 Jul 2023

Commented:

on 14 Jul 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!