Matlab connection through oscilloscope

Hi,
I just trying to connect my oscilloscope with my matlab using this code via usb port but it is not working matlab recognises the device but won't execute the code below;
% Find a VISA-USB object.
os = instrfind('Type', 'visa-usb', 'RsrcName', 'USB0::0x0957::0x17A2::MY53510244::0::INSTR', 'Tag', '');
% Create the VISA-USB object if it does not exist
% otherwise use the object that was found.
if isempty(os)
os = visa('KEYSIGHT', 'USB0::0x0957::0x17A2::MY53510244::0::INSTR');
else
fclose(os);
os = os(1);
end
% Connect to instrument object, obj1.
fopen(os);
fprintf(os,'*RST');
fprintf(os,'SELECT:CONTROL CH');
Any suggestions or help maybe ?
Thank you.

13 Comments

is there an error message?
Hi there,
Yes my codes works perfecly when i executed but at the end i get nothing on graph cause it says
VISA Warning: Unable to Read Any Data
The code you posted does not read or attempt to graph.
This is just the introducing part of the oscilloscope part let me share whole part;
clear all;
close all;
format long;
% Find a VISA-USB object.
fg = instrfind('Type', 'visa-usb', 'RsrcName', 'USB0::0x0957::0x1507::MY48014970::0::INSTR', 'Tag', '');
% Create the VISA-USB object if it does not exist
% otherwise use the object that was found.
if isempty(fg)
fg = visa('KEYSIGHT', 'USB0::0x0957::0x1507::MY48014970::0::INSTR');
else
fclose(fg);
fg = fg(1);
end
fopen(fg);
fprintf(fg,'*RST');
% Find a VISA-USB object.
os = instrfind('Type', 'visa-usb', 'RsrcName', 'USB0::0x0957::0x17A2::MY53510244::0::INSTR', 'Tag', '');
% Create the VISA-USB object if it does not exist
% otherwise use the object that was found.
if isempty(os)
os = visa('KEYSIGHT', 'USB0::0x0957::0x17A2::MY53510244::0::INSTR');
else
fclose(os);
os = os(1);
end
% Connect to instrument object, obj1.
fopen(os);
fprintf(os,'*RST');
fprintf(os,'SELECT:CONTROL CH');
% Max amplitude value of the driving voltage of the actuator
amp = 1;
while(amp>0)
%%%%%%%%% Start and Stop Frequencies %%%%%%%%%%%
startfreq = 70;
stopfreq = 300;
%%%%%%%%% Intermediate Freqencies and Step Sizes %%%%%%%%%%%
FREQ(1)=115;
FREQ(2)=145;
%FREQ(3)=-160;
% FREQ(4)=190;
FREQ(3)=230;
FREQ(4)=250;
FREQ(5)=2000;
FREQ(6)=2110;
STEPPRECISE=1;
STEPWIDE=5;
SETTLEWAIT=1;
fstep=0;
%%%%%%%%%% Automatic horizontal scale flags %%%%%%%%
done1=0;
done2=0;
done3=0;
done4=0;
done5=0;
done6=0;
% Initiliaze signal generator for measurement
fprintf(fg,'FUNC SIN'); % Couldn't find this code in the reference!
%fprintf(sg,'VOLT:UNIT VPP');
fprintf(fg,['SOUR1:VOLT:AMPL ',num2str(amp)]); % Set voltage amplitute
fprintf(fg,['SOUR1:FREQ:FIX ',num2str(startfreq)]); % Set the starting frequency
fprintf(fg,'OUTP:STAT ON') % Set sg output channel to ON
% initiliaze oscilloscope for measurement
fprintf(os,'MEASUREMENT:IMMED:SOURCE CH1');
fprintf(os,'MEASUREMENT:IMMED:SOURCE2 CH2');
fprintf(os,'AUTOSET EXECUTE'); % Execute autoset command
pause(1);
fprintf(os,'CH1:POS 0');
fprintf(os,'CH2:POS 0');
pause(1);
%%%%%%%%% 10 secs for manual configuration of oscilloscope %%%%%%%%%%%
fsp=0;
i= startfreq;
resonant_frequency=0;
vel_temp=0;
count = 1;
while( i < stopfreq)
%%%%%%%%% Set steps for intermediate frequencies %%%%%%%%%%%
if( i>= FREQ(2*fsp+1))
if( i <= FREQ(2*fsp+2) )
fstep=STEPPRECISE;
SETTLEWAIT=0.5;
else
fsp=fsp+1;
fstep=STEPWIDE;
SETTLEWAIT=0.5;
end
else
fstep=STEPWIDE;
SETTLEWAIT=0.5;
end
% Oscilloscope adjustments: set horizontal timing scale
if( i < 50 )
if(done1==0)
fprintf(os,'HORIZONTAL:SCALE 0.02');
pause(1);
done1=1;
end
else
if(i<160)
if(done2==0)
fprintf(os,'HORIZONTAL:SCALE 0.004');
pause(1);
done2=1;
end
else
if(i<280)
if(done3==0)
fprintf(os,'HORIZONTAL:SCALE 0.002');
pause(1);
done3=1;
end
else
if(i<800)
if(done4==0)
fprintf(os,'HORIZONTAL:SCALE 0.001');
pause(1);
done4=1;
end
else
if(i<1500)
if(done5==0)
fprintf(os,'HORIZONTAL:SCALE 0.0004');
pause(1);
done5=1;
end
else
if(i<2000)
if(done6==0)
fprintf(os,'HORIZONTAL:SCALE 0.0002');
pause(1);
done6=1;
end
end
end
end
end
end
end
fprintf(fg,['SOUR1:FREQ:FIX ',num2str(i)]); % Update frequency
pause(SETTLEWAIT); % wait for scanner to settle
%%%%%%%%% Sampling for one step %%%%%%%%%%%
N = 5;
temppk2pk = zeros(1,N);
%temppha = zeros(1,N);
for j = 1:N
temppk2pk(j)= 0;
% temppha(j) = 0;
% Measure duty-cycle N times
fprintf(os,'MEASUREMENT:IMMED:TYPE PK2PK');
fprintf(os,'MEASUREMENT:IMMED:VALUE?');
temppk2pk= fscanf(os,'%f'); %Read p2p value of the output signal. (1V per 100mm/s)
end
vel(count) = mean(temppk2pk); %Average of the N measurements
%%%%%%%%%% If this frequency is higher than older ones then this is the resonant frequency %%%%%%%%%%%
if ( vel(count) > vel_temp )
vel_temp = vel(count);
resonant_frequency = num2str(i);
end
%%%%%%%%% Automation ( BETA ) %%%%%%%%%%%
if( vel(count) < 0.04)
fprintf(os,'CH1:SCALE 0.01');
pause(1);
else
if( vel(count) < 0.081)
fprintf(os,'CH1:SCALE 0.02');
pause(1);
else
if ( vel(count) < 0.21)
fprintf(os,'CH1:SCALE 0.05');
pause(1);
else
if ( vel(count) < 0.41)
fprintf(os,'CH1:SCALE 0.1');
pause(1);
else
if ( vel(count) < 0.81)
fprintf(os,'CH1:SCALE 0.2');
pause(1);
else
if ( vel(count) < 2.1)
fprintf(os,'CH1:SCALE 0.5');
pause(1);
else
if ( vel(count) < 4.1)
fprintf(os,'CH1:SCALE 1.0');
pause(1);
else
if ( vel(count) < 8.1)
fprintf(os,'CH1:SCALE 2.0');
pause(1);
else
if ( vel(count) < 20.1)
fprintf(os,'CH1:SCALE 5.0');
pause(1);
end
end
end
end
end
end
end
end
end
%pha(count) = mean(temppha);
ref(count) = i;
count = count + 1;
pause(0.1);
i=i+fstep; %Update the current value of the freq. to the next freq. value
end
pause(1);
figure,plot(ref,vel);
xlabel('Frequency [Hz]');
ylabel('Magnitude [V_{pp}]');
pause(1);
amp=amp-1;
end
%reset all devices and close the gpib session
fprintf(fg,'*RST');
fprintf(os,'*RST');
fclose(fg);
fclose(os);
clear fg;
clear os;
% disp1=vel*100;
% for J=1:length(ref)
% disp(J)=disp1(J)/(6.14*ref(J));
% theta_radian(J)=2*asin(disp(J)/D);
% theta(J)=theta_radian(J)*180/pi;
% thetaDproduct(J)=theta(J)*D;
% end
% plot(ref,disp);
% xlabel('Frequency [Hz]');
% ylabel('Displacement [mm]');
%figure,plot(ref,theta);
%xlabel('Frequency [Hz]');
%ylabel('Angle of Displacement [deg]');
%figure,plot(ref,thetaDproduct);
%xlabel('Frequency [Hz]');
%ylabel('Theta*D product [deg.mm]');
%figure, plot(ref,pha);
%xlabel('Frequency [Hz]');
%ylabel('Phase [deg]');
Here is the whole part of the code and while function generator seems working oscilloscope gives no data to matlab through this code
Is the issue showing up on the line
temppk2pk= fscanf(os,'%f'); %Read p2p value of the output signal. (1V per 100mm/s)
are you being told that no data is available?
I recommend using fgetl(os) and assigning to a variable, and examining what is in the variable, in case you fell out of synchronization. You can sscanf() a string that does turn out to be numeric in form.
Yes its saying that no data variable found to me with orange colored thing. But while using the fget(os) which data should i assigned to and also do i need to use sscanf()
Actually that line u written had like this;
temppk2pk (j)= fscanf(os,'%f'); %Read p2p value of the output signal. (1V per 100mm/s)
but deleted the j part because it is saying that with j part right part and the left part does not have equal elements
success = false;
temp = fgets(os);
if isempty(temp)
%timed out, do something appropriate
else
temp = strtrim(temp);
if ~ismember(temp(1), ['0':'9', '+', '-'])
%response started with something that was not numeric
fprintf('Response not numeric, was: "%s"\n', temp);
else
temp2 = sscanf(temp, '%f');
if isempty(temp2)
fprintf('Response looked numeric at first, but was not. Was "%s"\n', temp);
elseif length(temp2) > 1
fprintf('Got %d numbers when only expected one. Ignoring rest. Response was "%s"\n', length(temp2), temp);
temp2 = temp2(1);
success = true;
else
success = true;
end
end
end
if success
temppk2pk(j) = temp2;
end
well thank you i will try this tomorrow!
But when i replaced my code with this code that u have written above it still gives same error no avaiable data found;
Btw for the information i replaced this part wtih your code;
temppk2pk = zeros(1,N);
%temppha = zeros(1,N);
for j = 1:N
temppk2pk(j)= 0;
% temppha(j) = 0;
% Measure duty-cycle N times
fprintf(os,'MEASUREMENT:IMMED:TYPE PK2PK');
fprintf(os,'MEASUREMENT:IMMED:VALUE?');
temppk2pk= fscanf(os,'%f'); %Read p2p value of the output signal. (1V per 100mm/s)
end
Hi again,
clear all;
close all;
format long;
% Find a VISA-USB object.
fg = instrfind('Type', 'visa-usb', 'RsrcName', 'USB0::0x0957::0x1507::MY48014970::0::INSTR', 'Tag', '');
% Create the VISA-USB object if it does not exist
% otherwise use the object that was found.
if isempty(fg)
fg = visa('KEYSIGHT', 'USB0::0x0957::0x1507::MY48014970::0::INSTR');
else
fclose(fg);
fg = fg(1);
end
fopen(fg);
fprintf(fg,'*RST');
% Find a VISA-USB object.
os = instrfind('Type', 'visa-usb', 'RsrcName', 'USB0::0x0957::0x17A2::MY53510244::0::INSTR', 'Tag', '');
% Create the VISA-USB object if it does not exist
% otherwise use the object that was found.
if isempty(os)
os = visa('KEYSIGHT', 'USB0::0x0957::0x17A2::MY53510244::0::INSTR');
else
fclose(os);
os = os(1);
end
% Connect to instrument object, obj1.
fopen(os);
fprintf(os,'*RST');
fprintf(os,':single');
% Max amplitude value of the driving voltage of the actuator
amp = 1;
while(amp>0)
%%%%%%%%% Start and Stop Frequencies %%%%%%%%%%%
startfreq = 70;
stopfreq = 85;
%%%%%%%%% Intermediate Freqencies and Step Sizes %%%%%%%%%%%
FREQ(1)=115;
FREQ(2)=145;
%FREQ(3)=-160;
% FREQ(4)=190;
FREQ(3)=230;
FREQ(4)=250;
FREQ(5)=2000;
FREQ(6)=2110;
STEPPRECISE=1;
STEPWIDE=5;
SETTLEWAIT=1;
fstep=0;
%%%%%%%%%% Automatic horizontal scale flags %%%%%%%%
done1=0;
done2=0;
done3=0;
done4=0;
done5=0;
done6=0;
% Initiliaze signal generator for measurement
fprintf(fg,'FUNC SIN'); % Couldn't find this code in the reference!
%fprintf(sg,'VOLT:UNIT VPP');
fprintf(fg,['SOUR1:VOLT:AMPL ',num2str(amp)]); % Set voltage amplitute
fprintf(fg,['SOUR1:FREQ:FIX ',num2str(startfreq)]); % Set the starting frequency
fprintf(fg,'OUTP:STAT ON') % Set sg output channel to ON
% initiliaze oscilloscope for measurement
fprintf(os,':WAVEFORM:SOURCE CHAN1');
fprintf(os,'*RST; :AUTOSCALE'); % Execute autoset command
% request data
fprintf(os, ':WAVeform:TYPE?'); %get waveform type
fprintf(os, ':WAVeform:POINts?'); %get waveform points
fprintf(os,':WAVeform:DATA?' ); % read data from scope
fprintf(os,':TRAC? TRACE1');
fprintf(os, ':SYSTem:HEADer OFF');
pause(1);
fprintf(os,'CH1:POS 0');
fprintf(os,'CH2:POS 0');
pause(1);
%%%%%%%%% 10 secs for manual configuration of oscilloscope %%%%%%%%%%%
fsp=0;
i= startfreq;
resonant_frequency=0;
vel_temp=0;
count = 1;
while( i < stopfreq)
%%%%%%%%% Set steps for intermediate frequencies %%%%%%%%%%%
if( i>= FREQ(2*fsp+1))
if( i <= FREQ(2*fsp+2) )
fstep=STEPPRECISE;
SETTLEWAIT=0.5;
else
fsp=fsp+1;
fstep=STEPWIDE;
SETTLEWAIT=0.5;
end
else
fstep=STEPWIDE;
SETTLEWAIT=0.5;
end
% Oscilloscope adjustments: set horizontal timing scale
if( i < 50 )
if(done1==0)
fprintf(os,'HORIZONTAL:SCALE 0.02');
pause(1);
done1=1;
end
else
if(i<160)
if(done2==0)
fprintf(os,'HORIZONTAL:SCALE 0.004');
pause(1);
done2=1;
end
else
if(i<280)
if(done3==0)
fprintf(os,'HORIZONTAL:SCALE 0.002');
pause(1);
done3=1;
end
else
if(i<800)
if(done4==0)
fprintf(os,'HORIZONTAL:SCALE 0.001');
pause(1);
done4=1;
end
else
if(i<1500)
if(done5==0)
fprintf(os,'HORIZONTAL:SCALE 0.0004');
pause(1);
done5=1;
end
else
if(i<2000)
if(done6==0)
fprintf(os,'HORIZONTAL:SCALE 0.0002');
pause(1);
done6=1;
end
end
end
end
end
end
end
fprintf(fg,['SOUR1:FREQ:FIX ',num2str(i)]); % Update frequency
pause(SETTLEWAIT); % wait for scanner to settle
%%%%%%%%% Sampling for one step %%%%%%%%%%%
N = 5;
temppk2pk = zeros(1,N);
%temppha = zeros(1,N);
for j = 1:N
temppk2pk(j)= 0;
% temppha(j) = 0;
% Measure duty-cycle N times
fprintf(os,'MEASUREMENT:IMMED:TYPE PK2PK');
fprintf(os,'MEASUREMENT:IMMED:VALUE?');
temppk2pk= fscanf(os,'%f'); %Read p2p value of the output signal. (1V per 100mm/s)
end
vel(count) = mean(temppk2pk); %Average of the N measurements
%%%%%%%%%% If this frequency is higher than older ones then this is the resonant frequency %%%%%%%%%%%
if ( vel(count) > vel_temp )
vel_temp = vel(count);
resonant_frequency = num2str(i);
end
%%%%%%%%% Automation ( BETA ) %%%%%%%%%%%
if( vel(count) < 0.04)
fprintf(os,'CH1:SCALE 0.01');
pause(1);
else
if( vel(count) < 0.081)
fprintf(os,'CH1:SCALE 0.02');
pause(1);
else
if ( vel(count) < 0.21)
fprintf(os,'CH1:SCALE 0.05');
pause(1);
else
if ( vel(count) < 0.41)
fprintf(os,'CH1:SCALE 0.1');
pause(1);
else
if ( vel(count) < 0.81)
fprintf(os,'CH1:SCALE 0.2');
pause(1);
else
if ( vel(count) < 2.1)
fprintf(os,'CH1:SCALE 0.5');
pause(1);
else
if ( vel(count) < 4.1)
fprintf(os,'CH1:SCALE 1.0');
pause(1);
else
if ( vel(count) < 8.1)
fprintf(os,'CH1:SCALE 2.0');
pause(1);
else
if ( vel(count) < 20.1)
fprintf(os,'CH1:SCALE 5.0');
pause(1);
end
end
end
end
end
end
end
end
end
%pha(count) = mean(temppha);
ref(count) = i;
count = count + 1;
pause(0.1);
i=i+fstep; %Update the current value of the freq. to the next freq. value
end
pause(1);
figure,plot(ref,vel);
xlabel('Frequency [Hz]');
ylabel('Magnitude [V_{pp}]');
pause(1);
amp=amp-1;
end
%reset all devices and close the gpib session
fprintf(fg,'*RST');
fprintf(os,'*RST');
fclose(fg);
fclose(os);
clear fg;
clear os;
% disp1=vel*100;
% for J=1:length(ref)
% disp(J)=disp1(J)/(6.14*ref(J));
% theta_radian(J)=2*asin(disp(J)/D);
% theta(J)=theta_radian(J)*180/pi;
% thetaDproduct(J)=theta(J)*D;
% end
% plot(ref,disp);
% xlabel('Frequency [Hz]');
% ylabel('Displacement [mm]');
%figure,plot(ref,theta);
%xlabel('Frequency [Hz]');
%ylabel('Angle of Displacement [deg]');
%figure,plot(ref,thetaDproduct);
%xlabel('Frequency [Hz]');
%ylabel('Theta*D product [deg.mm]');
%figure, plot(ref,pha);
%xlabel('Frequency [Hz]');
%ylabel('Phase [deg]');
Now i can autoscale at every signal but still could not get the data still same error which is;
Warning: Unsuccessful read: VISA: A timeout occurred
'visa' unable to read any data.
What can I do to get the data from my oscilloscope btw the model is DSOX3054A KEYSIGHT
I do not know.
If you are using National Instruments (NI) Visa drivers, then you might be able to trace I/O; see https://knowledge.ni.com/KnowledgeArticleDetails?id=kA03q000000YHdrCAG&l=en-CA
Actually I am using keysight instruments btw i made process with oscilloscope by replacing the code from its programmer's guide to autoscale the oscilloscope but still can't get the data from the channel 1 of the oscilloscope

Sign in to comment.

Answers (0)

Tags

Asked:

on 24 May 2021

Community Treasure Hunt

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

Start Hunting!