MATLAB Answers

Info

This question is closed. Reopen it to edit or answer.

How to write asynchronously to instrument?

1 view (last 30 days)
Leor Greenberger
Leor Greenberger on 23 May 2013
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi all,
I am trying to control an oscilloscope, and I have the following code for setting it up.
for Chan = 1:4
fprintf(visaObj,[ChanNumStr{Chan} ':TRA ON'],'async');
fprintf(visaObj,['PACU ' num2str(2*Chan-1) ',SDEV,' ChanNumStr{Chan} ',OFF'],'async');
fprintf(visaObj,['PACU ' num2str(2*Chan) ',PKPK,' ChanNumStr{Chan}],'async');
end
What I am trying to do here is set up channels 1 - 4 and some measurement parameters. When you send commands using fprintf to an instrument, MATLAB by default sends them synchronously. That is, it puts the data in the output buffer and waits until the buffer is empty before processing the next line in MATLAB. I would prefer that in this loop, all the commands be queued in the output buffer and for them to be sent asynchronously - that is, not wait for the output buffer to empty. Then later in my code, I could check to see if the output buffer is 0 before proceeding with a block of code that relies on the scope being set up with the commands I sent asynchronously.
Any ideas how I can accomplish this? With my current code, I get an error because if I try to write to the instrument while there is data still in the output buffer, I get a
Error using icinterface/fprintf (line 147)
An asynchronous write is already in progress.
Thus, this is not the way to queue several fprintf statements in the output buffer and have them sent asynchronously.
  3 Comments
Luke Rumbaugh
Luke Rumbaugh on 29 May 2013
you could sort of do your own queuing, too, with some (admittedly ugly) solution like
--get set up--
--> send the first command
while [the output buffer isn't 0]
--do some more stuff--
end
--> send next command
while [waiting for command to clear] ... do stuff
--> send next command
etc
I mean, I wouldn't show it to anyone, but it might work. if you figured out the approximate time it takes to get a command sent then you could load up your while loops with an approximately matching amount of processing (i.e. if it takes 100 ms to clear, you don't want 1 ms of processing or you'll waste time).

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!