Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing TaskHandle between functions - ANSI C

Hi

 

I am trying to create a task handle and pass it to another function (in C), but, I am getting a seg fault when trying to close the task handle. What do I have wrong with the code?

 

Thanks in advance.

 

#include <stdio.h>
#include <NIDAQmx.h>

#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else

 

TaskHandle start() {
      int32 error = 0;
      TaskHandle taskHandle = 0;
      uInt8 data[1] = { 1 };
      uInt8 data_open[1] = { 1 };
      uInt8 data_close[1] = { 0 };
      char errBuff[2048] = { '\0' };

 

      /*********************************************/
      // DAQmx Configure Code
      /*********************************************/
      DAQmxErrChk(DAQmxCreateTask("", &taskHandle));
      DAQmxErrChk(DAQmxCreateDOChan(taskHandle, "Dev1/port0/line13", "", DAQmx_Val_ChanForAllLines));

      /*********************************************/

      // DAQmx Start Code
      /*********************************************/
      DAQmxErrChk(DAQmxStartTask(taskHandle));

      /*********************************************/
      // DAQmx Write Code
      /*********************************************/
      DAQmxErrChk(DAQmxWriteDigitalLines(taskHandle, 1, 1, 10.0, DAQmx_Val_GroupByChannel, data_open, NULL, NULL));

 

      Error: if (DAQmxFailed(error))
            DAQmxGetExtendedErrorInfo(errBuff, 2048);
      if (taskHandle != 0) {
            /*********************************************/
            // DAQmx Stop Code
            /*********************************************/
            DAQmxStopTask(taskHandle);
            DAQmxClearTask(taskHandle);
      }
      if (DAQmxFailed(error))
            printf("DAQmx Error: %s\n", errBuff);

      return taskHandle;
}

int stop(TaskHandle taskHandle) {
      int32 error = 0;
      uInt8 data[1] = { 1 };
      uInt8 data_open[1] = { 1 };
      uInt8 data_close[1] = { 0 };
      char errBuff[2048] = { '\0' };

      

      printf("here1\n");
      DAQmxErrChk(DAQmxWriteDigitalLines(taskHandle, 1, 1, 10.0, DAQmx_Val_GroupByChannel, data_close, NULL, NULL));
      printf("here2\n");

      

      Error: if (DAQmxFailed(error))
            DAQmxGetExtendedErrorInfo(errBuff, 2048);
      if (taskHandle != 0) {
            /*********************************************/
            // DAQmx Stop Code
            /*********************************************/
            DAQmxStopTask(taskHandle);
            DAQmxClearTask(taskHandle);
      }
      if (DAQmxFailed(error))
            printf("DAQmx Error: %s\n", errBuff);

      return 0;

}

 

 

int main(void) {
      printf("Press enter to turn the HVPS on.\n");
      getchar();
      TaskHandle taskHandle = start();

      

      printf("Press enter to turn the HVPS off.\n");
      getchar();
      stop(taskHandle);

      

      printf("End of program, press Enter key to quit\n");
      getchar();
      return 0;
}

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

Not sure whether it's because of you declare taskHandle only in start function? As I know, if you don't declare it in main program, you should not be able to pass it to another function.

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

Hi dchaddock,

 

TaskHandle is an opaque pointer, and DAQmxClearTask() frees the memory that it points to, so your start() function returns a dangling pointer.

 

Brad

---
Brad Keryan
NI R&D
Message 3 of 4
(3,893 Views)

Thanks. Got it sorted out.

 

 

0 Kudos
Message 4 of 4
(3,889 Views)