Tektronix Technical Forums are maintained by community involvement. Feel free to post questions or respond to questions by other members. Should you require a time-sensitive answer, please contact your local Tektronix support center here.

MATLAB MSO64 / TDK 784 2-byte read binary waveform example

Programming examples for remote communication with instruments
C/C++, C#, Python, Matlab, LabVIEW
Post Reply
Posts: 2
Joined: October 5th, 2020, 8:26 pm
Country: United States

MATLAB MSO64 / TDK 784 2-byte read binary waveform example

Post by RonFred » October 7th, 2020, 4:19 pm

I just started using an MSO64 scope and found that my 1-byte waveform read examples produced noticeable error results. I thought I would share my working example here for a 2-byte binary waveform read and plot using MATLAB and Instrument Control Toolbox. My script incorporates lessons learned from other users as discussed in the script header "Referenced Discussion Threads."

To use the script:
  • Set desired instrument access method in User Inputs code section: gpib / visa (tcpip or usb)
  • Acquire the voltage vs time waveform you would like to capture on your scope
  • Run the script, the MATLAB plot will show the waveform

Code: Select all

%% MATLAB ICT: MSO64 / TDS 784 2-byte waveform read with plot
% Created on Wednesday Oct 6, 2020
% Updated on Thursday Oct 7, 2020
% Tested on both Tek MSO64 and TDS 784D scopes 
% (maybe many other scopes will work also - see lines 63 to 69 to add yours)
% Referenced Discussion Threads:
%   MATLAB ICT: MDO Simple Plot
%   https://forum.tek.com/viewtopic.php?f=580&t=141809
%   MATLAB Oscilloscope App
%   https://www.mathworks.com/matlabcentral/fileexchange/69847-oscilloscope-app
%   Python MSO64 2-byte read binary waveform example
%   https://forum.tek.com/viewtopic.php?f=580&t=142410
%   Python: MDO Simple Plot
%   https://forum.tek.com/viewtopic.php?t=138684
% Author: BiophysicsLab.com

%% User Inputs
visa_vendor = 'NI'; 

% Uncomment and set one of these instrument access methods
% gpib_board = 0; gpib_address = 14;
visa_address = 'TCPIP0::';
%visa_address = 'USB0::0x0699::0x040C::QU000136::INSTR';

input_channel = 'CH1';

timeout = 5; % seconds
buffer_size = 3E6; % large enough to capture 2-byte int16 data

%% Initialize Instrument
% instrument connection config
if ~isempty(instrfindall)

% Use GPIB or VISA
if exist('visa_address', 'var')
    scope = visa('NI', visa_address);
    scope = gpib('NI', gpib_board, gpib_address);
scope.InputBufferSize = buffer_size;
scope.OutputBufferSize = buffer_size;
scope.Timeout = timeout;


%% Instrument control
fprintf(scope,'*cls'); % clear ESR
fprintf(scope,'header OFF'); % disable attribute echo in replies

r = query(scope, '*idn?');
fprintf('%s\n', r);
% Old scopes need channel name in waveform parameter settings
if isletter(r(18))
    % Example: TEKTRONIX,TDS 784D,0,CF:91.1CT FV:v6.4e
    command_mod = input_channel;
    % Example: TEKTRONIX,MSO64,C012872,CF:91.1CT FV:
    command_mod = '';

% Record length settings
record = str2double(query(scope, 'HORizontal:RECORDLength?'));
fprintf(scope,'DATa:STARt 1');
fprintf(scope,['DATa:STOP '  num2str(record)]);

% Acquistion settings
fprintf(scope,['DATA:SOU ' input_channel]);
fprintf(scope,'DATA:WIDTH 2');
fprintf(scope,'DATA:ENC SRI');

%% Get waveform
data = binblockread(scope, 'int16')';
% Tektronix scopes send and extra terminator after the binblock.
fread(scope, 1);

% error checking
r = query(scope, '*esr?');
fprintf('event status register: %s\n', strtrim(r));
r = query(scope, 'allev?');
fprintf('all event messages: %s\n', strtrim(r));
if isempty(data)
    error('binblockread: An error occurred while reading the waveform.');

%% Waveform and Time scale parameters
ymult = str2double(query(scope,['WFMPRE:' command_mod ':YMULT?']));
yzero = str2double(query(scope,['WFMPRE:' command_mod ':YZERO?']));
yoff = str2double(query(scope,['WFMPRE:' command_mod ':YOFF?']));

xincr = str2double(query(scope,['WFMPRE:' command_mod ':XINCR?']));
xzero = str2double(query(scope,['WFMPRE:' command_mod ':XZERO?']));
pre_trig_record = str2double(query(scope,['WFMPRE:' command_mod ':PT_OFF?'])); 

%% Close visa session
clear scope;

%% Waveform and Time scaling

scaled_data = ((data - yoff) .* ymult) + yzero;

total_time = record * xincr;
t_start = (-1 * pre_trig_record * xincr) + xzero;
t_stop = t_start + total_time;
scaled_time = linspace(t_start, t_stop, record);

%% Plot
plot(scaled_time, scaled_data)
title('Channel 1')
xlim([t_start t_stop])
grid on
grid minor

fprintf('\nPlot complete\n')
Command Window Output:
>> getWaveform

event status register: 0
all event messages: 0,"No events to report - queue empty"

Plot complete

Here is a plot using the 2-byte code listed above from the output of a DHT22 temperature/humidity sensor
http://www.biophysicslab.com/wp-content ... window.jpg

Here is a similar plot using 1-byte read binary waveform code (see the two 1-byte python and MATLAB discussion threads in code listing above for examples)
http://www.biophysicslab.com/wp-content ... window.jpg

Post Reply

Return to “Remote Instrument Communication (Programming) Examples”

Who is online

Users browsing this forum: No registered users and 1 guest