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

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

I have the same problem.
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:

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Instrument Control Toolbox Supported Hardware에 대해 자세히 알아보기

제품

릴리스

R2021a

태그

질문:

2021년 11월 3일

편집:

2025년 3월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by