Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

[C#] Digital Read Continuous

Solved!
Go to solution
Hello,

I’m trying to read data continuously from a NI USB-6251, I’ve already look at the examples and trying to find how to do it.

I would like to acquire an array of bool and then process it to determine the duration of a pulse (I’ve already made it on the counter, but now I need to do it on without counter); I want to do it for a single channel.

Thanks for your help!

My code for now:

***********************************************************

myTask = new Task(pTaskName);

myTask.DIChannels.CreateChannel(pChan, pTaskName + "Chan", ChannelLineGrouping.OneChannelForEachLine);

/*

myTask.Timing.ConfigureSampleClock("", 200, SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, 200);

*/

  myCallBack = new AsyncCallback(FlushBuffer);

myDIReader = new DigitalSingleChannelReader(myTask.Stream);

myDIReader.BeginReadSingleSampleMultiLine(myCallBack, null);

--------------------------------------------------

        public void FlushBuffer(IAsyncResult ar)
        {
            Trace.Write("ReadChan FlushBuffer : ");
            try
            {
                bool[] myTabs = myDIReader.ReadSingleSampleMultiLine();
                Trace.Write(myTabs.Length);
            }
            catch (DaqException daqex)
            {
                MessageBox.Show(daqex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

***********************************************************

I want to do it with the external clock.



Vincent.
Belgian student doing his placement, trying to speak English...
Trying to use NI USB-6251 with C# w/ Measurement Studio.
0 Kudos
Message 1 of 12
(6,074 Views)

Internal clock (sorry for the mistake).

 

In the beging of the examples I often see :

*   This example shows how to measure pulse width on the counter's default input
*   terminal (see I/O Connections Overview below for more information), but
*   could easily be expanded to measure pulse width on any PFI, RTSI, or
*   internal signal.

 

But I don't understand how am I suppose to do, to measure pulse width on any PFI input gate  ?

 

Thanks again.

Message Edité par Vincs le 03-31-2009 03:01 PM



Vincent.
Belgian student doing his placement, trying to speak English...
Trying to use NI USB-6251 with C# w/ Measurement Studio.
0 Kudos
Message 2 of 12
(6,072 Views)
I’ve succeeded with this sample clock configuration:

*
myTask.Timing.ConfigureSampleClock("100kHzTimeBase", 0.002, SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, 2000);
*

But the acquisition is too fast, I’m trying to get one samples each 5 ms and with a buffer of 2000samples.

Can you tell me if it’s right?

*****************************************************************
myTask.DIChannels.CreateChannel(pChan, pTaskName + "Chan", ChannelLineGrouping.OneChannelForEachLine)

myTask.Timing.ConfigureSampleClock("100kHzTimeBase", 0.002, SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, 2000);

myCallBack = new AsyncCallback(FlushBuffer);

myDIReader = new DigitalSingleChannelReader(myTask.Stream);

myDIReader.BeginReadMultiSamplePortUInt16(500,myCallBack, null);


public void FlushBuffer(IAsyncResult ar)
{
    Trace.Write("ReadChan FlushBuffer " + DateTime.Now + ": ");
    try
    {
        ushort[] myData = myDIReader.EndReadMultiSamplePortUInt16(ar);
        Trace.Write(myData.Length + "\n");
        myDIReader.BeginReadMultiSamplePortUInt16(500, myCallBack, null);

    }
    catch (DaqException daqex)
    {
        MessageBox.Show(daqex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    catch (Exception exc)
    {
        Trace.Write("FlushBuffer : " + exc.Message);
    }
}


*****************************************************************



Vincent.
Belgian student doing his placement, trying to speak English...
Trying to use NI USB-6251 with C# w/ Measurement Studio.
0 Kudos
Message 3 of 12
(6,055 Views)
Well, I've succeeded with an external clock, but it could be better to use the internal clock.

So my question is how to divide the internal clock to use it at around 2 kHz for example?

Thanks for your help.



Vincent.
Belgian student doing his placement, trying to speak English...
Trying to use NI USB-6251 with C# w/ Measurement Studio.
0 Kudos
Message 4 of 12
(6,046 Views)

Hi

 

On an M-series device, like your USB-6251, you cannot select a 'real' internal clock for correlated DIO. You can however route the AI, AO or Counter clock. You can simply do this by selecting e.g. the aisampleclock (/Dev1/ai/SampleClock) as the source in the ConfigureSampleClock method. To achieve a rate of 2kHz, you should simply specify a rate of 2000 to the same method to achieve a rate of 2000 samples/second.

 

When measuring pulse width on any of the PFI (or RTSI pins), you are actually just routing the pins to the counter source. So in the end you are still using the counter to measure the pulse width, which limits the number of signals to measure at the same time to 2 in your case. You can select a PFI pin as source for the counter by setting the CountEdgesTerminal property. To see what pins can be routed to the counter source internally, check the device properties in the Measurement & Automation Explorer. The device routes tab gives a nice overview of all possible routes on your device.

Best Regards

Michiel
Applications Engineer
NI Belgium
http://www.ni.com/ask
Message 5 of 12
(6,029 Views)

Ok I see, I will try with another way. I would like to do the less cabling as possible and reduce the external clock.
 
Now I'm trying to use two tasks on two different channels without counter, so on port0.0 and port 0.1, and trying to buffer it, then to get the raw data (0,0,0,0,1,1,1,…) and to process it in C#.
But when I’m trying to launch the task I get this error:
Error Task
I assume that is because, both task trying to access to the same resource, but I don’t see which it is…

Maybe the internal clock or a buffer ?




Vincent.
Belgian student doing his placement, trying to speak English...
Trying to use NI USB-6251 with C# w/ Measurement Studio.
0 Kudos
Message 6 of 12
(6,007 Views)
Solution
Accepted by topic author Vincs
I’ve saw on this forum, that only one task cans access to a port even if this port is divided into multichannel, is that right?

So, I’ve got to find another way to measure pulse width without counter? (I’ve maybe an idea).



Vincent.
Belgian student doing his placement, trying to speak English...
Trying to use NI USB-6251 with C# w/ Measurement Studio.
0 Kudos
Message 7 of 12
(6,003 Views)

This is indeed true. However, nothing holds you from putting the lines togheter in one task and reading them out this way. Afterwards you can then process them again using C# 😉

Best Regards

Michiel
Applications Engineer
NI Belgium
http://www.ni.com/ask
Message 8 of 12
(5,984 Views)

Yes that's what I'm doing basis on this example :

C:\Users\Public\Documents\National Instruments\NI-DAQ\Examples\DotNET1.1\Digital\Read Values\ReadDigPort_ExtClk\cs




Vincent.
Belgian student doing his placement, trying to speak English...
Trying to use NI USB-6251 with C# w/ Measurement Studio.
0 Kudos
Message 9 of 12
(5,981 Views)
I've succeeded (I think so), if someone is interested by the code, you can ask for it and I will send it to you, but I can’t past it all on this forum. (Char limitation).



Vincent.
Belgian student doing his placement, trying to speak English...
Trying to use NI USB-6251 with C# w/ Measurement Studio.
0 Kudos
Message 10 of 12
(5,943 Views)