09-07-2023 07:05 AM
Hello,
Error: -200279
I am currently designing a digital filter for sensor smoothing using the USB-6002 programmed on Visual C#. I keep getting the error "application is unable to keep up with hardware acquisition". I have tried decreasing the sample rate, removing UI updates, increasing number of samples to read however, the problem persists. I am aware that this is because I am not reading from the PC Buffer fast enough but how do I increase the read speed? Below is the source code:
using System;
using System.Data;
using System.Windows.Forms;
using NationalInstruments.DAQmx;
namespace filterFaster
{
public partial class Form1 : Form
{
private Task analogInTask;
private AnalogSingleChannelReader analogInReader;
private AsyncCallback analogCallback;
private AIChannel myAIChannel;
private Task runningTask;
private NationalInstruments.AnalogWaveform<double> data;
double currX = 0;
double currY = 0;
double prevX = 0;
double prevY = 0;
int count = 0;
double time = 0;
private DateTime value;
private DateTime value2;
private TimeSpan interval;
bool first = true;
public Form1()
{
InitializeComponent();
initializeChart();
}
private void initializeChart()
{
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
chart1.ChartAreas[0].RecalculateAxesScale();
chart1.ChartAreas[0].AxisY.Maximum = Double.NaN;
chart1.ChartAreas[0].CursorY.AutoScroll = true;
chart1.ChartAreas[0].AxisY.ScrollBar.Size = 15;
chart1.ChartAreas[0].AxisY.ScrollBar.IsPositionedInside = true;
chart1.ChartAreas[0].AxisY.ScrollBar.Enabled = true;
chart1.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
}
private void startButton_Click(object sender, EventArgs e)
{
if (runningTask == null)
{
try
{
startButton.Enabled = false;
// Create a new task
analogInTask = new Task();
// Create a virtual channel
analogInTask.AIChannels.CreateVoltageChannel("dev2/ai0", "AIChannel",
AITerminalConfiguration.Rse, 0,
5, AIVoltageUnits.Volts);
// Configure the timing parameters
analogInTask.Timing.ConfigureSampleClock("", 48000,
SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples);
analogInTask.Stream.Buffer.InputBufferSize=100000;
// Verify the Task
analogInTask.Control(TaskAction.Verify);
// Prepare the table for Data
runningTask = analogInTask;
analogInReader = new AnalogSingleChannelReader(analogInTask.Stream);
analogCallback = new AsyncCallback(AnalogInCallback);
// Use SynchronizeCallbacks to specify that the object
// marshals callbacks across threads appropriately.
analogInReader.SynchronizeCallbacks = true;
value = DateTime.Now;
analogInReader.BeginReadWaveform(Convert.ToInt32(1),
analogCallback, analogInTask);
}
catch (Exception exception)
{
// Display Errors
MessageBox.Show(exception.Message);
runningTask = null;
analogInTask.Dispose();
startButton.Enabled = true;
}
}
}
private void AnalogInCallback(IAsyncResult ar)
{
try
{
if (runningTask != null && runningTask == ar.AsyncState)
{
// Read the available data from the channels
data = analogInReader.EndReadWaveform(ar);
// Plot your data here
currY = data.Samples[0].Value;
interval=data.Samples[0].TimeStamp.Subtract(value);
time = interval.TotalSeconds;
//fc=1000Hz , fs=48kHz Butterworth
currX = 0.877 * prevX + 0.0615*currY+ 0.0615* prevY;
//fc=100Hz, fs=48kHz
//currX=0.987*prevX+0.0065*currY+0.0065*prevY;
chart1.Series[0].Points.AddXY(time, currY);
chart1.Series[1].Points.AddXY(time, currX);
chart1.ChartAreas[0].RecalculateAxesScale();
if (chart1.Series[0].Points.Count > 200)
{
chart1.Series[0].Points.RemoveAt(0);
chart1.Series[1].Points.RemoveAt(0);
chart1.ChartAreas[0].AxisX.Minimum = chart1.Series[0].Points[0].XValue;
chart1.ChartAreas[0].AxisX.Maximum = time;
}
prevX = currX;
prevY = currY;
analogInReader.BeginMemoryOptimizedReadWaveform(1,
analogCallback, analogInTask, data);
}
}
catch (Exception exception)
{
// Display Errors
MessageBox.Show(exception.Message);
runningTask = null;
analogInTask.Dispose();
startButton.Enabled = true;
}
}
private void displayData(NationalInstruments.AnalogWaveform<double> data)
{
for(int i = 0; i < data.SampleCount; i++)
{
currY = data.Samples[i].Value;
interval = data.Samples[i].TimeStamp.Subtract(value);
time = interval.TotalSeconds;
//fc=1000Hz , fs=48kHz Butterworth
currX = 0.877 * prevX + 0.0615 * currY + 0.0615 * prevY;
//fc=100Hz, fs=48kHz
//currX=0.987*prevX+0.0065*currY+0.0065*prevY;
chart1.Series[0].Points.AddXY(time, currY);
chart1.Series[1].Points.AddXY(time, currX);
chart1.ChartAreas[0].RecalculateAxesScale();
if (chart1.Series[0].Points.Count > 200)
{
chart1.Series[0].Points.RemoveAt(0);
chart1.Series[1].Points.RemoveAt(0);
chart1.ChartAreas[0].AxisX.Minimum = chart1.Series[0].Points[0].XValue;
chart1.ChartAreas[0].AxisX.Maximum = time;
}
prevX = currX;
prevY = currY;
}
}
}
}