PicoScope 7 Software
Available on Windows, Mac and Linux
Code: Select all
[DllImport("USBDrDAQ.dll", CallingConvention = CallingConvention.StdCall)]
private static extern int UsbDrDaqOpenUnit(ref short handle);
[DllImport("USBDrDAQ.dll", CallingConvention = CallingConvention.StdCall)]
private static extern int UsbDrDaqCloseUnit(short handle);
[DllImport("USBDrDAQ.dll", CallingConvention = CallingConvention.StdCall)]
private static extern int UsbDrDaqRun(short handle, uint noOfValues, byte method);
[DllImport("USBDrDAQ.dll", CallingConvention = CallingConvention.StdCall)]
private static extern int UsbDrDaqReady(short handle, ref short ready);
[DllImport("USBDrDAQ.dll", CallingConvention = CallingConvention.StdCall)]
private static extern int UsbDrDaqStop(short handle);
[DllImport("USBDrDAQ.dll", CallingConvention = CallingConvention.StdCall)]
private static extern int UsbDrDaqSetInterval(short handle, ref uint usForBlock, uint idealNoOfSamples,
uint[] channels, short noOfChannels);
[DllImport("USBDrDAQ.dll", CallingConvention = CallingConvention.StdCall)]
private static extern int UsbDrDaqSetTrigger(short handle, short usFenabledorBlock, short autoTrigger,
short autoMs, short channel, short dir, short threshold,
short hysterisis, float delay);
[DllImport("USBDrDAQ.dll", CallingConvention = CallingConvention.StdCall)]
private static extern int UsbDrDaqGetValues(short handle, short[] values, ref uint noOfValues,
ref short overflow, ref uint triggerIndex);
Code: Select all
short handle;
int status = 0;
const uint nSamples = 20000; //maximal possible samples
const uint nSampleBufferSize = nSamples * 2 * 2; //large enought buffer
var samples = new short[nSampleBufferSize]; //buffer
uint usForBlock = 100 * 1000; //get blocks each 100ms
var channels = new uint[] { 1, 4 }; //ext1 = 1, scope = 4
short nChannels = 2;
//worker variables
uint nSamplesCollected = nSampleBufferSize;
short overflow = 0;
uint triggerIndex = 0;
int sleepTime = Convert.ToInt32(usForBlock/1000); //sleep time for worker thread (100ms)
_shouldStop = false;
//Open Unit
if (UsbDrDaqOpenUnit(ref handle) != 0)
throw new Exception("Error on Open");
//Set the trigger (disabled)
status = UsbDrDaqSetTrigger(handle, 0, 0, 0, 0, 0, 0, 0, 0);
if (status != 0)
throw new Exception(string.Format("Error on UsbDrDaqSetTrigger {0}", status));
//set sampling rate and channels
status = UsbDrDaqSetInterval(handle, ref usForBlock, nSamples, channels, nChannels);
if (status != 0)
throw new Exception(string.Format("Error on UsbDrDaqSetInterval {0}", status));
//Start streaming
status = UsbDrDaqRun(handle, nSamples, 2);
if (status != 0)
throw new Exception(string.Format("Error on UsbDrDaqReady {0}", status));
//Wait until unit is ready
short isReady = 0;
while (isReady == 0)
{
status = UsbDrDaqReady(handle, ref isReady);
if (status != 0)
throw new Exception(string.Format("Error on UsbDrDaqReady {0}", status));
}
//get first measure and throw it (incomplete set of data)
status = UsbDrDaqGetValues(handle, samples, ref nSamplesCollected, ref overflow, ref triggerIndex);
if (status != 0)
throw new Exception(string.Format("Error on First UsbDrDaqGetValues {0}", status));
//wait to complete
Thread.Sleep(sleepTime);
//start working loop
while (!_shouldStop)
{
//reset worker vars
nSamplesCollected = nSampleBufferSize;
overflow = 0;
triggerIndex = 0;
//get values from DrDaq
status = UsbDrDaqGetValues(handle, samples, ref nSamplesCollected, ref overflow, ref triggerIndex);
if (status != 0)
throw new Exception(string.Format("Error on Loop UsbDrDaqGetValues {0}", status));
//define output array with correct size
var sampleList = new short[nSamplesCollected];
//transfer values to output array
for (var index = 0; index < nSamplesCollected; index++)
sampleList[index] = samples[index];
//detect overflow
if (overflow != 0)
Debug.WriteLine("-- OverFlow --" + overflow);
//pass sampleList to main process, in the main process this data will be processed
//!!sampleList array positions are not the same for each call of UsbDrDaqGetValues!!
((BackgroundWorker)workerObject).ReportProgress(0, sampleList);
//wait for next capture
Thread.Sleep(sleepTime);
}
//stop DrDaq
status = UsbDrDaqStop(handle);
if (status != 0)
throw new Exception(string.Format("Error on UsbDrDaqStop {0}", status));
//close Unit
if(UsbDrDaqCloseUnit(handle) != 0)
throw new Exception("Error on Close");
Code: Select all
for (uint index = 0; index < nSamplesCollected; index ++)
{
if (index % 2 == 0 || index == 0)
{
file1.WriteLine(samples[index].ToString());
}
else
{
file2.WriteLine(samples[index].ToString());
}
}
1/10 microseconds = 100 / 1 millisecond = 100.000 / 1 secThe fastest possible sampling interval is 1 microsecond, when the number of samples
is 8192 divided by the number of channels active and the capture mode is
BM_SINGLE. Under all other conditions, the fastest possible sampling interval is 10
microseconds.
Code: Select all
static void Main()
{
//my target is to get 100.000 samples per second!
short handle = 0;
int status = 0;
const uint nSamples = 5000;
const uint nSampleBufferSize = nSamples * 2;
var samples = new short[nSampleBufferSize];
uint usForBlock = 50 * 1000; //get blocks each 50ms
//this is not working (should get 50.000 samples/sec @two channels)
//error UsbDrDaqRun 2B PICO_INVALID_SAMPLE_INTERVAL!!
//if i change nSamples to 2500 its working but i get only 25.000 samples/sec @two channels
//my question is how to get 100.000 samples with two channels (50.000 each channel)?
var channels = new uint[] { 1, 4 }; //ext1 = 1, scope = 4
//this is working!!! (100.000 samples/sec @one channel)
//var channels = new uint[] { 1 }; //ext1 = 1, scope = 4
short nChannels = (short)channels.Length;
//worker variables
uint nSamplesCollected = nSampleBufferSize;
short overflow = 0;
uint triggerIndex = 0;
int sleepTime = Convert.ToInt32(usForBlock / 1000); //sleep time for worker thread (100ms)
//Open Unit
if ((status = Imports.UsbDrDaqOpenUnit(ref handle) )!= 0)
throw new Exception(string.Format("Error on Open {0}", status));
//Set the trigger (disabled)
status = Imports.UsbDrDaqSetTrigger(handle, 0, 0, 0, 0, 0, 0, 0, 0);
if (status != 0)
throw new Exception(string.Format("Error on UsbDrDaqSetTrigger {0}", status));
//set sampling rate and channels
status = Imports.UsbDrDaqSetInterval(handle, ref usForBlock, nSamples, channels, nChannels);
if (status != 0)
throw new Exception(string.Format("Error on UsbDrDaqSetInterval {0}", status));
//Start streaming
status = Imports.UsbDrDaqRun(handle, nSamples, 2);
if (status != 0)
throw new Exception(string.Format("Error on UsbDrDaqRun {0}", status));
//Wait until unit is ready
short isReady = 0;
while (isReady == 0)
{
status = Imports.UsbDrDaqReady(handle, ref isReady);
if (status != 0)
throw new Exception(string.Format("Error on UsbDrDaqReady {0}", status));
}
//get first measure and throw it (incomplete set of data)
status = Imports.UsbDrDaqGetValues(handle, samples, ref nSamplesCollected, ref overflow, ref triggerIndex);
if (status != 0)
throw new Exception(string.Format("Error on First UsbDrDaqGetValues {0}", status));
//wait to complete
Thread.Sleep(0);
TextWriter file1 = new StreamWriter("stream1.txt", false);
TextWriter file2 = new StreamWriter("stream2.txt", false);
long sumSamples = 0;
var timer = new Stopwatch();
timer.Start();
Console.WriteLine("Capture started... (duration ~10 sec)");
//start working loop
while (timer.ElapsedMilliseconds < 10 * 1000)
{
//reset worker vars
nSamplesCollected = nSampleBufferSize;
overflow = 0;
triggerIndex = 0;
//get values from DrDaq
status = Imports.UsbDrDaqGetValues(handle, samples, ref nSamplesCollected, ref overflow, ref triggerIndex);
if (status != 0)
throw new Exception(string.Format("Error on Loop UsbDrDaqGetValues {0}", status));
sumSamples += nSamplesCollected;
for (uint index = 0; index < nSamplesCollected; index ++)
{
if (index % 2 == 0 || index == 0)
{
file1.WriteLine(samples[index].ToString());
}
else
{
file2.WriteLine(samples[index].ToString());
}
}
//wait for next capture
Thread.Sleep(0);
}
file1.Close();
file2.Close();
timer.Stop();
//stop DrDaq
status = Imports.UsbDrDaqStop(handle);
if (status != 0)
throw new Exception(string.Format("Error on UsbDrDaqStop {0}", status));
//close Unit
if (Imports.UsbDrDaqCloseUnit(handle) != 0)
throw new Exception("Error on Close");
//summary
Console.WriteLine("TotalTime: " + timer.ElapsedMilliseconds);
Console.WriteLine("TotalSamplesCaptured: " + sumSamples);
Console.WriteLine("Samples/Block: " + nSamples + " MilliSeconds/Block: " + usForBlock / 1000);
Console.WriteLine("Calculated Samples/Sec: " + Math.Round((((double)nSamples * 1000) / ((double)usForBlock / 1000)), 3));
Console.WriteLine("Samples/Sec: " + Math.Round(((double)sumSamples / timer.ElapsedMilliseconds * 1000), 3));
Console.WriteLine("Press any Key to Close.");
Console.ReadKey();
}
Code: Select all
const uint nSamples = 50000;
uint usForBlock = 50 * 10000; //get blocks each 500ms
Code: Select all
const uint nSamples = 50000;
uint usForBlock = 500 * 1000; //get blocks each 500ms
Code: Select all
status = Imports.UsbDrDaqSetInterval(handle, ref usForBlock, nSamples, channels, nChannels);