165 lines
3.5 KiB
C
165 lines
3.5 KiB
C
#include "hdio.h"
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include <NIDAQmx.h>
|
|
|
|
#define SAMPLE_COUNT 1
|
|
#define BUFFER_SIZE 96
|
|
#define PORT_RANGE "Dev2/line0:95"
|
|
#define READ_TIMEOUT 2.0
|
|
|
|
#define DAQmxErrChk(functionCall) \
|
|
do { if( DAQmxFailed(error=(functionCall)) ) \
|
|
goto Error; } while(0);
|
|
|
|
/**
|
|
* This structure encapsulates the data that is private to
|
|
* the implementation of the NI DAQ counter interface
|
|
*/
|
|
typedef struct counter_private_t
|
|
{
|
|
/** NIDAQ opaque task handle */
|
|
TaskHandle taskHandle;
|
|
/** Actual physical data value, as returned by NIDAQ read function */
|
|
uInt8 data[BUFFER_SIZE];
|
|
/** previous physical data value */
|
|
uInt8 old_data[BUFFER_SIZE];
|
|
/** NIDAQ device number of card */
|
|
int device_number;
|
|
/** number of samples read */
|
|
int32 numRead;
|
|
/** number of bytes per sample */
|
|
int32 bytesPerSamp;
|
|
} COUNTER_PRIVATE;
|
|
|
|
|
|
int hdio_ctor(const char* device_name, pHDIO* ptr)
|
|
{
|
|
int error = 0;
|
|
bool flag = false;
|
|
char text_string[] = "dev1/line0:7";
|
|
const char *name;
|
|
const char *text;
|
|
|
|
*ptr = (COUNTER_PRIVATE*) malloc(sizeof(COUNTER_PRIVATE));
|
|
memset(*ptr, 0, sizeof(COUNTER_PRIVATE));
|
|
|
|
name = device_name;
|
|
text = text_string;
|
|
while (name && *name)
|
|
{
|
|
if (isspace(*name))
|
|
++name;
|
|
else if (*name >= '0' && *name <= '7')
|
|
{
|
|
if (flag)
|
|
{
|
|
}
|
|
else
|
|
{
|
|
(*ptr)->device_number = *name - '0';
|
|
flag = true;
|
|
}
|
|
}
|
|
else if (tolower(*name) != *text)
|
|
{
|
|
/* TODO error */
|
|
break;
|
|
}
|
|
++name;
|
|
++text;
|
|
}
|
|
/*********************************************/
|
|
// Create a DAQmx task
|
|
/*********************************************/
|
|
DAQmxErrChk (DAQmxCreateTask("",&(*ptr)->taskHandle));
|
|
|
|
/*********************************************/
|
|
// Create a DAQmx port within the task
|
|
/*********************************************/
|
|
DAQmxErrChk (DAQmxCreateDIChan((*ptr)->taskHandle,
|
|
PORT_RANGE,
|
|
"",
|
|
DAQmx_Val_ChanPerLine));
|
|
|
|
/*********************************************/
|
|
// Start the DAQmx task
|
|
/*********************************************/
|
|
DAQmxErrChk (DAQmxStartTask((*ptr)->taskHandle));
|
|
|
|
return 0;
|
|
Error:
|
|
free(*ptr);
|
|
*ptr = NULL;
|
|
return error;
|
|
}
|
|
|
|
int hdio_read(pHDIO hdio, unsigned long long* value)
|
|
{
|
|
int error = 0;
|
|
error = 0;
|
|
hdio->numRead = 0;
|
|
hdio->bytesPerSamp = 0;
|
|
/*********************************************/
|
|
// DAQmx Read Code
|
|
/*********************************************/
|
|
error = DAQmxReadDigitalLines (hdio->taskHandle,
|
|
SAMPLE_COUNT,
|
|
READ_TIMEOUT,
|
|
DAQmx_Val_GroupByScanNumber,
|
|
hdio->data,
|
|
BUFFER_SIZE,
|
|
&hdio->numRead,
|
|
&hdio->bytesPerSamp,
|
|
NULL);
|
|
|
|
if (error == DAQmxErrorSamplesNotYetAvailable)
|
|
{
|
|
printf("Timeout, reading raw data\n");
|
|
error = 0;
|
|
}
|
|
else if (hdio_failed(error))
|
|
goto Error;
|
|
else
|
|
{
|
|
// TODO copy the data
|
|
}
|
|
|
|
return error;
|
|
Error:
|
|
return error;
|
|
}
|
|
|
|
int hdio_dtor(pHDIO* hdio)
|
|
{
|
|
if( hdio && *hdio && (*hdio)->taskHandle!=0 )
|
|
{
|
|
/*********************************************/
|
|
// DAQmx Stop Code
|
|
/*********************************************/
|
|
DAQmxStopTask((*hdio)->taskHandle);
|
|
DAQmxClearTask((*hdio)->taskHandle);
|
|
}
|
|
(*hdio)->taskHandle = 0;
|
|
free (*hdio);
|
|
*hdio = NULL;
|
|
return 0;
|
|
}
|
|
|
|
bool hdio_failed(int error)
|
|
{
|
|
if (DAQmxFailed(error))
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
|
|
void hdio_errmsg(char* buff, int len)
|
|
{
|
|
*buff = '\0';
|
|
DAQmxGetExtendedErrorInfo(buff, len);
|
|
}
|
|
|