12-08-2008 01:10 PM
Hi,
I’m trying to build an MFC application with MSVC++6, where I would continuously acquire
samples
from 2 channels of a USB-6289. I’m using the DAQmx C functions. First I
declear the callback fun as static function in the dialog class:
class DataCollectionWin : public CDialog
{.....
static int32 CVICALLBACK
EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType,
uInt32 nSamples, void *callbackData);
static int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData);
CString str;
char* FileName;
}
Click "start tracking" to start data collection and save to an excel file:
void DataCollectionWin::OnStartTracking()
{
// TODO: Add your control notification handler code here
float64 data[2000];
int32 read=0;
int32 errorcode=0;
int i,length;
int32 error=0;
char errBuff[2048]={'\0'};
//********Connect DAQ channels***********
CString szButtonText;
CString szFilter = "Excel Files (*.xls)|*.xls|All Files (*.*)|*.*||";
CFileDialog dlg(FALSE,"Excel",NULL,OFN_OVERWRITEPROMPT,szFilter);
GetDlgItem( IDStartTracking)->GetWindowText( szButtonText );
/* if the text read Start, then we start, else we stop tracking */
if ( szButtonText == "Start Tracking" )
{
if(dlg.DoModal()==IDOK)
{
str=dlg.GetPathName ();
UpdateData(TRUE);
if(!cfData.Open (str,CFile::modeCreate|CFile::modeReadWrite))
{
MessageBox("create/read/write file fail","warning",
MB_ICONWARNING | MB_OK);
}
length=str.GetLength();
FileName=new char[length];
for(i=0;i<length;i++)
*(FileName+i)=str.GetAt(i);
*(FileName+length)=NULL;
DAQmxErrChk(DAQmxCreateTask ("connect", &taskHandle));
DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0,Dev1/ai1","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));
DAQmxErrChk(DAQmxReadAnalogF64(taskHandle,1000,-1,DAQmx_Val_GroupByChannel,data,2000,&read,NULL));
DAQmxErrChk
(DAQmxRegisterEveryNSamplesEvent(taskHandle,DAQmx_Val_Acquired_Into_Buffer,1000,0,EveryNCallback,NULL));
DAQmxErrChk (DAQmxRegisterDoneEvent(taskHandle,0,DoneCallback,NULL));
Error:
if( DAQmxFailed(error) ) {
DAQmxGetExtendedErrorInfo(errBuff,2048);
MessageBox(errBuff);
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
return;
}
GetDlgItem( IDStartTracking )->SetWindowText("Stop Tracking");
}
}
else
// nStopTracking();
{ GetDlgItem( IDStartTracking )->SetWindowText("Start Tracking");
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
cfData.Close();
}
}
static int32 EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData)
{
int32 error=0;
char errBuff[2048]={'\0'};
static int totalRead=0;
int32 read=0;
float64 data[1000];
FILE *datafile;
datafile = fopen(FileName,"w+");
int i;
if (datafile == NULL )
printf( "The file was not opened\n" );
/*********************************************/
// DAQmx Read Code
/*********************************************/
DAQmxErrChk(DAQmxReadAnalogF64(taskHandle,1000,-1,DAQmx_Val_GroupByChannel,data,2000,&read,NULL));
printf("data number: %d\n",read);
if( read>0 ) {
for (i=0;i<2000;i++)
{
fprintf(datafile,"%f\n",data[i]);
}
fflush(stdout);
}
Error:
if( DAQmxFailed(error) )
{
DAQmxGetExtendedErrorInfo(errBuff,2048);
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
printf("DAQmx Error: %s\n",errBuff);
return 0;
}
}
static int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData)
{
int32 error=0;
char errBuff[2048]={'\0'};
printf("hello");
// Check to see if an error stopped the task.
DAQmxErrChk (status);
Error:
if( DAQmxFailed(error) ) { DAQmxGetExtendedErrorInfo(errBuff,2048);
DAQmxClearTask(taskHandle);
}
return 0;
}
Can anyone check the codes for me? when i run the program, no data is save into the excel file. It seems the EveryNCallback function does not work, because when I put "printf("hello"); "for checking,nothing happens. Also there is an debug assertion error (see enclosure).
Actually how does the callback function work to continuously collect the data?
Thank you!
kg
12-09-2008 03:00 PM
12-10-2008 01:53 AM
Hello,
So is your read code not working at all? Are you getting data and just not able to write it to excel?
Have you tried the ANSI C examples? Which version of Measurement Studio are you using?
12-10-2008 08:37 AM
Hi, David,
Thank you very much for your reply. When I run the ANSI C sample, it seems ok. I tried to use DAQmxReadAnalogF64 to collect 2000 samples and save to a CFile. No problem. But when I add the everyNCallback to continuously collect data, then it seems not work. Do I need to add head file for measurement studio? I only add "NIDAQmx.h". Must I use measurement studio to complete this task? I thought I can directly use NIDAQmx library for this purpose. Please advice. Thanks!
12-10-2008 09:46 AM
Hello,
You can use the DAQmx functions, yes. I was just curious how Measurement Studio played into this equation since you posted in the Measurement Studio forum rather than the Multifunction DAQ forum. Have you tried the "Cont Acq - Int Clk" example? This should give you a template for acquiring data continuously using ANSI C.
12-10-2008 10:15 AM
12-10-2008 11:08 AM
Hello,
It looks the original post was in the LabVIEW forum. For future reference, the best place for questions about the DAQmx functions or programming in DAQmx (even text-based) is the Multifunction DAQ forum, this forum is for questions about Measurement Studio functions or programming.
All that aside, have you tried the standard debugging methods? IE diff-ing your program with the example, single-stepping to see exactly where you are getting the error? Have you tried using any other callbacks successfully? Did you check the C++ documentation for asserts as mentioned in that error?
12-10-2008 12:52 PM
I tried to debug. It didn't get into the everyNcallback function. I get following error information. It seems that I miss sth. Can anyone give me a clue?
Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\nicaiu.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\nipalu.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\nipalut.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\msvcr71.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\nipal32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\advapi32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\rpcrt4.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\secur32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\setupapi.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\msvcrt.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\gdi32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\user32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\shell32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\shlwapi.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\niorbu.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\nirpc.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\wsock32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\ws2_32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\ws2help.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\nimdbgu.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\nimstsu.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\nimxdfu.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\nidmxfu.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\nimru2u.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\nidimu.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\nimxpu.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\nimhwcfu.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\ole32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\oleaut32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\nimercu.dll', no matching symbolic information found.
12-10-2008 12:53 PM
12-10-2008 12:53 PM