Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

How to control the acquisition loop?

Hello,
I have the following structure in my code, in the acquisition below, I would like to set some sort of a trigger or condition to control when to start reading. Without the condition,It works fine, But it reads all the time as long as my thread is on. In traditional drivers, I had DAQ-DB-HalfReady flag to control when to start reading. How could I accomplish the same concept here?
Thanks
 
while(Thread is ON)
{  GetNIRawData()....}
 
int GetNIRawData()
{
   if( Condition here){
 
DAQmxReadAnalogBinary16(taskHandle1,ai0:ai3,....) // Read Analog
DAQmxReadCounterU32(taskHandle2, 1000,......) // Read Counter1
DAQmxReadCounterU32(taskHandle3, 1000,......) // Read Counter2
        } // end condition here
}
 
Note if I use the following structure, I get error code -200019,
 
int GetNIRawData()
{
int status = DAQmxReadAnalogBinary16(taskHandle1,ai0:ai3,....) // Read Analog
if(Status==0) // as my condition
  {
     DAQmxReadCounterU32(taskHandle2, 1000,......) // Read Counter1
     DAQmxReadCounterU32(taskHandle3, 1000,......) // Read Counter2
      ........
  }
}
0 Kudos
Message 1 of 6
(7,133 Views)

Hi softwareguy,

If I understand you correctly, you want to know how you can set a condition to start your DAQmx acquisition in C. You should be able to use a normal if statement as you’ve described. In order to help you resolve this error, could you please post a buildable version of your code which includes the task initialization and termination.

A side note: if your goal is to error check your DAQmxReadAnalogBinary function before beginning your counter reads, try using the DAQmxErrChk function instead. In fact, it’s a good practice to use this function!

Regards,

Ryan D.
District Sales Manager for Boston & Northern New England
National Instruments
0 Kudos
Message 2 of 6
(7,126 Views)
Hello Ryan,
What I am trying to do is set a condition before reading the three tasks. But, the condition has to realize that acquisition has started. Is there any property or some sort of indicators in Nidaqmx that would tell me acquisition is started without actually reading any buffers? The reason is my application reads analog and counter data of a motor (Spinning encoder), but if the motor is not on, I don't want to invoke any of the NIDAQmx Read statments, thats why I need to set a condition there to have it read only when motor is spinning.
 
while(....)
{
   if(condition)
    {
      NIDAQmxRead.......
      NIDAQmxRead...
    }
}
0 Kudos
Message 3 of 6
(7,123 Views)

Hi softwareguy,

You could trigger analog input and counter tasks off of your encoder. The example DAQmx – Event Counting on Multiple Counters with Digital Start Trigger shows how you can set a digital start trigger for two counter tasks. For your application, wire your encoder to a PFI line and select that line as your trigger. This method will not require you to read or check the motor state since it won’t begin reading until the encoder starts moving.

Alternatively, if you are using a DAQmx Task to drive your motor, you can use the DAQmxIsTaskDone function to query the execution status of that task. The Boolean variable isTaskDone will return true if the task has completed or is not currently executing and false otherwise. Below is a screen shot of the NI-DAQmx C Reference Help (Start » Programs » National Instruments » NI-DAQmx C Reference Help) on this function.


Please post back if you have any questions. Have a great day!



Message Edited by ryan_d on 12-03-2007 02:43 PM
Ryan D.
District Sales Manager for Boston & Northern New England
National Instruments
0 Kudos
Message 4 of 6
(7,095 Views)

Hello Ryan,

I do have the encoder hooked up to PFI line, It only reads when the motor is on. But in my loop, I say read buffer, if buffer is empty, continue, if Buffer is full, then go on to processing stuff. It will not read data if the motor is on, the buffer will be empty, but How do I set a condition even before that to prevent it even from invoking the read buffer call. Because, even though its not reading data, it still takes say (5 sec trying to read the buffer, I have a timeout of 5 secs). I wanna be able to set a condition to have it even stop the read attempt, so I can save useless time when there is no actual data.

0 Kudos
Message 5 of 6
(7,081 Views)

Hi softwareguy,

Once the read command has been executed, you have to wait either for the hardware to return data, or for the timeout. There is no way to abort a read attempt. We can, however, decide under what conditions we want to start a read task.

The Event Counting example I mention above shows how you can configure a task to start acquisition only after it has received an external digital start signal (trigger). This method will cause your program to wait until it receives the triggering signal before it will execute the read commands. You can continue to do other processing while you wait for the external trigger (your motor) by either making your acquisition thread exclusively an acquisition thread and include all other processing in the parent thread, by creating a new thread for those extra processing steps from your acquisition thread, or by creating a second processing thread from the parent thread.

Ryan D.
District Sales Manager for Boston & Northern New England
National Instruments
0 Kudos
Message 6 of 6
(7,061 Views)