Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Order of execution of callbacks from multiple synchronized tasks

I have an application with three tasks: analog input, counter input, and digital input.

 

I start the analog task first, then use "/<dev>/ai/SampleClock" for the other two tasks so acquisition is synchronized.

 

Now this is all for continuous acquisition so following the examples each task has a callback which is called when there is data ready to be processed. I do what processing needs to be done in each one, and store the data in my own arrays. Update to the screen is done via a separate timer that simply reads from these arrays.

 

Now I'm to the stage where I want to output this data to a file, and I see there's a problem: I have three callbacks and no guarantee (that I know of) of execution order. So I cannot simply start dumping data to a file with the callbacks. I tried making all the tasks use the same callback (since they are running off the same clock anyway) but I cannot get around the "ar" (IAsyncResult) parameter that is passed to the callback. In the typical examples, this parameter is first used to verify if the callback is being called by the correct task (ar.AsyncState = <task>), and then it is used again to end the read (for example, CounterInReader.EndReadMultiSampleDouble(ar)).

 

I start the analog task last since it is the one generating the clock for the other two. So what I'd like to do is have the callback of the analog task handle processing of the digital and counter data as well. Then at the end of such callback I can simply dump the data to a file and I'm then guaranteed that the data is in the right order.

 

How can I do this?

0 Kudos
Message 1 of 4
(3,382 Views)

I found a way to do this, which seems to be proper.

 

Create and start all your tasks; in my case I create and start the counter and digital tasks first because the analog one has the active clock (this way I *think* I guarantee all will acquire the same number of points). When you call the appropriate "BeingReadWaveForm" for each task, the digital and counter tasks have "Nothing" (null; i'm in VB here) for the callback, and you store the IAsyncResult in a global variable. You need to store the result of these calls because you need to use that value when you try to end the read.

 

Then your last (and driving) task has a valid callback argument. In that callback now you can do everything in order. According to the documentation, the "EndReadWaveForm" functions are blocking, that is, they don't return until they are done reading, so the callback is guaranteed not to finish executing until all the appropriate data is available to you. Assuming that you are asking for the same number of points in each asynchronous read, it is easy then to just dump the data you just got to a file at the end of your callback.

 

Some other words of advice: the examples seem to always (at least the ones I've seen) set the SynchronizeCallBacks property of the reader objects to True. This is a bad idea; it will make your callback execute in the UI thread which could lead to problems. You should set this to False and take the appropriate precautions to not make illegal cross-thread calls.

0 Kudos
Message 2 of 4
(3,381 Views)

Hi I am having big trouble with multi tasking thing , it seems that my understanding of the concept of task and multi synchronised tasks  is completely wrong , 

What I have is I want to read AI Write AO and write DO and cheque if there is any interruption from the user (read DI ) all at the same time .

I am trying to create a closed loop PID controller to control a hydraulic Presse I need to read the feed back from the pressure sensor then calculate the PID algorithm the send the result to the pompe via (write AO) . I need to send a digital HI to close the release pressure valve and a HI to the start the motor and keep it running, thats a (write DO) ,while the processes is working I have to keep chequeing if there is any interruption from the used to stop the processes that's the (Read DI)

DAC examples are not very clear for me as I am at the beginning with systems

my company had working program taht does all that ,that was tiyed to the NI 6221 card (stopped working ), after purchasing two new cards still not working , now I have to create the same program in VS 2010 .

I have no problem  with the PID or with the concept of the program but my only problem for now is how to do all Tue tasks as discribed . 

Any help will appreciated , thanks example are best for me 

0 Kudos
Message 3 of 4
(2,044 Views)

Hi, have you heard of using producer/consumer loops? You might want to look into them: http://www.ni.com/white-paper/3023/en/

 

Also, it looks like this forum post is very old.  You will get more replies if you open a new forum post.  

0 Kudos
Message 4 of 4
(2,036 Views)