Liza,根据您需要配置数字或模拟输入通道的设备。然后,您创建一个任务来监视您感兴趣的端口,并等待采集完成后回调。像这样的东西:
public void CreateAnalogInputTask()
{
try
{
// create a new task
myAITask = new Task();
// create a new virtual channel
myAITask.AIChannels.CreateVoltageChannel("Dev1/ai0, "", AITerminalConfiguration.Differential, -10, 10, AIVoltageUnits.Volts);
// configure the timing
myAITask.Timing.ConfigureSampleClock("",
AISampleRate,
SampleClockActiveEdge.Rising,
SampleQuantityMode.ContinuousSamples,
numberOfAISamples);
myAITask.Stream.Buffer.InputBufferSize = 10000000;
// verify the task
myAITask.Control(TaskAction.Verify);
// create the analogReader object
analogInReader = new AnalogMultiChannelReader(myAITask.Stream);
analogInReader.SynchronizeCallbacks = false;
arAnalogInReader = analogInReader.BeginReadWaveform(numberOfAISamples, AnalogInputRead, myAITask);
}
catch (Exception ex)
{
log.Error("", ex);
if (myAITask != null)
myAITask.Dispose();
}
}
然后在调用此回调时读取数据:
public void AnalogInputRead(IAsyncResult ar)
{
try
{
try
{
myAIdata = analogInReader.EndReadWaveform(ar);
}
catch (DaqException ex)
{
if (ex.Error != 88710)
throw;
else
log.Debug("DaqException 88710 ignored", ex);
}
for (int i = 0; i < numberOfAISamples; i++)
{
myData[i] = myAIdata[0].Samples[i].Value;
}
}
catch (Exception ex)
{
log.Error("", ex);
}
}