Pause in for loop gives 'Adaptor command 'clearDevice' failed with status code: -495. ' error when trying to communicate with instrument. what does this mean?

10 views (last 30 days)
I have some code that requires pause to work. I control a device with the instrument control toolbox, but if the device is being written to while it is in a loop with a pause, it eventually crashes with 'Adaptor command 'clearDevice' failed with status code: -495. How do I get around this problem of needing the pauses but the pause causing the crash?
LED_currents = 0:0.01:1;
time = 0;
pause('on')
while true
i = randi(length(LED_currents));
current = LED_currents(i)
command = sprintf('SOUR1:CCUR:LEV:AMPL %0.3f',current);
tf = visastatus(DC2200)
flush(DC2200);
writeline(DC2200,command);
flush(DC2200);
%data = writeread(DC2200,"SOUR1:CCUR:LEV:AMPL?");
%disp(data)
disp('Set current')
pause(1)
time = time+1
end
pause('off')
writeline(DC2200,'OUTP:STAT OFF')
  2 Comments
MULI
MULI on 26 Mar 2025
Edited: MULI on 26 Mar 2025
The timeout error (-495) in your MATLAB code might happens due to excessive flushing and using the "pause()" function, which can interrupt communication with your device. You can try the below steps to avoid this issue:
  • Reduce Flushing: Avoid using "flush(DC2200)" repeatedly unless it's necessary. Frequent flushing can disrupt communication.
  • Use Accurate Delays: Replace "pause(1)" with "tic" and "toc" for a more precise delay. This keeps MATLAB focused on the device without allowing other processes to interrupt.
  • Add Error Handling: You can use a try-catch block to handle errors and prevent the loop from stopping.
  • Increase Timeout: You can also extend the device timeout using "DC2200.Timeout" to allow more time for responses.
Example Code:
LED_currents = 0:0.01:1;
DC2200.Timeout = 10; % Increase timeout
while true
i = randi(length(LED_currents));
current = LED_currents(i);
command = sprintf('SOUR1:CCUR:LEV:AMPL %0.3f', current);
try
writeline(DC2200, command);
disp('Current Set');
% Use tic-toc for a precise delay
tic;
while toc < 0.5
end
catch ME
fprintf('Error: %s\n', ME.message);
end
end
% Turn off output when done
writeline(DC2200, 'OUTP:STAT OFF');
If the error continues, you can reset the device periodically using:
if mod(time, 100) == 0
writeline(DC2200, '*RST'); % Reset device
pause(2);
end
You can refer to the below link for more information on usage of "tic" and "toc" functions:

Sign in to comment.

Answers (0)

Categories

Find more on Instrument Control Toolbox Supported Hardware in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!