LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Having trouble reading an analog input using DAQmx

This seems like it should be pretty simple, but I am having some trouble. I keep getting the error shown below when I attempt to read an analog input.  I'm sure that I'm just doing something fairly stupid or naive, so can someone suggest what's wrong with the DAQmx usage shown?

analog error.png

0 Kudos
Message 1 of 8
(1,630 Views)

Best guess from this small piece of code is that the error comes because the hardware is in use already.  You either have it open in another program (could even be test panels in NI-MAX), or in the same program you started using it but didn't stop using it, or you get this error message because this code is in a loop and you didn't stop the task in the previous loop.  

Message 2 of 8
(1,626 Views)

You should add a DAQmx Clear Task after the DAQmx Read to unreserve the hardware.

Anyway, using CreateTask-Read-ClearTask in a loop is not a good idea. You should place the CreateTask before the loop and the ClearTask after the loop. I would recommend you to refer to the shipping examples.

-------------------------------------------------------
Applications Engineer | TME Systems
0 Kudos
Message 3 of 8
(1,617 Views)

Thanks, gentlemen. I will take these suggestions to heart. However, I also established that this error occurs during the very first read, so this problem involves more than just multiple iterations through the loop.

 

0 Kudos
Message 4 of 8
(1,585 Views)

One other thing to consider is that you are not using the Error Line in your very minimal "program".  You should almost always wire Error In and Error Out, which will give you an error when the first function that has a problem "has a problem".  You don't even know if the (tiny, isolated) piece of code you attached is the source of the Error!

 

Next time, please attach a complete LabVIEW VI (or, even better, a complete LabVIEW Project, most easily done by right-clicking the Project folder on your disk and choosing "Send to", "Compressed (zipped) folder", then attach the result.  Having executable LabVIEW code lets us "find the bug" much faster and with less "guessing".

 

Bob Schor

0 Kudos
Message 5 of 8
(1,531 Views)

Bob, you're right to say that the error line wasn't wired up. This was just a minimal initial test within a very large existing project, not yet intended as a full solution.  (The problem only manifested after we added this segment, so we know that this is causing the error to occur.)
It would take quite a while before I can boil the entire project down into a reasonably sized VI.  In the meantime though, I discovered that this problem occurs because we're also reading AI0 in another section of the code.  The following segment produces an error.  (I deliberately omitted the DAQmx reads for simplicity and because the errors appear before the read is performed.)

error - two tasks.png

0 Kudos
Message 6 of 8
(1,458 Views)

The reason you have the Error, I think, is you are asking the hardware to do two different things at the same time, namely read 250 samples from ai0 and 250 samples from ai1.  The hardware knows (I certainly hope!) how to read two sequential channels at a time, so you could say "read ai0:1 at 1 kHz until you have a 2D array of size 2 x 1000 (or 1000 x 2, depending how you set up DAQmx).  You might have to pry the two data channels apart (because they have different scale factors, perhaps), but you do that when you analyze (or maybe plot) the data, not when you collect it.

 

See if this change gets rid of the error.

 

Bob Schor

0 Kudos
Message 7 of 8
(1,452 Views)

That sounds plausible. Thanks.

0 Kudos
Message 8 of 8
(1,416 Views)