티스토리 뷰

이번 포스팅에서는 C언어에서 DAQmx를 DIO로 제어하기 위한 방법을 알아보도록 하겠습니다.


NI사는 DAQmx에 대한 C언어 기반의 예제를 제공하고 있습니다. 아래의 폴더에서 예제를 찾을 수 있습니다.

위치 : C:\Users\Public\Documents\National Instruments\NI-DAQ\예제




텍스트 언어에서 DAQmx DIO 제어에 대한 순서는 아래와 같습니다. 일반적으로 모든 DAQmx 프로그램은 아래와 같이 사용이 됩니다. LabVIEW든 C언어든 VB든 모두 같습니다. 사용하는 언어의 차이만 있을 뿐입니다.

  1. 태스크를 생성한다.
  2. 태스크를 설정한다.
  3. 태스크를 시작한다.
  4. 태스크를 제어한다.
  5. 태스크를 종료한다. 

그럼 코드를 살펴보도록 하겠습니다.


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

*

* ANSI C Example program:

*    WriteDigChan.c

*

* Example Category:

*    DO

*

* Description:

*    This example demonstrates how to write values to a digital

*    output channel.

*

* Instructions for Running:

*    1. Select the digital lines on the DAQ device to be written.

*    2. Select a value to write.

*    Note: The array is sized for 8 lines, if using a different

*          amount of lines, change the number of elements in the

*          array to equal the number of lines chosen.

*

* Steps:

*    1. Create a task.

*    2. Create a Digital Input/Output channel. Use one channel for all

*       lines.

*    3. Call the Start function to start the task.

*    4. Write the digital Boolean array data. This write function

*       writes a single sample of digital data on demand, so no

*       timeout is necessary.

*    5. Call the Clear Task function to clear the Task.

*    6. Display an error if any.

*

* I/O Connections Overview:

*    Make sure your signal output terminals match the Lines I/O

*    Control. In this case wire the item to receive the signal to the

*    first eight digital lines on your DAQ Device.

*

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


#include <stdio.h>

#include <NIDAQmx.h>


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


int main(void)

{

int32       error=0;

TaskHandle  taskHandle=0; // DO Task

TaskHandle  taskHandle1=0; // DI Task


uInt8       data[8]={1,0,1,0,1,1,1,1}; // 쓸 DO 배열

uInt8 readData[8]; // 읽을 DI 배열


int32 read,bytesPerSamp; // Read 관련 필요 파라미터


char        errBuff[2048]={'\0'};


char quitChar;  // 종료 캐릭터 확인 변수

int i = 0; // 반복문 관련 변수


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

// DAQmx Configure Code

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

DAQmxErrChk (DAQmxCreateTask("",&taskHandle)); // 태스크를 생성합니다.

DAQmxErrChk (DAQmxCreateTask("",&taskHandle1)); // DI와 DO에 필요한 태스크를 각각 생성합니다.


// 생성한 Task에 대한 설정을 한다.

// 이번 예제에서는 DI/DO에 대한 채널을 이용하여 여러 채널을 제어합니다. 

// 이를 위해서 DAQmxCreateDOChan이라는 함수를 사용하였습니다.

// 본 예제에서는 Port0의 0~3번 라인을 DO로 Port0의 4~7라인을 DI로 사용하고 있습니다. 

DAQmxErrChk (DAQmxCreateDOChan(taskHandle,"Dev1/port0/line0:3","",DAQmx_Val_ChanForAllLines));

DAQmxErrChk (DAQmxCreateDIChan(taskHandle1,"Dev1/port0/line4:7","",DAQmx_Val_ChanForAllLines));

quitChar = '0';


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

// DAQmx Start Code

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

DAQmxErrChk (DAQmxStartTask(taskHandle));

DAQmxErrChk (DAQmxStartTask(taskHandle1));


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

// DAQmx Write and Read Code

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


// 여기서 부터 실질적으로 DI/DO를 제어합니다.

 // 알고리즘은 quitChar가 q가 아닌경우 계속 반복해서 실행을 합니다.

while( quitChar != 'q')

{

// 먼저 0~3번 라인에 data 배열에 들어간 값을 Write 합니다.

DAQmxErrChk (DAQmxWriteDigitalLines(taskHandle,1,1,10.0,DAQmx_Val_GroupByChannel,data,NULL,NULL));

// 다음에 4~7번의 DI 값을 읽어 옵니다. 

DAQmxErrChk (DAQmxReadDigitalLines(taskHandle1,1,10.0,DAQmx_Val_GroupByChannel,readData,8,&read,&bytesPerSamp,NULL));


// 읽어온 배열중 4번 라인의 값을 확인합니다.

printf("CH0:%d  ==> Enter 1 char[Exit : q] : ", readData[0]);

// 0~3 번에 쓴 DO 값은 True는 False로 False는 True로 변경합니다. 이 값은 다음 While문에서 사용됩니다.

for(i = 0 ; i < 4 ; i++)

{

data[i] = data[i] == 1 ? 0 : 1;

}


// 값을 하나 입력 받습니다. q가 입력이 되면 프로그램이 종료됩니다.

quitChar = getchar();

}


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);

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

getchar();

return 0;

}



위와 같이 코딩을 하고 나서 프로그램을 실행하면 아래와 같은 화면이 나옵니다.

현재 결선은 Line0과 Line4를 연결하여 Line0의 출력값이 변경될 때마다 Line4의 입력값이 변경되도록 만들어 놓았습니다.



이상입니다.

첨부파일로 위의 코드를 올려놓도록 하겠습니다.


DIO(SWTimed) Sample.zip


보시다가 궁금점이나, 의아한 부분이 있으시면 방명록에 글을 남겨주세요. 성심껏 답변달아 드리겠습니다. 

도움이 되셨다면, 아래의 그림을 클릭해주시기 바랍니다.



댓글