Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

adapt the example of Digital counter events without Ext clock for NI USB-6501

My purpose is to detect an digital signal which is generated by other devices, the generated signal is feeded to P2.7 of NI-USB 6501, P2.7 is reserved for counter according to the document of USB 6501. I used the following codes (which is almost the same as the exampe--DAQmxANSI C\Counter\Count Digital Events\Cnt Dig Events, except 3 line changed codes I marked) to detect the first falling edge of the signal, once the counter numer >0, jump out while loop.

 

The problem is:

 

if I start this application, and feed the outside signal to P2.7 immediately, the counter will get a number > 0, but if I start this application without feeding the outside signal P2.7 immediately, and wait for about a few minutes, and then I start to feed the outside signal to P2.7. The counter doesn't work and application is stuck, even worse the computer can not shutdown anymore.

 

Could you please give me any direction/suggestion? Thanks a lot!

 

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

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

int main(void)
{
 int         error=0;
 TaskHandle  taskHandle=0;
 uInt32      data=0;
 char        errBuff[2048]={'\0'};

 /*********************************************/
 // DAQmx Configure Code
 /*********************************************/
 DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
 

//I changed DAQmx_Val_Rising to DAQmx_Val_Falling 

 DAQmxErrChk (DAQmxCreateCICountEdgesChan(taskHandle,"Dev2/ctr0","",DAQmx_Val_Falling,0,DAQmx_Val_CountUp));

 

 

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

 printf("Continuously polling. Press Ctrl+C to interrupt\n");
 while(data == 0)  //Change While(1) to while(data==0)
 {
  /*********************************************/
  // DAQmx Read Code
  /*********************************************/
  DAQmxErrChk (DAQmxReadCounterScalarU32(taskHandle,-1,&data,NULL));
  
  if(data>0) //add this condition, if the counter number is greater than 0, print its value
  {
   printf("\rCount: %d",data);
   fflush(stdout);
  }
 }

Error:
 puts("");
 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;
}

0 Kudos
Message 1 of 7
(7,283 Views)

Deliang,

 

I do not see anything initially that would make me think there would be a problem with your code. You might try and put a print statement inside the initial while loop where you are checking to see if the data is greater than 0. Possibly print out the value that is being returned by ReadCounter function. I am curious to see if it just never reads a value other than zero or if the program locks up immediately, or only after a signal is given to the device. Let me know. 

 

Aaron W.
National Instruments
CLA, CTA and CPI
0 Kudos
Message 2 of 7
(7,265 Views)

Aaron,

 

I  put print statements inside the while loop just before and after DAQmxErrChk (DAQmxReadCounterScalarU32(taskHandle,-1,&data,NULL))  to print some information and the value of data; I started the application without feeding the outside digital signal, everytime from the very beginning, the application works well and data value is zero, but the application was stuck at DAQmxErrChk (DAQmxReadCounterScalarU32(taskHandle,-1,&data,NULL)) after going through the loop for a while, the computer even can not logoff until I power off the power to the USB 6501.

 

if I started the application and feed the outside signal immediately, it works as we want, but we never know when users will feed in the signal, that is why I would like to test the application without feeding the outside signal. but I run into the above problem, please give me some directions.

 

Thank you very much,

 

Deliang 

 

 


0 Kudos
Message 3 of 7
(7,254 Views)

Aaron,

 

Here is the codes with a fewI added print statements, after starting the application without feeding the outside signal for a while, the application was stuck at the readcounter statement just after printing out "start polling".   Thank you for your kind help.

 

#include <stdio.h>

#include <NIDAQmx.h>

#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; elseint main(void)

{

int error=0;

TaskHandle taskHandle=0;

uInt32 data=0;

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

int i=0;

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

// DAQmx Configure Code

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

DAQmxErrChk (DAQmxCreateTask("",&taskHandle));

DAQmxErrChk (DAQmxCreateCICountEdgesChan(taskHandle,"Dev2/ctr0","",DAQmx_Val_Falling,0,DAQmx_Val_CountUp));

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

// DAQmx Start Code

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

DAQmxErrChk (DAQmxStartTask(taskHandle));

 

printf(
"Continuously polling. Press Ctrl+C to interrupt\n"); while(data == 0)

{

 

printf(
"start polling\n");

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

// DAQmx Read Code

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

DAQmxErrChk (DAQmxReadCounterScalarU32(taskHandle,0,&data,NULL));

i++;

printf(
"Count: %d\n",data);printf("end polling at %i iteration, i\n");

 

fflush(stdout);

}

Error:

puts(
""); if( DAQmxFailed(error) )

DAQmxGetExtendedErrorInfo(errBuff,2048);

if( taskHandle!=0 ) {

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

// DAQmx Stop Code

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

DAQmxStopTask(taskHandle);

DAQmxClearTask(taskHandle);

printf(
"\nclean up task\n");

}

if( DAQmxFailed(error) )

printf("DAQmx Error: %s\n",errBuff);printf(

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

getchar();

return 0;

}

0 Kudos
Message 4 of 7
(7,250 Views)

Deliang,

 

Do you know which version of the DAQmx driver you currently have installed? I am not convinced that this is a driver issue, but if you don't have the most recent driver you might upgrade to the latest DAQmx driver and see if you notice any difference in behavior. DAQmx 8.9 is the latest version. 

Aaron W.
National Instruments
CLA, CTA and CPI
0 Kudos
Message 5 of 7
(7,246 Views)

Aaron,

 

We solved the problem just by connecting to another USB port on the computer. but the orignal port we have been used works well with other devices. I don't know why. Do you know the reason?

 

Thanks,

 

Deliang

0 Kudos
Message 6 of 7
(7,237 Views)

Deliang,

 

I do not know exactly why one USB port would cause that problem. My guess is that it is a problem with the hardware of the computer and not something with your program or the DAQmx driver. Glad to hear everything is working. 

Aaron W.
National Instruments
CLA, CTA and CPI
0 Kudos
Message 7 of 7
(7,234 Views)