Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Event on Counter Rising Edge

Hi,

I'd like to trigger a .NET event when the first rising edge appears on PFI4.  (At the same time, I have other tasks running on the same device.)  I determined that one way to do this is to trap the event that happens when a counter reaches it's terminal count, and have been playing with the following code:

            Device dev = DaqSystem.Local.LoadDevice(DeviceName);
            long counterMax = (long)Math.Pow(2, dev.CIMaxSize) - 1;

            mCounterITask = new Task();
            string ctrChan = string.Format("/{0}/ctr0", DeviceName);
            string edgeSourceChan = string.Format("/{0}/pfi4", DeviceName);
            string clockChan = string.Format("/{0}/ai/SampleClock", DeviceName);
            mCounterITask.CIChannels.CreateCountEdgesChannel(ctrChan, "",
                CICountEdgesActiveEdge.Rising, counterMax - 1, CICountEdgesCountDirection.Up);
            mCounterITask.CIChannels[0].CountEdgesTerminal = edgeSourceChan;

            mCounterITask.CounterOutput += new CounterOutputEventHandler(mCounterITask_CounterOutput);

            mCounterITask.Start();


So, I set the initial counter to one less than the terminla count and try to attach the counter output event.  However, I keep getting error related to the Timing of this task.  I;ve messed with the timing property, but cannot seem to get it right.

Am I approaching this the right way?  What timing settings do I need?

Thanks,
Nick
0 Kudos
Message 1 of 11
(6,605 Views)
Hi Nick,

I understand that you are trying to trigger an event based on reaching the terminal count of your counter. To do this, you are simply counting an edge starting at the initial count of one less than the terminal count value. I am wondering what specific error you are receiving regarding timing (a screenshot would be most beneficial). Also, what is the actual device you are using for your application? Thanks,
Daniel S.
National Instruments
0 Kudos
Message 2 of 11
(6,579 Views)
Hi Daniel,

I am doing my testing on a PCI-6035E, but I'd need to support the USB-6251/6259, and the PCI-6220/6224 in the future.

The code as I presented it throws this exception when I call Start on the task:
Measurements: DAQmx Create Timing Source create an invalid source because the specified Sample Timing Type is not supported when the signal is Sample Clock.

To use this timing source with a Timed Loop, change the Sample Timing Type.

Property: N/A
You Have Requested: NationalInstruments.DAQmx..CounterOutputEvent
You Can Select: NationalInstruments.DAQmx..SampleClock, NationalInstruments.DAQmx..SampleCompleteEvent

Task Name: _unnamedTask<6>

Status Code: -200800

I have also tried, based on this error message, to set
mCounterITask.Timing.SampleTimingType = SampleTimingType.SampleClock;
but this gives me the exception when I try to attach to the CounterOutput event:
Measurements: External sample clock source must be specified for this application.

Unspecified Property: NationalInstruments.DAQmx.Timing.SampleClockSource
Channel Name: Dev1/ctr0

Task Name: _unnamedTask<7>

Status Code: -200303

Then based on this I tried setting Timing.SampleClockSource to /DevX/ai/SampleClock, but then this gives me the first error back again.  I hope you can help.  I don't understand why a counter task needs a sample clock.  Really all I want is an event when the signal goes high on PFI4.

Regards,
Nick

0 Kudos
Message 3 of 11
(6,575 Views)

Hi Nick,

I am still looking into how to make this method of using the Counter Output Event work. However, I am assuming that you are simply interested in triggering a .NET event based on some signal you input on the PFI4 line. In this case, one method that I would suggest trying would be to use the Sample Clock Event, which will trigger an event based on each sample clock. For your application, you could simply configure some task (such as CI Count Edges) and configure the timing for the task to use a sample clock. The source of the sample clock would be the PFI4 line and you would configure the sample mode to be “Hardware Timed Single Point” (you can use an arbitrary rate of 1000 and set the active edge to be rising). This way, when you trigger the PFI line, the Sample Clock Event will register and trigger your event. Let me know if this method will work for your application. Thanks,

Daniel S.
National Instruments
0 Kudos
Message 4 of 11
(6,554 Views)

Hi Daniel S...

     I too ran into the same issue as the original poster. Tried using a CI Task, with a channel set to Count Edges, and setting initial count to one below terminal count, and then use CounterOutputEvent to get the software event. However, I too got error -200800.

 

     Then I tried your suggestion...but it doesn't seem to work. The CI Task never gets any samples. Come to think of it...it's not clear what sample clock timing means for a CI Count Edges Task/Channel...I'm more surprised it doesn't give an error, rather than it doesn't work.

 

     I'm trying other variations of this game..but am getting weary. I'm keen to know if you or anybody has a solution for this sample problem of generating a software event based on a trigger that can be explained.

 

Vijay Iyer

HHMI/Janelia Farm

 

 

0 Kudos
Message 5 of 11
(5,956 Views)

Hi Vijay,

 

I never did get the code I posted working, and am insead using a less-than-optimal polling solution.

 

Sorry I cannot be of more help.

 

/Nick 

0 Kudos
Message 6 of 11
(5,953 Views)

Something that does sort of work is to create a CO Task (Frequency Generation), and then set it to a high sample rate and a finite number of samples (minimum of 2), and set to trigger on edge of interest. Then you can register the DoneEvent, and it will fire callback.  However, I find there is more latency with Done event than with everyNSamples event, so this event will fire after the first EveryN event of the 'real' Task, if it is set to come quite soon.

 

This is pretty vexing.

 

 

0 Kudos
Message 7 of 11
(5,949 Views)

Hello Vijay,

 

Unfortunately, the problem is that the CounterOutputEvent is only supported for a Counter Output task, and will return the -200800 error if used with another task, such as Counter input. This is not intuitive, but it is the case. In fact, the error description has since been changed from what Nick posted above, and now reads as follows:

 

 "Event source signal specified is not supported with the Measurement Type and/or Sample Timing Type of the task."

 

It has been some time, but if I remember correctly, the workaround I posed allows an event to be fired based off of an external trigger occurring on the PFI line. I assumed this was the intended function since the original code was trying to register an event when a pulse was detected and caused the counter to reach terminal count. This code does the same thing, only in a different way by setting the external trigger as a sample clock and firing the SampleClockEvent when it occurs. Please see the VI below for how this is implemented. 

 

Gen Event for Ext Signal.png

Hope this helps,

Daniel S.
National Instruments
0 Kudos
Message 8 of 11
(5,946 Views)

Thanks very much for posting this and verifying that it does work in your hands. I am having trouble getting to work in my hands, and am concerned that perhaps it's an S series board issue. Is there any way you can verify that this does in fact work for S series boards?

 

 

0 Kudos
Message 9 of 11
(5,944 Views)

Good and bad news.

 

I have in fact gotten this idea to largely work, using S series device (also works with other device types).

 

Only issue is that the first edge that occurs on the sample clock source does not generate an event, but then all subsequent events do (causing callback to execute each time).

 

This can be worked around, in my application at least, by sending an extra edge, before any of the other Tasks that wait for this edge are started, but this is not elegant to say the least.

 

I have seen a similar-ish issue in past, with EveryNSamples events. They fail to execute the first time. This I've pretty much resolved by calling DAQmxStopTask() after registering the event via DAQmxRegisterEveryNSamplesEvent()...strange, but it has worked reliably as a fix. Unfortunately, this doesn't seem to help in this case.

 

I will be curious to see if anybody else observes this issue.

 

 

 

0 Kudos
Message 10 of 11
(5,924 Views)