PXI

cancel
Showing results for 
Search instead for 
Did you mean: 

How Do I know if I am talking to PCI_MIO-16E-4 from C++ ?

Hello,
I think I should post the code that I am trying to switch over to NIDAQmx. I know the concept, Ithink I know the concept, just can't get the NI syntax right here. here is the old code.

if(m_DoExternalClock){

unsigned long ulGpctrNum = ND_COUNTER_0;

m_NITimeBuffer = new unsigned long[TimeBufferSize];

 

if(!NISetUpCounter(ulGpctrNum, m_NITimeBuffer, TimeBufferSize, ND_PFI_7,m_NICounter0HighToLow,0))

return FALSE;

if(m_NIUseZeroCounter){

m_NIZeroBuffer= new unsigned long[TimeBufferSize];

ulGpctrNum = ND_COUNTER_1;

if(!NISetUpCounter(ulGpctrNum, m_NIZeroBuffer, TimeBufferSize, ND_PFI_4,m_NICounter1HighToLow,ND_COUNTER_0))

return FALSE;

IncreaseInInputBufferSize += m_NIHalfBufferSize;//add on for counter byte

}

int

CCommunication::NISetUpCounter(unsigned long ulGpctrNum, unsigned long *Buffer, int BufferSize, unsigned long Gate,BOOL HighToLow,unsigned long SyncToGpctrNum)

{

int

Status = pGPCTR_Control(m_NIDeviceNumber, ulGpctrNum, ND_RESET);

Status = pGPCTR_Set_Application(m_NIDeviceNumber, ulGpctrNum,ND_BUFFERED_EVENT_CNT);

unsigned

long Resolution = ND_INTERNAL_100_KHZ;

Status = pGPCTR_Change_Parameter(m_NIDeviceNumber, ulGpctrNum, ND_SOURCE,Resolution);

Status = pGPCTR_Change_Parameter(m_NIDeviceNumber, ulGpctrNum,ND_GATE,Gate);

Status = pGPCTR_Change_Parameter(m_NIDeviceNumber, ulGpctrNum,ND_GATE_POLARITY,Polarity );

Status = pGPCTR_Change_Parameter(m_NIDeviceNumber, ulGpctrNum,ND_INITIAL_COUNT, 0);

Status = pGPCTR_Change_Parameter(m_NIDeviceNumber, ulGpctrNum,ND_BUFFER_MODE, ND_CONTINUOUS);

Status = pGPCTR_Config_Buffer(m_NIDeviceNumber, ulGpctrNum, 0,BufferSize,Buffer);

Status = pGPCTR_Control(m_NIDeviceNumber, ulGpctrNum, ND_PROGRAM);

}

 

Thanks a lot for your help.

 

0 Kudos
Message 21 of 38
(2,508 Views)

NewBe,

I would like you to try your program, taking out the “while (1)” .  The ‘while(1)’ is an endless loop, which will continuously read the counter and should never reach the printf statements, so I am not really sure how you are even reading the values from the array.

After this, please run the program and put a breakpoint after doing the DAQmxReadCounter and before reading the array, so that we can verify the array memory has the counter values in it.

Lastly, using printf(“%u, data[i]) should work, but printf(“%d, data[i]) could work as well. Please let me know the results of these tests. If you are still having trouble, please post back with a screenshot of the array at the breakpoint.

Regards,
David L.
Systems Engineering
National Instruments
Message 22 of 38
(2,503 Views)

Hello,

I can look at the data array inside the read call using the debug mode (Stepping through it), I also used a printf statment after that which gave me zeros. What I wanted to ask you though is, looking at the traditional code, seems like 20 MHZ clk is used as source clock, and PFI7 is used as a gate. I think thats what I want to do, as my encoder will be my pulse IN, I need the 20 MHZ clk to read time, and I need PFI7 and PFI4 for gating. PFI4 would be used for once per revolution. Could you help me translate this concept and the above code into DAQmx, as I am not sure how to set up a counter for gating?

Thanks

0 Kudos
Message 23 of 38
(2,500 Views)

NewBe,

I would like to ask what kind of counter input you are looking to do. From your traditional DAQ code, it looks like you want to do an edge counter input, where the on-board 20Mhz clock is used and your input is brought into the counter source. Please see this knowledgebase:Location of ANSI C NI-DAQmx Shipping Examples and DAQmx Library File and go here: C:\Documents and Settings\All Users\Documents\National Instruments\NI-DAQ\Examples\DAQmx ANSI C\Counter\Count Digital Events\Cnt Dig Events     to find a simple example. No external clock is needed, as in your DAQmx code, to do this counter input. Although, an external clock can be used if you would like. If you need to post back, please provide more explanation around what you are using your two PFI lines for.

David L.
Systems Engineering
National Instruments
Message 24 of 38
(2,480 Views)
Hello David,
Thanks for your help. Well, I am trying to measure about 11 channels in our application from our board via NI. For the time being, I have an encoder and pressure transducer hooked up in order to read voltage and RPM. The encoder is hooked up to PFI4 nad PFI7.  From the traditional code, it looks that 20MHZ was set as source clock, and PFI7 was used as a Gate to get timestamps. PFI4 is used as a Gate for once per revolution counter. I am not sure how to set the 20 MHZ clk as source and how to set other counters as Gates as it was achieved in the traditional code above.
If I do the following, I can successfully count on the PFI4 and PFI7 when the encoder is spinning, and obviously 20MHZ which runs all the time.
DAQmxCreateCICountEdges(handle,"Dev1/ctr0","",DAQmx_Val_Rising,0,DAQ_Val_Countup);
DAQmxSetChanAttribute(handle,"Dev1/ctr0",DAQmc_CI_CountEdges_Term,"/Dev1/PFI7");
DAQmxCreateCICountEdges(handle2,"Dev1/ctr0","",DAQmx_Val_Rising,0,DAQ_Val_Countup);
DAQmxSetChanAttribute(handle2,"Dev1/ctr0",DAQmc_CI_CountEdges_Term,"/Dev1/PFI4");
 
This code allows me to count on these channels. But, how do I use them as Gates and assign 20MHZ as souce? Also, how do I specify continuous mode without using DAQmxCfgTiming? Also, Do I need to assign a channel for 20 MHz clock?
Thanks
0 Kudos
Message 25 of 38
(2,476 Views)
I meant to say "Dev1/ctr1" in the second set of lines (handle2) in the code, not "Dev1/ctr0".
Thanks
0 Kudos
Message 26 of 38
(2,474 Views)

NewBe,

We can follow the example located here: C:\Documents and Settings\All Users\Documents\National Instruments\NI-DAQ\Examples\DAQmx ANSI C\Counter\Count Digital Events\Cnt-Buf-Cont-ExtClk

and adjust it slightly by putting in a DAQmxSetCICountEdgesTerm () function (with counter 0 as the channel, and the 20Mhz timebase as the data) before configuring the sample clock. This will use the 20Mhz clock as your source and your quadrature encoder as the gate. No channel will need to be assigned for the 20Mhz clock (other than declaring Dev1/20MhzTimebase in the above function)

To answer the second part of your question, the DAQmx timing function is how we configure ‘finite’ or continuous samples. If this is set for finite, but you call the daqmx read multiple times, than an error will occur. It needs to be set to ‘continuous’ in order to call the DAQmx read more than once.  I also found a great DevZone document: Transitioning from Traditional NI-DAQ (Legacy) to NI-DAQmx Using ANSI C and NI LabWindows / CVI.

Message Edited by David L. on 08-22-2007 11:25 AM

David L.
Systems Engineering
National Instruments
0 Kudos
Message 27 of 38
(2,468 Views)

Hello David,

Thanks a lot for your help. I did what you suggested, Let me know if thats what you meant.

DAQmxErrChk (DAQmxCreateCICountEdgesChan(taskHandle,

"Dev1/Ctr0","",DAQmx_Val_Rising,0,DAQmx_Val_CountUp));

DAQmxErrChk (DAQmxSetCICountEdgesTerm(taskHandle,

"Dev1/Ctr0","/Dev1/20MHzTimebase"));

DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,

"/Dev1/PFI7",1000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000));

 DAQmxErrChk (DAQmxCreateCICountEdgesChan(taskHandle1,"Dev1/Ctr1","",DAQmx_Val_Rising,0,DAQmx_Val_CountUp));

DAQmxErrChk (DAQmxSetCICountEdgesTerm(taskHandle1,"Dev1/Ctr1","/Dev1/20MHzTimebase"));

DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle1,"/Dev1/PFI4",1000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000));

Q1) So, Does this set the 20MHZ clock as my source and PFI7 & PFI4 as Gates?

Q2) if this code is correct, then the data array should contain Timebase counts from the 20MHZ clock. Correct?

Q3) In the above code I posted earlier with channelAttribute function, was I using PFI7 and PFI4 as sources instead of gates?

Thanks again

0 Kudos
Message 28 of 38
(2,462 Views)

Actually, I am having a problem with the above code, i keep getting an error of "Data is ovewritten before it could be read". I tried changing the freq, the #of samples, Buffersie, but I couldn't get rid of the problem. When I remove the DAQmxCfgSampClkTiming statment, it counts fine without errors, but I need this line so I can make PFI7 and PFI4 act as gates. I know my computer speed is good, 4GRAM, 2.7 GHZ dual Core. I have this read statment in addition to the code above,

DAQmxErrChk (DAQmxReadCounterU32(taskHandle,1000,10.0,data,1000,&read,NULL));

I changed the Number of Samples, the frequency, BufferSize...

Any idea?

Thanks

0 Kudos
Message 29 of 38
(2,454 Views)

Hi NewBe,

Although  we do not typically write code for customers, this has been an ongoing forum, and my goal is to help you succeed. I have posted below a zip file which has C code to do what you are looking for. I personally tested this and verified it works and reads out the count. Please alter the samples to read as necessary.  I also did this because I could not reproduce the error you are getting. This error typically occurs when the 20MhzTimebase is used as the clock because there is only a 2 sample buffer and that rate is too fast, but should not happen if it is used as the input terminal like we have assigned.

Also, since you are doing RPM, I may suggest doing a frequency measurement. This will automatically use the 20Mhz clock, and calculate the frequency. To do this you will create task, create a counter input frequency channel,  start the task, read, clear task. (DAQmxCreateTask, DAQmxCreateCIFreqChan, DAQmxStartTask, DAQmxReadCounterScalarF64 ).

Hope this helps!


David L.
Systems Engineering
National Instruments
0 Kudos
Message 30 of 38
(2,440 Views)