Keysight Oscilloscope - Visa connection using matlab

27 vues (au cours des 30 derniers jours)
Mohammed Patel
Mohammed Patel le 17 Fév 2021
I need help in reading Channels from all four channels of a Keysight oscillocope.
I can correctly connect to the scope using the instrument control interface. How do I extract the data from the oscilloscope and place it onto a Matlab file/ figure?
I am getting a ' binblockread error' when trying to plot the data.
%% Instrument Connection
% Find a VISA-TCPIP object.
obj1 = instrfind('Type', 'visa-tcpip', 'RsrcName', 'TCPIP0::localhost::inst0::INSTR', 'Tag', '');
% Create the VISA-TCPIP object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = visa('KEYSIGHT', 'TCPIP0::localhost::inst0::INSTR');
else
fclose(obj1);
obj1 = obj1(1);
end
set(obj1, 'OutputBufferSize', 3000);
set(obj1,'InputBufferSize',100000);
% Connect to instrument object, obj1.
fopen(obj1);
IDN = query(obj1,'*IDN?');
fprintf('This is %s', IDN)
fprintf(obj1, ':MEASure:SOURce CHANnel1');
% Reset the instrument and autoscale and stop
fprintf(obj1,'*RST; :AUTOSCALE');
% Set timebase to main
fprintf(obj1,':TIMEBASE:VIEW MAIN');
% Set up acquisition type and count.
fprintf(obj1,':ACQUIRE:MODE RTIME');
% Now tell the instrument to digitize channel1
fprintf(obj1,':DIGITIZE CHAN1');
%% SPECIFY THE SOURCE
% Specify data from Channel 1
fprintf(obj1,':WAVEFORM:SOURCE CHAN1');
% % Specify data from Channel 2
% fprintf(obj1,':WAVEFORM:SOURCE CHAN1');
% % Specify data from Channel 3
% fprintf(obj1,':WAVEFORM:SOURCE CHAN1');
% % Specify data from Channel 4
% fprintf(obj1,':WAVEFORM:SOURCE CHAN1');
% Specify 5000 points at a time by :WAV:DATA?
fprintf(obj1,':ACQ:POINTS 5000');
% Get the data back as a WORD (i.e., INT16), other options are ASCII and BYTE
fprintf(obj1,':WAVEFORM:FORMAT WORD');
% Set the byte order on the instrument as well
fprintf(obj1,':WAVEFORM:BYTEORDER LSBFirst');
fwrite(obj1, 'int16');
fprintf(obj1, ':WAVEFORM:STReaming off'); % Turn off waveform streaming
fprintf(obj1, ':ACQUIRE:MODE SEGMented'); % activate Segmented Memory Mode
fprintf(obj1, ':WAVeform:SEGMented:ALL OFF'); % read all segments at once on/off
fprintf(obj1, ':ACQuire:SEGMented:TTAGs ON'); % turn time-tag feature ON
fprintf(obj1,'DATA:DESTINATION REFA');
%% write a waveform
%% START
noOfSegments = 8; % choose number of segments to be acquired
fprintf(obj1, [':ACQuire:SEGMented:COUNt ' num2str(noOfSegments)]); % set number of segments to be acquired
%data = zeros(198003,noOfSegments); % preallocate data space
%ttag = zeros(198003, noOfSegments);
% capture data on channel 1:
fprintf(obj1,':WAVEFORM:SOURCE CHAN1'); % read only from CH1
%fprintf(obj1,':WAVEFORM:SOURCE CHAN2'); % read only from CH1
fprintf(obj1,':single'); % acquire data (all segments)
pause(.5)
%% read segments
fprintf(obj1, ':wav:data?');
ii = 1;
for ii = 1:noOfSegments
% get segment data
fprintf(obj1, [':ACQuire:SEGMented:INDex ' num2str(ii) ]); % choose segment to read
% request data
fprintf(obj1, [':WAVeform:TYPE?']); %get waveform type
fprintf(obj1, [':WAVeform:POINts?']); %get waveform points
fprintf(obj1, [':WAVeform:DATA?'] ); % read data from scope
fprintf(obj1,':TRAC? TRACE1');
fprintf(obj1, ':SYSTem:HEADer OFF'); % turn off system headers
%
data = binblockread(obj1,'int16');
% fscanf(obj1); %removes the terminator character
end
plot(data)
xlabel('Time (s)');
ylabel('Volts (V)');
title('Oscilloscope Data');
grid on;
  1 commentaire
Sviatoslav Voskresnskyi
Sviatoslav Voskresnskyi le 16 Fév 2024
Modifié(e) : Sviatoslav Voskresnskyi le 16 Fév 2024
Hi, I've been dealing with the same problem for a couple of days now, and I think I've found the solution.
I use 'visadev' instead of 'visa', but you can check the command translations here.
The solution I found might be not that elegant, but it works for sure.
So my scope obejc settinghs are:
ResourceName: "TCPIP0::169.254.195.***::inst0::INSTR"
Alias: "Scope"
Vendor: "KEYSIGHT TECHNOLOGIES"
Model: "DSOS804A"
LANName: "inst0"
InstrumentAddress: "169.254.195.***"
SerialNumber: "MY555*****"
Type: tcpip
PreferredVisa: "National Instruments VISA"
BoardIndex: 0
ByteOrder: "little-endian"
Timeout: 10
Terminator: "LF"
BytesAvailableFcnMode: "off"
BytesAvailableFcnCount: 64
BytesAvailableFcn: []
NumBytesWritten: 69
ErrorOccurredFcn: []
UserData: []
The '':WAVeform:FORMat" is set to ASCii and the answer is querid by writeread(****, ":WAVeform:DATA?") which gives you the single string output. Then by using 'split' function you can retrieve all the sample points from the curve as an individual elements.
Regards,
Sviatoslav

Connectez-vous pour commenter.

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by