From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

about DAQ task and writing to a line

Hi all,

  I swtich from Labview to Labwindows and switch from traditional DAQ to DAQmx. I have two questions about using the DAQmx. Based on the example from NI, I write a function for a single sample output to a single digitial line

 

void WriteLine(int zero_or_one, char line_name[])
{
  int         error=0;
  TaskHandle  taskID=0;
  char        errBuff[256];
  
  DAQmxErrChk(DAQmxCreateTask("", &taskID));
  DAQmxErrChk(DAQmxCreateDOChan(taskID, line_name, "", DAQmx_Val_ChanPerLine ));
  DAQmxErrChk(DAQmxStartTask(taskID));
  DAQmxErrChk(DAQmxWriteDigitalLines(taskID, 1, 1, 10.0, DAQmx_Val_GroupByChannel, &zero_or_one, NULL, NULL));
  
  Error:
    if (DAQmxFailed(error)) DAQmxGetExtendedErrorInfo(errBuff, 2048);
    if (taskID!=0) 
    {
      DAQmxStopTask(taskID);
      DAQmxClearTask(taskID);
    }
}

 I am calling this function to a single digital channel. For example, WriteLine(0, "Dev1/port0/line0");. I wonder if above code really writing to a single line or a port (8 lines). I have 4 cables connecting 4 digital lines of one port to 4 external device (so to control the on/off of the device). I use the above code to control one line but don't know why seems sometimes there are something output to other three lines.

 

The second question is: now every time I write to a line, I call above code, so it will create the task each time even I just write one bit to one line. In my code, write to the lines pretty often, so how much it cost to create task every time before I write to a channel? Should I create one task for each channel upon the initialization of the program and clear them when the code closed? I just wonder how labview do that? In labview, will it create the task on each writing?

0 Kudos
Message 1 of 4
(4,100 Views)

One more question about creating task for analog output. Can I create more than one task associated with the same analog output channel on the same PCI card but with different sampling rate? For example, I want to output analoy samples to dev1/A03 with rate 100000 and rate 1000 in different situtation. If I create one task (named task100k)  with rate set to 100,000 and another task (named task1000) with rate set to 1000. Can I have those two tasks exists at the same time without confliction?

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

Creating and destroying a task is a relatively time consuming activity, so if you need to write to the digital lines very frequently it is advisable that you create the task at program start and destroy it at the end, leaving only the start/stop inside the write function.

As of my knowledge, LabVIEW can be structured in a similar way if you use low level functions, while it creates and destroys the task if you use a DAQ assistant (there is a note on this in the help).

 

With reference to your last question, you could have a single task for analog generation and simply set the appropriate rate with calling DAQmxCfgSampClkTiming before starting the task.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 4
(4,090 Views)

@RobertoBozzolo wrote:

Creating and destroying a task is a relatively time consuming activity, so if you need to write to the digital lines very frequently it is advisable that you create the task at program start and destroy it at the end, leaving only the start/stop inside the write function.

As of my knowledge, LabVIEW can be structured in a similar way if you use low level functions, while it creates and destroys the task if you use a DAQ assistant (there is a note on this in the help).

 

With reference to your last question, you could have a single task for analog generation and simply set the appropriate rate with calling DAQmxCfgSampClkTiming before starting the task.


Thanks RobertoBozzolo. I am still in confusion regarding the analog output in DAQmx. I have an array of samples (totally 512 samples), I need to send to a specifc analog channel (A0) such that each sample will be sent at the interval 1millisecond. From your link, I use DAQmxCfgSampClkTiming

 

  int error=0;
  TaskHandle taskid;
  double samples[512];

  // initialize samples here
  DAQmxCreateTask("mytask", &taskid);
  DAQmxCreateAOVoltageChan(taskid, "Dev2/ao0","",-10,10,DAQmx_Val_Volts,NULL);
  
  DAQmxCfgSampClkTiming(taskid, NULL, 1000, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, 512); // sample rate is 1000, so 1sample/ms, 512 samples in total
  DAQmxWriteAnalogF64(taskid, 512,0, 10, DAQmx_Val_GroupByChannel, samples, NULL, NULL);

  // I don't start the task automatically
  // some other time-consuming code here 
  // after other code running, I start it manually

  DAQmxStartTask(taskid);
  DAQmxWaitUntilTaskDone(taskid, 10.0);
  DAQmxStopTask(taskid);

 I wonder if this code looks right? There are 512 samples to send, so I set the numSampsPerChan in DAQmxWriteAnalogF64 to 512, is that correct? 

 

The same channel will be used for other output also. So after the above task done, if I want to send one sample instead of 512 to the same channel, does the following code work?

 

  double one_sample_value = 0.2;
DAQmxStartTask(taskid); DAQmxWriteAnalogScalarF64(taskid, 1, 10.0, one_sample_value,NULL); DAQmxStopTask(taskid);

 What I am asking is do I have to reset the timing since it was set for outputing multiple sample at some rate before but now I only have to send one sample immediately, so does the time or update rate matter?

 

I am porting a big system which written in Labview 8 to CVI but there are so many code needed to be changed, I don't even know how to tell if the DAQ do the same thing or not. So I separate it as above code for testing.

0 Kudos
Message 4 of 4
(4,073 Views)