private void RapidBlockDataHandler(ushort nRapidCaptures) { short status; int numChannels = _channelCount; uint numSamples = BUFFER_SIZE; // Run the rapid block capture int timeIndisposed; _ready = false; _callbackDelegate = BlockCallback; Imports.RunBlock(_handle, 0, (int)numSamples, _timebase, _oversample, out timeIndisposed, 0, _callbackDelegate, IntPtr.Zero); // Set up the data arrays and pin them short[][][] values = new short[nRapidCaptures][][]; PinnedArray <short>[,] pinned = new PinnedArray <short> [nRapidCaptures, numChannels]; for (ushort segment = 0; segment < nRapidCaptures; segment++) { values[segment] = new short[numChannels][]; for (short channel = 0; channel < numChannels; channel++) { if (_channelSettings[channel].enabled) { values[segment][channel] = new short[numSamples]; pinned[segment, channel] = new PinnedArray <short>(values[segment][channel]); status = Imports.SetDataBuffersRapid(_handle, (Imports.Channel)channel, values[segment][channel], (int)numSamples, segment); } else { status = Imports.SetDataBuffersRapid(_handle, (Imports.Channel)channel, null, 0, segment); } } } // Read the data short [] overflows = new short[nRapidCaptures]; status = Imports.GetValuesRapid(_handle, ref numSamples, 0, (ushort)(nRapidCaptures - 1), overflows); // Un-pin the arrays foreach (PinnedArray <short> p in pinned) { if (p != null) { p.Dispose(); } } //TODO: Do what ever is required with the data here. }
/**************************************************************************** * BlockDataHandler * - Used by all block data routines * - acquires data (user sets trigger mode before calling), displays 10 items * and saves all to data.txt * Input : * - unit : the unit to use. * - text : the text to display before the display of data slice * - offset : the offset into the data buffer to start the display's slice. ****************************************************************************/ void BlockDataHandler(string text, int offset) { uint sampleCount = BUFFER_SIZE; PinnedArray <short>[] minPinned = new PinnedArray <short> [_channelCount]; PinnedArray <short>[] maxPinned = new PinnedArray <short> [_channelCount]; int timeIndisposed; for (int i = 0; i < _channelCount; i++) { short [] minBuffers = new short[sampleCount]; short [] maxBuffers = new short[sampleCount]; minPinned[i] = new PinnedArray <short>(minBuffers); maxPinned[i] = new PinnedArray <short>(maxBuffers); Imports.SetDataBuffers(_handle, (Imports.Channel)i, minBuffers, maxBuffers, (int)sampleCount); } /* find the maximum number of samples, the time interval (in timeUnits), * the most suitable time units, and the maximum _oversample at the current _timebase*/ int timeInterval; int maxSamples; while (Imports.GetTimebase(_handle, _timebase, (int)sampleCount, out timeInterval, _oversample, out maxSamples, 0) != 0) { _timebase++; } Console.WriteLine("Timebase: {0}\toversample:{1}", _timebase, _oversample); /* Start it collecting, then wait for completion*/ _ready = false; _callbackDelegate = BlockCallback; Imports.RunBlock(_handle, 0, (int)sampleCount, _timebase, _oversample, out timeIndisposed, 0, _callbackDelegate, IntPtr.Zero); /*Console.WriteLine("Run Block : {0}", status);*/ Console.WriteLine("Waiting for data...Press a key to abort"); while (!_ready && !Console.KeyAvailable) { Thread.Sleep(100); } if (Console.KeyAvailable) { Console.ReadKey(true); // clear the key } Imports.Stop(_handle); if (_ready) { short overflow; Imports.GetValues(_handle, 0, ref sampleCount, 1, Imports.DownSamplingMode.None, 0, out overflow); /* Print out the first 10 readings, converting the readings to mV if required */ Console.WriteLine(text); Console.WriteLine("Value {0}", (_scaleVoltages) ? ("mV") : ("ADC Counts")); for (int i = offset; i < offset + 10; i++) { for (int j = 0; j < _channelCount; j++) { if (_channelSettings[j].enabled) { Console.Write("{0}\t", adc_to_mv(minPinned[j].Target[i], (int)_channelSettings[(int)(Imports.Channel.ChannelA + j)].range)); } } Console.WriteLine(); } sampleCount = Math.Min(sampleCount, BUFFER_SIZE); TextWriter writer = new StreamWriter("data.txt", false); for (int i = 0; i < sampleCount; i++) { for (int j = 0; j < _channelCount; j++) { writer.Write("{0}", (i * timeInterval)); if (_channelSettings[j].enabled) { writer.WriteLine(", {0}, {1}, {2}, {3}", minPinned[j].Target[i], adc_to_mv(minPinned[j].Target[i], (int)_channelSettings[(int)(Imports.Channel.ChannelA + j)].range), maxPinned[j].Target[i], adc_to_mv(maxPinned[j].Target[i], (int)_channelSettings[(int)(Imports.Channel.ChannelA + j)].range)); } } writer.WriteLine(); } writer.Close(); } else { Console.WriteLine("data collection aborted"); WaitForKey(); } }