Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

read multiple analog inputs at the same time

Solved!
Go to solution

Hello all,

 

I am using USB-6001 and want to develop a multiple task app in C++. I am trying to read multiple analog inputs at the same time, but get some errors. To make it simple, I copy one of the example code to read in analog data in one channel then turn it into a function. Then I call this function in thread with different pin names (such as Dev1/ai0, Dev1/ai1) and I come cross this error:

 

"The specified source is reserved. The operation could not be complete as specified"  status code -50103

 

I search the forums, this may because I use hardware timing in this function, and this hardware timing cannot be use at the same time by several tasks. I may have to put all lines I want to read in one single task (such as Dev1/ai0:1). In this way I can read two lines at the same time. However, when I try this, I meet other error:

 

"buffer is too small to fit read data" status code -200299

 

So, here is my question, what should I do if I do want to read multiple analog inputs at the same time? Is the thing that hard timing cannot be use by multiple tasks true? If I have to read multiple lines at a single task, how do I set the parameters?

0 Kudos
Message 1 of 14
(7,923 Views)
Your approach is just not correct. You must use a single task with multiple channels. The channel list would look something like Dev1/ai0:3 if you wanted channels 0 through 3. You can define a task in MAX, save it, and call it in your program. Your timing has to be the same for all channels.
0 Kudos
Message 2 of 14
(7,915 Views)

Thank you for your reply! I have tried to put "Dev1/ai0:1" to a single task, but I still have error, saying the buffer is 100 but the data read is 200 (I read 100 samples per channel, but now I have two channels). Can you give me some hints about this?

0 Kudos
Message 3 of 14
(7,910 Views)
I am not that familiar with c++ and how you set the buffer. In LabVIEW, the buffer size setting is usually automatic. Look again at doing the setup in MAX is what I would suggest.
0 Kudos
Message 4 of 14
(7,908 Views)

This is my .VI file, I stuck with this same problem. 

Could you recommend me for more?

0 Kudos
Message 5 of 14
(7,897 Views)

Hi,

 

I think I have partially solved the problem. But I am using C++, so I am not sure if I can help you. Here is what I did. It is sure that we cannot read separated lines in different tasks at the same time, because there is only one internal clock inside the device. The solution is reading multiples lines in the same task. The key is, when defining the data array into which we will put the acquired data (when using the function "DAQmxReadAnalogF64 "), you have to make its size big enough to hold all acquired data, otherwise you will get a buffer error like I had before. For example, you are reading 3 lines at the same time (e.g. Dev1/ai0:2), each line you want 100 samples. Then you need to defined the data array as:

 

data[300]

 

so that you have enough space to hold all data. I hope this can help you a little bit. Good luck!

0 Kudos
Message 6 of 14
(7,878 Views)

Now I can read Analog signal but still stuck with read only first time then not to read anymore. Any Ideas?

0 Kudos
Message 7 of 14
(7,831 Views)

You are only reading the data once and passing those values into the loops.  As I stated here, you need to take your two loops and make them one.  You will have to mess around with how you stop your loop, but it will work.

 

Alternatively, you could have one loop that just does the reading and then use queues or notifiers to send the data to these processing loops.  You may want to have a look at the Producer/Consumer.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 8 of 14
(7,817 Views)

Do you have more Examples with Daq Assistant? (.vi file) Thank a lot

0 Kudos
Message 9 of 14
(7,808 Views)

Hello,

 

Thank you for replying, but I think you are well advanced in the topic! For most end-users, they just try to use the functions provided, once, not in a loop, without trying to look into how the function works. The problem is that, the description of the C functions are sometimes confusing. I always have to guess how actually I should set the parameters. For example, I know that the analog input ports have a "differential mode", but I still cannot find any documentation about this. I think NI has put too much attention to the Labview support, while C users are less taken care off.

0 Kudos
Message 10 of 14
(7,807 Views)